UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

32 lines 1.48 kB
"use client"; import { useEffect } from "react"; import { useEventCallback } from "../../../util/hooks/useEventCallback.js"; import { useLatestRef } from "../../../util/hooks/useLatestRef.js"; import { useAnimationsFinished } from "./useAnimationsFinished.js"; /** * Waits for the element's current Web Animations / CSS transitions to finish after an * `open` state change, then invokes `onComplete`. Guards against race conditions by * comparing the `open` value captured at scheduling time with the latest `open` via ref; * if they differ (state flipped again mid‑animation) the callback is skipped. */ export function useOpenChangeAnimationComplete(parameters) { const { enabled = true, open, ref = null, onComplete: onCompleteParam, } = parameters; const openRef = useLatestRef(open); const onComplete = useEventCallback(onCompleteParam); const runOnceAnimationsFinish = useAnimationsFinished(ref, open); useEffect(() => { if (!enabled) { return; } /* * Schedule completion once the *current* set of animations settle. If during * that wait `open` toggles again, skip to avoid firing for an outdated cycle. */ runOnceAnimationsFinish(() => { if (open === openRef.current) { onComplete(); } }); }, [enabled, open, onComplete, runOnceAnimationsFinish, openRef]); } //# sourceMappingURL=useOpenChangeAnimationComplete.js.map