UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

34 lines (33 loc) 1.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useAfterExitAnimation = useAfterExitAnimation; var _useAnimationsFinished = require("./useAnimationsFinished"); var _useEnhancedEffect = require("./useEnhancedEffect"); var _useEventCallback = require("./useEventCallback"); var _useLatestRef = require("./useLatestRef"); /** * Calls the provided function when the CSS exit animation or transition completes. * Useful for unmounting the component after animating out. */ function useAfterExitAnimation(parameters) { const { open, animatedElementRef, onFinished: onFinishedParam } = parameters; const onFinished = (0, _useEventCallback.useEventCallback)(onFinishedParam); const runOnceAnimationsFinish = (0, _useAnimationsFinished.useAnimationsFinished)(animatedElementRef); const openRef = (0, _useLatestRef.useLatestRef)(open); (0, _useEnhancedEffect.useEnhancedEffect)(() => { function callOnFinished() { if (!openRef.current) { onFinished(); } } if (!open) { runOnceAnimationsFinish(callOnFinished); } }, [open, openRef, runOnceAnimationsFinish, onFinished]); }