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

Sidebar.motion.ts

215 lines7.4 KB

1const motionTiming = { duration: 150, easing: "ease-out" };
2// A project filter change or a bulk snooze swaps a large part of the list at
3// once. Fades are the expensive part: every removed row gets a deep clone and
4// every clone and entering row gets its own animation, and the layout reads
5// in between force synchronous reflows. Translating displaced rows is cheap,
6// so only the fade count decides whether an update animates.
7const MAX_FADED_ROWS_PER_UPDATE = 40;
8​
9type RowPosition = { top: number; left: number; width: number; height: number };
10​
11function progress(animation: Animation) {
12 return animation.playState === "finished"
13 ? 1
14 : (animation.effect?.getComputedTiming().progress ?? 0);
16​
17/** Animate rows between their layout positions. The list must be
18 * positioned so every direct child's offsetTop has the same origin. */
19export function createSidebarListMotion(parent: HTMLUListElement) {
20 let positions: Map<HTMLElement, RowPosition> | null = null;
21 let disposed = false;
22 const reducedMotion = parent.ownerDocument.defaultView?.matchMedia(
23 "(prefers-reduced-motion: reduce)",
24 );
25 const running = new Map<HTMLElement, { animation: Animation; offset: number }>();
26 const entering = new Map<HTMLElement, Animation>();
27 const exiting = new Map<HTMLElement, Animation>();
28 // Visual tops at drag release, relative to the list, so the release
29 // commit can glide every row from where dnd-kit left it into its slot.
30 let released: Map<HTMLElement, number> | null = null;
31​
32 const remainingOffset = (node: HTMLElement) => {
33 const current = running.get(node);
34 return current ? current.offset * (1 - progress(current.animation)) : 0;
35 };
36 const clearFades = () => {
37 for (const animation of [...entering.values(), ...exiting.values()]) animation.cancel();
38 for (const node of exiting.keys()) node.remove();
39 entering.clear();
40 exiting.clear();
41 };
42 const fadeOut = (node: HTMLElement, position: RowPosition) => {
43 if (position.height === 0) return;
44 // React owns the removed row; only a noninteractive copy stays for the fade.
45 const clone = node.cloneNode(true) as HTMLElement;
46 for (const element of [clone, ...clone.querySelectorAll("*")]) {
47 for (const attribute of Array.from(element.attributes)) {
48 if (
49 (attribute.name === "id" && element.namespaceURI !== "http://www.w3.org/2000/svg") ||
50 attribute.name === "data-thread-item" ||
51 attribute.name === "data-thread-selection-safe" ||
52 attribute.name === "data-testid"
53 ) {
54 element.removeAttribute(attribute.name);
55 }
56 }
57 }
58 clone.setAttribute("aria-hidden", "true");
59 clone.inert = true;
60 Object.assign(clone.style, {
61 position: "absolute",
62 top: `${position.top + remainingOffset(node)}px`,
63 left: `${position.left}px`,
64 width: `${position.width}px`,
65 height: `${position.height}px`,
66 margin: "0",
67 boxSizing: "border-box",
68 contentVisibility: "visible",
69 transform: "none",
70 transition: "none",
71 pointerEvents: "none",
72 });
73 parent.append(clone);
74 const entry = entering.get(node);
75 const animation = clone.animate(
76 [{ opacity: entry ? progress(entry) : 1 }, { opacity: 0 }],
77 motionTiming,
78 );
79 exiting.set(clone, animation);
80 animation.addEventListener(
81 "finish",
82 () => {
83 clone.remove();
84 exiting.delete(clone);
85 },
86 { once: true },
87 );
88 };
89​
90 const cancel = (node: HTMLElement) => {
91 running.get(node)?.animation.cancel();
92 running.delete(node);
93 };
94 const suspend = () => {
95 for (const node of running.keys()) cancel(node);
96 clearFades();
97 positions = null;
98 released = null;
99 };
100 const move = (node: HTMLElement, offset: number) => {
101 cancel(node);
102 if (offset === 0) return;
103 const animation = node.animate(
104 [{ transform: `translateY(${offset}px)` }, { transform: "translateY(0px)" }],
105 motionTiming,
106 );
107 running.set(node, { animation, offset });
108 animation.addEventListener(
109 "finish",
110 () => {
111 if (running.get(node)?.animation === animation) running.delete(node);
112 },
113 { once: true },
114 );
115 };
116​
117 return {
118 update(animate: boolean) {
119 if (disposed) return;
120 const next = new Map(
121 Array.from(parent.children)
122 .filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node))
123 .map((node) => [
124 node,
125 {
126 top: node.offsetTop,
127 left: node.offsetLeft,
128 width: node.offsetWidth,
129 height: node.offsetHeight,
130 },
131 ]),
132 );
133 let fadeCount = 0;
134 if (positions !== null) {
135 for (const [node, position] of positions) {
136 if (!next.has(node) && position.height > 0) fadeCount++;
137 }
138 for (const [node, position] of next) {
139 if (!positions.has(node) && position.height > 0) fadeCount++;
140 }
141 }
142 const shouldAnimate =
143 animate &&
144 positions !== null &&
145 !reducedMotion?.matches &&
146 fadeCount <= MAX_FADED_ROWS_PER_UPDATE;
147 if (!shouldAnimate) clearFades();
148 else {
149 for (const [node, position] of positions!) {
150 if (!next.has(node)) fadeOut(node, position);
151 }
152 }
153 for (const [node, animation] of entering) {
154 if (!next.has(node)) {
155 animation.cancel();
156 entering.delete(node);
157 }
158 }
159 for (const node of running.keys()) {
160 if (!shouldAnimate || !next.has(node)) cancel(node);
161 }
162 if (shouldAnimate) {
163 for (const [node, position] of next) {
164 const previousTop = positions?.get(node)?.top;
165 if (previousTop === undefined) {
166 if (position.height > 0) {
167 const animation = node.animate([{ opacity: 0 }, { opacity: 1 }], motionTiming);
168 entering.set(node, animation);
169 animation.addEventListener(
170 "finish",
171 () => {
172 if (entering.get(node) === animation) entering.delete(node);
173 },
174 { once: true },
175 );
176 }
177 continue;
178 }
179 if (previousTop === position.top) continue;
180 // Computed progress includes the effect's easing. Only our own
181 // translate is carried forward; dnd-kit's transforms are never read.
182 move(node, previousTop + remainingOffset(node) - position.top);
183 }
184 }
185 if (released !== null) {
186 if (!reducedMotion?.matches) {
187 for (const [node, position] of next) {
188 const top = released.get(node);
189 if (top !== undefined) move(node, top - position.top);
190 }
191 }
192 released = null;
193 }
194 positions = next;
195 },
196 /** Called on drag release, before the commit that clears dnd-kit's
197 * transforms. Takes every row's visual top, including the lifted row
198 * under the pointer and the peers holding the label gaps open, so the
199 * next update glides each of them into its committed slot. */
200 release() {
201 suspend();
202 const origin = parent.getBoundingClientRect().top;
203 released = new Map(
204 Array.from(parent.children)
205 .filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node))
206 .map((node) => [node, node.getBoundingClientRect().top - origin]),
207 );
208 },
209 suspend,
210 dispose() {
211 suspend();
212 disposed = true;
213 },
214 };