UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

35 lines 1.67 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useOpenChangeAnimationComplete = useOpenChangeAnimationComplete; const react_1 = require("react"); const useAnimationsFinished_1 = require("./useAnimationsFinished"); const useEventCallback_1 = require("./useEventCallback"); const useValueAsRef_1 = require("./useValueAsRef"); /** * 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. */ function useOpenChangeAnimationComplete(parameters) { const { enabled = true, open, ref = null, onComplete: onCompleteParam, } = parameters; const openRef = (0, useValueAsRef_1.useValueAsRef)(open); const onComplete = (0, useEventCallback_1.useEventCallback)(onCompleteParam); const runOnceAnimationsFinish = (0, useAnimationsFinished_1.useAnimationsFinished)(ref, open); (0, react_1.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