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

Sidebar.motion.ts

246 lines8.5 KB

1const motionTiming = { duration: 150, easing: "ease-out" };
2const rowTravel = (height: number) => Math.min(height, 40);
3// Fades clone removed rows and animate clones and entries; intervening layout reads force reflows.
4const MAX_FADED_ROWS_PER_UPDATE = 40;
5​
6type RowPosition = { top: number; left: number; width: number; height: number };
7​
8function progress(animation: Animation) {
9 return animation.playState === "finished"
10 ? 1
11 : (animation.effect?.getComputedTiming().progress ?? 0);
13​
14/** Position the list so all direct children's offsetTop share an origin. */
15export function createSidebarListMotion(parent: HTMLUListElement) {
16 let positions: Map<HTMLElement, RowPosition> | null = null;
17 let disposed = false;
18 const reducedMotion = parent.ownerDocument.defaultView?.matchMedia(
19 "(prefers-reduced-motion: reduce)",
20 );
21 const running = new Map<HTMLElement, { animation: Animation; offset: number }>();
22 const entering = new Map<HTMLElement, { animation: Animation; travel: number }>();
23 const exiting = new Map<HTMLElement, Animation>();
24 let released: Map<HTMLElement, number> | null = null;
25​
26 const remainingOffset = (node: HTMLElement) => {
27 const current = running.get(node);
28 const run = current ? current.offset * (1 - progress(current.animation)) : 0;
29 const entry = entering.get(node);
30 const enter = entry ? entry.travel * (1 - progress(entry.animation)) : 0;
31 return run + enter;
32 };
33 const clearFades = () => {
34 for (const entry of entering.values()) entry.animation.cancel();
35 for (const animation of exiting.values()) animation.cancel();
36 for (const node of exiting.keys()) node.remove();
37 entering.clear();
38 exiting.clear();
39 };
40 const fadeOut = (node: HTMLElement, position: RowPosition, travel: number) => {
41 if (position.height === 0) return;
42 const clone = node.cloneNode(true) as HTMLElement;
43 for (const element of [clone, ...clone.querySelectorAll("*")]) {
44 for (const attribute of Array.from(element.attributes)) {
45 if (
46 (attribute.name === "id" && element.namespaceURI !== "http://www.w3.org/2000/svg") ||
47 attribute.name === "data-thread-item" ||
48 attribute.name === "data-thread-selection-safe" ||
49 attribute.name === "data-testid"
50 ) {
51 element.removeAttribute(attribute.name);
52 }
53 }
54 }
55 clone.setAttribute("aria-hidden", "true");
56 clone.inert = true;
57 Object.assign(clone.style, {
58 position: "absolute",
59 top: `${position.top + remainingOffset(node)}px`,
60 left: `${position.left}px`,
61 width: `${position.width}px`,
62 height: `${position.height}px`,
63 margin: "0",
64 boxSizing: "border-box",
65 contentVisibility: "visible",
66 transform: "none",
67 transition: "none",
68 pointerEvents: "none",
69 });
70 parent.append(clone);
71 const entry = entering.get(node);
72 const entryProgress = entry ? progress(entry.animation) : 1;
73 const animation = clone.animate(
74 [
75 { opacity: entryProgress, transform: "translateY(0px)" },
76 { opacity: 0, transform: `translateY(${travel}px)` },
77 ],
78 motionTiming,
79 );
80 exiting.set(clone, animation);
81 animation.addEventListener(
82 "finish",
83 () => {
84 clone.remove();
85 exiting.delete(clone);
86 },
87 { once: true },
88 );
89 };
90​
91 const cancel = (node: HTMLElement) => {
92 running.get(node)?.animation.cancel();
93 running.delete(node);
94 };
95 const suspend = () => {
96 for (const node of running.keys()) cancel(node);
97 clearFades();
98 positions = null;
99 released = null;
100 };
101 const move = (node: HTMLElement, offset: number) => {
102 cancel(node);
103 const entry = entering.get(node);
104 if (entry) entry.travel = 0;
105 if (offset === 0 && !entry) return;
106 const animation = node.animate(
107 [{ transform: `translateY(${offset}px)` }, { transform: "translateY(0px)" }],
108 motionTiming,
109 );
110 running.set(node, { animation, offset });
111 animation.addEventListener(
112 "finish",
113 () => {
114 if (running.get(node)?.animation === animation) running.delete(node);
115 },
116 { once: true },
117 );
118 };
119​
120 return {
121 update(animate: boolean) {
122 if (disposed) return;
123 const next = new Map(
124 Array.from(parent.children)
125 .filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node))
126 .map((node) => [
127 node,
128 {
129 top: node.offsetTop,
130 left: node.offsetLeft,
131 width: node.offsetWidth,
132 height: node.offsetHeight,
133 },
134 ]),
135 );
136 let fadeCount = 0;
137 if (positions !== null) {
138 for (const [node, position] of positions) {
139 if (!next.has(node) && position.height > 0) fadeCount++;
140 }
141 for (const [node, position] of next) {
142 if (!positions.has(node) && position.height > 0) fadeCount++;
143 }
144 }
145 const shouldAnimate =
146 animate &&
147 positions !== null &&
148 !reducedMotion?.matches &&
149 fadeCount <= MAX_FADED_ROWS_PER_UPDATE;
150 const movedDelta = new Map<HTMLElement, number>();
151 const nextOrder = [...next.keys()];
152 const oldOrder = positions === null ? [] : [...positions.keys()];
153 const ridingDelta = (
154 order: readonly HTMLElement[],
155 index: number,
156 retained: (node: HTMLElement) => boolean,
157 ) => {
158 for (let cursor = index - 1; cursor >= 0; cursor--) {
159 const node = order[cursor]!;
160 const delta = movedDelta.get(node);
161 if (delta !== undefined) return delta;
162 if (retained(node)) return remainingOffset(node);
163 }
164 return undefined;
165 };
166 if (!shouldAnimate) clearFades();
167 else {
168 for (const [node, position] of next) {
169 const previousTop = positions!.get(node)?.top;
170 if (previousTop === undefined || previousTop === position.top) continue;
171 movedDelta.set(node, previousTop + remainingOffset(node) - position.top);
172 }
173 for (const [node, position] of positions!) {
174 if (next.has(node)) continue;
175 const delta = ridingDelta(oldOrder, oldOrder.indexOf(node), (n) => next.has(n));
176 fadeOut(node, position, delta === undefined ? rowTravel(position.height) : -delta);
177 }
178 }
179 for (const [node, entry] of entering) {
180 if (!next.has(node)) {
181 entry.animation.cancel();
182 entering.delete(node);
183 }
184 }
185 for (const node of running.keys()) {
186 if (!shouldAnimate || !next.has(node)) cancel(node);
187 }
188 if (shouldAnimate) {
189 for (const [index, node] of nextOrder.entries()) {
190 const position = next.get(node)!;
191 const previousTop = positions!.get(node)?.top;
192 if (previousTop === undefined) {
193 if (position.height > 0) {
194 const delta = ridingDelta(nextOrder, index, (n) => positions!.has(n));
195 const travel = delta === undefined ? -rowTravel(position.height) : delta;
196 const animation = node.animate(
197 [
198 { opacity: 0, transform: `translateY(${travel}px)` },
199 { opacity: 1, transform: "translateY(0px)" },
200 ],
201 motionTiming,
202 );
203 entering.set(node, { animation, travel });
204 animation.addEventListener(
205 "finish",
206 () => {
207 if (entering.get(node)?.animation === animation) entering.delete(node);
208 },
209 { once: true },
210 );
211 }
212 continue;
213 }
214 const delta = movedDelta.get(node);
215 // Computed progress includes easing; carry forward only our translation, never dnd-kit's
216 // transforms.
217 if (delta !== undefined) move(node, delta);
218 }
219 }
220 if (released !== null) {
221 if (!reducedMotion?.matches) {
222 for (const [node, position] of next) {
223 const top = released.get(node);
224 if (top !== undefined) move(node, top - position.top);
225 }
226 }
227 released = null;
228 }
229 positions = next;
230 },
231 release() {
232 suspend();
233 const origin = parent.getBoundingClientRect().top;
234 released = new Map(
235 Array.from(parent.children)
236 .filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node))
237 .map((node) => [node, node.getBoundingClientRect().top - origin]),
238 );
239 },
240 suspend,
241 dispose() {
242 suspend();
243 disposed = true;
244 },
245 };