@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
28 lines (27 loc) • 1.82 kB
TypeScript
import React from "react";
/**
* Returns a stable function that, when invoked, waits for all current CSS/Web Animations
* on a target element (and its subtree) to finish before executing a callback.
*
* Why:
* - Coordinate logic (unmount, focus restore, measuring) after exit / enter animations.
* - Avoid `animationend` event bookkeeping across multiple animations / nested elements.
* - Batch detection using `requestAnimationFrame` so freshly-applied animations are discoverable.
*
* Mechanics:
* 1. Resolves the concrete `HTMLElement` (direct element or from ref) – early no-op if missing.
* 2. If `getAnimations` is unsupported or animations are globally disabled (`AKSEL_ANIMATIONS_DISABLED`),
* runs the callback immediately.
* 3. Schedules a frame so style/animation changes applied this render are committed.
* 4. Optionally schedules an additional frame (`waitForNextTick=true`) to catch animations that
* start only after layout (e.g. when an `open` class triggers the animation).
* 5. Captures all current animations, waits on their `.finished` promises (using `Promise.allSettled`
* so rejections don't block), then `flushSync` executes the callback (ensures React state updates
* inside run before the browser paints the next frame).
* 6. If an `AbortSignal` aborts while waiting, it silently cancels execution.
*
* @param elementOrRef HTMLElement or ref to observe.
* @param waitForNextTick If true, waits an extra frame to ensure enter animations are detectable.
* @returns Stable function (identity preserved) accepting (fn, abortSignal?).
*/
export declare function useAnimationsFinished(elementOrRef: React.RefObject<HTMLElement | null> | HTMLElement | null, waitForNextTick?: boolean): (fnToExecute: () => void, signal?: AbortSignal | null) => void;