const motionTiming = { duration: 150, easing: "ease-out" }; // Rows normally ride their displaced neighbour's travel. Absent a moving // neighbour, a row still travels on its own, clamped so a tall card does not // slide its full height. const rowTravel = (height: number) => Math.min(height, 40); // A project filter change or a bulk snooze swaps a large part of the list at // once. Fades are the expensive part: every removed row gets a deep clone and // every clone and entering row gets its own animation, and the layout reads // in between force synchronous reflows. Translating displaced rows is cheap, // so only the fade count decides whether an update animates. const MAX_FADED_ROWS_PER_UPDATE = 40; type RowPosition = { top: number; left: number; width: number; height: number }; function progress(animation: Animation) { return animation.playState === "finished" ? 1 : (animation.effect?.getComputedTiming().progress ?? 0); } /** Animate rows between their layout positions. The list must be * positioned so every direct child's offsetTop has the same origin. */ export function createSidebarListMotion(parent: HTMLUListElement) { let positions: Map | null = null; let disposed = false; const reducedMotion = parent.ownerDocument.defaultView?.matchMedia( "(prefers-reduced-motion: reduce)", ); const running = new Map(); const entering = new Map(); const exiting = new Map(); // Visual tops at drag release, relative to the list, so the release // commit can glide every row from where dnd-kit left it into its slot. let released: Map | null = null; const remainingOffset = (node: HTMLElement) => { const current = running.get(node); const run = current ? current.offset * (1 - progress(current.animation)) : 0; const entry = entering.get(node); const enter = entry ? entry.travel * (1 - progress(entry.animation)) : 0; return run + enter; }; const clearFades = () => { for (const entry of entering.values()) entry.animation.cancel(); for (const animation of exiting.values()) animation.cancel(); for (const node of exiting.keys()) node.remove(); entering.clear(); exiting.clear(); }; const fadeOut = (node: HTMLElement, position: RowPosition, travel: number) => { if (position.height === 0) return; // React owns the removed row; only a noninteractive copy stays for the fade. const clone = node.cloneNode(true) as HTMLElement; for (const element of [clone, ...clone.querySelectorAll("*")]) { for (const attribute of Array.from(element.attributes)) { if ( (attribute.name === "id" && element.namespaceURI !== "http://www.w3.org/2000/svg") || attribute.name === "data-thread-item" || attribute.name === "data-thread-selection-safe" || attribute.name === "data-testid" ) { element.removeAttribute(attribute.name); } } } clone.setAttribute("aria-hidden", "true"); clone.inert = true; Object.assign(clone.style, { position: "absolute", top: `${position.top + remainingOffset(node)}px`, left: `${position.left}px`, width: `${position.width}px`, height: `${position.height}px`, margin: "0", boxSizing: "border-box", contentVisibility: "visible", transform: "none", transition: "none", pointerEvents: "none", }); parent.append(clone); const entry = entering.get(node); const entryProgress = entry ? progress(entry.animation) : 1; const animation = clone.animate( [ { opacity: entryProgress, transform: "translateY(0px)" }, { opacity: 0, transform: `translateY(${travel}px)` }, ], motionTiming, ); exiting.set(clone, animation); animation.addEventListener( "finish", () => { clone.remove(); exiting.delete(clone); }, { once: true }, ); }; const cancel = (node: HTMLElement) => { running.get(node)?.animation.cancel(); running.delete(node); }; const suspend = () => { for (const node of running.keys()) cancel(node); clearFades(); positions = null; released = null; }; const move = (node: HTMLElement, offset: number) => { cancel(node); const entry = entering.get(node); // The newer transform supersedes entry travel; its original opacity keeps fading. if (entry) entry.travel = 0; if (offset === 0 && !entry) return; const animation = node.animate( [{ transform: `translateY(${offset}px)` }, { transform: "translateY(0px)" }], motionTiming, ); running.set(node, { animation, offset }); animation.addEventListener( "finish", () => { if (running.get(node)?.animation === animation) running.delete(node); }, { once: true }, ); }; return { update(animate: boolean) { if (disposed) return; const next = new Map( Array.from(parent.children) .filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node)) .map((node) => [ node, { top: node.offsetTop, left: node.offsetLeft, width: node.offsetWidth, height: node.offsetHeight, }, ]), ); let fadeCount = 0; if (positions !== null) { for (const [node, position] of positions) { if (!next.has(node) && position.height > 0) fadeCount++; } for (const [node, position] of next) { if (!positions.has(node) && position.height > 0) fadeCount++; } } const shouldAnimate = animate && positions !== null && !reducedMotion?.matches && fadeCount <= MAX_FADED_ROWS_PER_UPDATE; const movedDelta = new Map(); const nextOrder = [...next.keys()]; const oldOrder = positions === null ? [] : [...positions.keys()]; const ridingDelta = ( order: readonly HTMLElement[], index: number, retained: (node: HTMLElement) => boolean, ) => { for (let cursor = index - 1; cursor >= 0; cursor--) { const node = order[cursor]!; const delta = movedDelta.get(node); if (delta !== undefined) return delta; if (retained(node)) return remainingOffset(node); } return undefined; }; if (!shouldAnimate) clearFades(); else { // A shelf that opens above its collapsed anchor shifts every retained // row by the same amount. Entering rows take that same displacement so // the shelf arrives as one moving block instead of rows popping into // their final slots; exiting rows leave by it. for (const [node, position] of next) { const previousTop = positions!.get(node)?.top; if (previousTop === undefined || previousTop === position.top) continue; movedDelta.set(node, previousTop + remainingOffset(node) - position.top); } for (const [node, position] of positions!) { if (next.has(node)) continue; const delta = ridingDelta(oldOrder, oldOrder.indexOf(node), (n) => next.has(n)); fadeOut(node, position, delta === undefined ? rowTravel(position.height) : -delta); } } for (const [node, entry] of entering) { if (!next.has(node)) { entry.animation.cancel(); entering.delete(node); } } for (const node of running.keys()) { if (!shouldAnimate || !next.has(node)) cancel(node); } if (shouldAnimate) { for (const [index, node] of nextOrder.entries()) { const position = next.get(node)!; const previousTop = positions!.get(node)?.top; if (previousTop === undefined) { if (position.height > 0) { const delta = ridingDelta(nextOrder, index, (n) => positions!.has(n)); const travel = delta === undefined ? -rowTravel(position.height) : delta; const animation = node.animate( [ { opacity: 0, transform: `translateY(${travel}px)` }, { opacity: 1, transform: "translateY(0px)" }, ], motionTiming, ); entering.set(node, { animation, travel }); animation.addEventListener( "finish", () => { if (entering.get(node)?.animation === animation) entering.delete(node); }, { once: true }, ); } continue; } const delta = movedDelta.get(node); // Computed progress includes the effect's easing. Only our own // translate is carried forward; dnd-kit's transforms are never read. if (delta !== undefined) move(node, delta); } } if (released !== null) { if (!reducedMotion?.matches) { for (const [node, position] of next) { const top = released.get(node); if (top !== undefined) move(node, top - position.top); } } released = null; } positions = next; }, /** Called on drag release, before the commit that clears dnd-kit's * transforms. Takes every row's visual top, including the lifted row * under the pointer and the peers holding the label gaps open, so the * next update glides each of them into its committed slot. */ release() { suspend(); const origin = parent.getBoundingClientRect().top; released = new Map( Array.from(parent.children) .filter((node): node is HTMLElement => node instanceof HTMLElement && !exiting.has(node)) .map((node) => [node, node.getBoundingClientRect().top - origin]), ); }, suspend, dispose() { suspend(); disposed = true; }, }; }