import type { RefObject } from "react"; import { useLayoutEffect, useRef } from "react"; import { token } from "../styled-system/tokens"; import type { Rect } from "./rect"; import { useHandoffs } from "./SettleHandoffs"; import type { Offset, Point } from "./settle"; import { drawnAt, offsetFrom, partway, uprightAlong } from "./settle"; /** Reads and plays an element's settle. */ export type Settler = { /** How far the element is drawn from its box. */ offset: (element: Element) => Offset; /** * Stops the element's settle, then eases it from `offset` to its box, * keeping each `upright` part at its own size and the element at depth * `over`, if any. Only stops for `undefined`. Calls `onEnded` when the * settle runs to its end, not when it is stopped. */ play: ( element: Element, offset: Offset | undefined, upright: readonly Element[], over: number | undefined, onEnded: () => void, ) => void; }; /** * Marks a part of a settling element, such as a title, kept at its own size * while the element scales. It scales back about its `transform-origin`, * which should be the edge of the element it keeps to. */ export const UPRIGHT = "data-upright"; /** * The box an element was given last, the origin it scaled about, and the row * it was a piece of. */ type Placed = { origin: Point; rect: Rect; row: Rect | undefined }; /** `T` with every field possibly missing, as for an element off screen. */ type Unknown = { [K in keyof T]: T[K] | undefined }; type Options = { element: RefObject; /** * Called with the box the element is drawn at when a settle starts, and * with `undefined` when it ends or stops. */ onSettling?: ((from: Rect | undefined) => void) | undefined; /** * The element's `transform-origin`, from its box's corner. `undefined`, like * `rect`, when the element is off screen. */ origin: Point | undefined; /** * The depth the element holds while it eases into a new box, over the parts * it passes, such as a window moving to another group over the strips on * its way. `undefined` keeps its own. */ over?: number | undefined; /** The element's box, or `undefined` when it is off screen. */ rect: Rect | undefined; /** * The row the element is a piece of, such as a tab's strip, or `undefined`. * An element that leaves its row eases from the whole row, as the rest of * its window does. */ row?: Rect | undefined; settler?: Settler | undefined; /** * Keys of the part the element draws. An element new in a commit that * removed one sharing a key eases from where that one was drawn, as when a * tab switch hands a strip to the newly open tab. See `SettleHandoffs`. */ shared?: readonly string[] | undefined; /** * Whether the box takes its new place without easing: it follows the * pointer, which easing would make it trail, or it plays a motion, whose * travel `scale` would scale. */ still: boolean; /** * Keys the element eases from, like `shared`, but leaves nothing under when * it goes. */ takesOver?: readonly string[] | undefined; }; /** * Eases an element from its old box to a new one with `translate` and * `scale`, so its box changes once. * * Changing the box every frame costs a layout every frame, and an ``'s * box is its client's size, so its client would draw at every size in * between. The engine holds the page's frame until a resized client draws, so * the whole desktop would run at the slowest client's pace. `translate` and * `scale` run on the compositor thread. * * Every part of a window eases this way, on one timing, so the parts keep * together: each edge moves linearly in the eased progress, whatever the * part's origin. * * Runs before paint, so no frame shows the new box without the offset. */ export const useSettling = ({ element, onSettling = IGNORED, origin, over, rect, row, settler = WEB_ANIMATIONS, shared = NO_KEYS, still, takesOver = NO_KEYS, }: Options): void => { const last = useRef(undefined); const { x, y, width, height } = rect ?? {}; const { x: originX, y: originY } = origin ?? {}; const { x: rowX, y: rowY, width: rowWidth, height: rowHeight } = row ?? {}; const handoffs = useHandoffs(); const keys = shared.join("\n"); const taken = [...shared, ...takesOver].join("\n"); useLayoutEffect(() => { const was = last.current; const shown = element.current; const now = placedOf( { height, width, x, y }, { x: originX, y: originY }, rowOf({ height: rowHeight, width: rowWidth, x: rowX, y: rowY }), ); last.current = now; if (shown !== null && now !== undefined) { if (was === undefined) { const from = takenOver(handoffs, taken); if (from !== undefined && !still) { settle( settler, shown, now, offsetFrom(from, now.rect, now.origin), over, onSettling, ); } } else if (!sameBox(was.rect, now.rect)) { settle( settler, shown, now, still ? undefined : offsetTo(shown, leftRow(was, now), now, settler), over, onSettling, ); } } }, [ element, handoffs, onSettling, height, originX, originY, over, rowHeight, rowWidth, rowX, rowY, settler, still, taken, width, x, y, ]); // Leaves where it is drawn for an element taking over in this commit. useLayoutEffect( () => () => { const shown = element.current; const placed = last.current; if (handoffs !== undefined && shown !== null && placed !== undefined) { const drawn = drawnAt( placed.rect, placed.origin, settler.offset(shown), ); for (const key of keysIn(keys)) { handoffs.set(key, drawn); } } }, [element, handoffs, keys, settler], ); }; const NO_KEYS: readonly string[] = []; const IGNORED = () => undefined; /** * Plays the element's settle from `offset` to its box `now`, at depth `over` * if any, or stops it for `undefined`, and tells `onSettling` the box it * starts from. */ const settle = ( settler: Settler, element: Element, now: Placed, offset: Offset | undefined, over: number | undefined, onSettling: (from: Rect | undefined) => void, ): void => { onSettling( offset === undefined ? undefined : drawnAt(now.rect, now.origin, offset), ); settler.play(element, offset, uprightIn(element), over, () => { onSettling(undefined); }); }; /** The parts of `element` kept upright; see {@link UPRIGHT}. */ const uprightIn = (element: Element): readonly Element[] => [ ...element.querySelectorAll(`[${UPRIGHT}]`), ]; /** The keys `useSettling` joined, so they can be its effects' dependency. */ const keysIn = (keys: string): readonly string[] => keys === "" ? [] : keys.split("\n"); /** Where a part that shared one of `keys` was drawn, if one just went. */ const takenOver = ( handoffs: ReadonlyMap | undefined, keys: string, ): Rect | undefined => keysIn(keys) .map((key) => handoffs?.get(key)) .find((drawn) => drawn !== undefined); /** The row, or `undefined` for none. */ const rowOf = ({ height, width, x, y }: Unknown): Rect | undefined => x === undefined || y === undefined || width === undefined || height === undefined ? undefined : { height, width, x, y }; /** * Where the element was placed, as it eases to `now`: its whole row, if it * just left it. */ const leftRow = (was: Placed, now: Placed): Placed => was.row === undefined || now.row !== undefined ? was : { ...was, rect: was.row }; /** The box and origin, or `undefined` for an element off screen. */ const placedOf = ( { height, width, x, y }: Unknown, origin: Unknown, row: Rect | undefined, ): Placed | undefined => { if ( x === undefined || y === undefined || width === undefined || height === undefined || origin.x === undefined || origin.y === undefined ) { return undefined; } else { return { origin: { x: origin.x, y: origin.y }, rect: { height, width, x, y }, row, }; } }; /** The offset that draws `now` where `element`, placed at `was`, is drawn. */ const offsetTo = ( element: Element, was: Placed, now: Placed, settler: Settler, ): Offset => offsetFrom( drawnAt(was.rect, was.origin, settler.offset(element)), now.rect, now.origin, ); const sameBox = (one: Rect, other: Rect): boolean => one.x === other.x && one.y === other.y && one.width === other.width && one.height === other.height; /** No offset: the element is drawn at its box. */ const NONE: Offset = { scaleX: 1, scaleY: 1, x: 0, y: 0 }; /** * Each element's settle, the offset it started from, and the animations of * its upright parts and of the depth it holds. */ const settles = new WeakMap< Element, { animation: Animation; from: Offset; held: Animation | undefined; upright: readonly Animation[]; } >(); /** * The browser's settle: a Web Animation, which leaves the element's CSS * `animation` to its motion keyframes. * * Every part of a window settles on one timing, so the parts move together. */ const WEB_ANIMATIONS: Settler = { // `progress` is eased, and `scale` and `translate` interpolate linearly, so // this is where the settle draws the element. `null` outside the settle's // run, when it draws nothing. offset: (element) => { const settle = settles.get(element); const progress = settle?.animation.effect?.getComputedTiming().progress; return settle === undefined || progress === null || progress === undefined ? NONE : partway(settle.from, progress); }, play: (element, offset, upright, over, onEnded) => { const was = settles.get(element); was?.animation.cancel(); was?.held?.cancel(); for (const part of was?.upright ?? []) { part.cancel(); } settles.delete(element); if (offset !== undefined) { const timing = settleTiming(); const animation = settled(element, timing, [ { scale: `${offset.scaleX.toString()} ${offset.scaleY.toString()}`, translate: `${offset.x.toString()}px ${offset.y.toString()}px`, }, { scale: "1", translate: "0px" }, ]); // Keyframe offsets are eased progress, as the element's are, so each // part keeps pace with it. const steps = uprightAlong(offset).map(({ at, scaleX, scaleY }) => ({ offset: at, scale: `${scaleX.toString()} ${scaleY.toString()}`, })); animation.finished.then( () => { onEnded(); }, () => { // Stopped: the next settle took over, and tells its own end. }, ); settles.set(element, { animation, from: offset, // A second animation, so the compositor thread still runs the move: // it cannot run `z-index`. held: over === undefined ? undefined : settled(element, timing, [ { zIndex: over.toString() }, { zIndex: over.toString() }, ]), upright: upright.map((part) => settled(part, timing, steps)), }); } }, }; /** The shortest duration under reduced motion, as the preset gives others. */ const settleTiming = (): KeyframeAnimationOptions => ({ duration: millisecondsOf( document.documentElement.hasAttribute("data-reduced-motion") ? token("durations.fastest") : token("durations.fast"), ), easing: token("easings.out"), }); /** Plays `keyframes` on `element` as part of a settle. */ const settled = ( element: Element, timing: KeyframeAnimationOptions, keyframes: Keyframe[], ): Animation => { const animation = element.animate(keyframes, timing); // A canceled settle rejects `finished`. The next settle replaces it, so // there is nothing to handle. animation.finished.catch(() => { /* no-op */ }); return animation; }; /** A duration token's value, such as `150ms`, in milliseconds. */ const millisecondsOf = (duration: string): number => { const milliseconds = /^(\d+)ms$/.exec(duration)?.[1]; if (milliseconds === undefined) { throw new Error(`shell: cannot read the duration "${duration}"`); } else { return Number(milliseconds); } };