Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Sidebar.motion.ts

168 lines5.6 KB

1const motionTiming = { duration: 150, easing: "ease-out" };
2​
3type RowPosition = { top: number; left: number; width: number; height: number };
4​
5function progress(animation: Animation) {
6 return animation.playState === "finished"
7 ? 1
8 : (animation.effect?.getComputedTiming().progress ?? 0);
9}
10​
11/** Animate rows between their layout positions. The list must be
12 * positioned so every direct child's offsetTop has the same origin. */
13export function createSidebarListMotion(parent: HTMLUListElement) {
14 let positions: Map<HTMLElement, RowPosition> | null = null;
15 let disposed = false;
16 const reducedMotion = parent.ownerDocument.defaultView?.matchMedia(
17 "(prefers-reduced-motion: reduce)",
18 );
19 const running = new Map<HTMLElement, { animation: Animation; offset: number }>();
20 const entering = new Map<HTMLElement, Animation>();
21 const exiting = new Map<HTMLElement, Animation>();
22​
23 const remainingOffset = (node: HTMLElement) => {
24 const current = running.get(node);
25 return current ? current.offset * (1 - progress(current.animation)) : 0;
26 };
27 const clearFades = () => {
28 for (const animation of [...entering.values(), ...exiting.values()]) animation.cancel();
29 for (const node of exiting.keys()) node.remove();
30 entering.clear();
31 exiting.clear();
32 };
33 const fadeOut = (node: HTMLElement, position: RowPosition) => {
34 if (position.height === 0) return;
35 // React owns the removed row; only a noninteractive copy stays for the fade.
36 const clone = node.cloneNode(true) as HTMLElement;
37 for (const element of [clone, ...clone.querySelectorAll("*")]) {
38 for (const attribute of Array.from(element.attributes)) {
39 if (
40 (attribute.name === "id" && element.namespaceURI !== "http://www.w3.org/2000/svg") ||
41 attribute.name === "data-thread-item" ||
42 attribute.name === "data-thread-selection-safe" ||
43 attribute.name === "data-testid"
44 ) {
45 element.removeAttribute(attribute.name);
46 }
47 }
48 }
49 clone.setAttribute("aria-hidden", "true");
50 clone.inert = true;
51 Object.assign(clone.style, {
52 position: "absolute",
53 top: `${position.top + remainingOffset(node)}px`,
54 left: `${position.left}px`,
55 width: `${position.width}px`,
56 height: `${position.height}px`,
57 margin: "0",
58 boxSizing: "border-box",
59 contentVisibility: "visible",
60 transform: "none",
61 transition: "none",
62 pointerEvents: "none",
63 });
64 parent.append(clone);
65 const entry = entering.get(node);
66 const animation = clone.animate(
67 [{ opacity: entry ? progress(entry) : 1 }, { opacity: 0 }],
68 motionTiming,
69 );
70 exiting.set(clone, animation);
71 animation.addEventListener(
72 "finish",
73 () => {
74 clone.remove();
75 exiting.delete(clone);
76 },
77 { once: true },
78 );
79 };
80​
81 const cancel = (node: HTMLElement) => {
82 running.get(node)?.animation.cancel();
83 running.delete(node);
84 };
85 const suspend = () => {
86 for (const node of running.keys()) cancel(node);
87 clearFades();
88 positions = null;
89 };
90​
91 return {
92 update(animate: boolean) {
93 if (disposed) return;
94 const next = new Map(
95 Array.from(parent.children)
96 .filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node))
97 .map((node) => [
98 node,
99 {
100 top: node.offsetTop,
101 left: node.offsetLeft,
102 width: node.offsetWidth,
103 height: node.offsetHeight,
104 },
105 ]),
106 );
107 const shouldAnimate = animate && positions !== null && !reducedMotion?.matches;
108 if (!shouldAnimate) clearFades();
109 else {
110 for (const [node, position] of positions!) {
111 if (!next.has(node)) fadeOut(node, position);
112 }
113 }
114 for (const [node, animation] of entering) {
115 if (!next.has(node)) {
116 animation.cancel();
117 entering.delete(node);
118 }
119 }
120 for (const node of running.keys()) {
121 if (!shouldAnimate || !next.has(node)) cancel(node);
122 }
123 if (shouldAnimate) {
124 for (const [node, position] of next) {
125 const previousTop = positions?.get(node)?.top;
126 if (previousTop === undefined) {
127 if (position.height > 0) {
128 const animation = node.animate([{ opacity: 0 }, { opacity: 1 }], motionTiming);
129 entering.set(node, animation);
130 animation.addEventListener(
131 "finish",
132 () => {
133 if (entering.get(node) === animation) entering.delete(node);
134 },
135 { once: true },
136 );
137 }
138 continue;
139 }
140 if (previousTop === position.top) continue;
141 // Computed progress includes the effect's easing. Only our own
142 // translate is carried forward; dnd-kit's transforms are never read.
143 const offset = previousTop + remainingOffset(node) - position.top;
144 cancel(node);
145 if (offset === 0) continue;
146 const animation = node.animate(
147 [{ transform: `translateY(${offset}px)` }, { transform: "translateY(0px)" }],
148 motionTiming,
149 );
150 running.set(node, { animation, offset });
151 animation.addEventListener(
152 "finish",
153 () => {
154 if (running.get(node)?.animation === animation) running.delete(node);
155 },
156 { once: true },
157 );
158 }
159 }
160 positions = next;
161 },
162 suspend,
163 dispose() {
164 suspend();
165 disposed = true;
166 },
167 };