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.

31 lines (29 loc) 925 B
'use client'; import * as React from 'react'; import { useEventCallback } from '@base-ui-components/utils/useEventCallback'; import { useLatestRef } from '@base-ui-components/utils/useLatestRef'; import { useAnimationsFinished } from "./useAnimationsFinished.js"; /** * Calls the provided function when the CSS open/close animation or transition completes. */ export function useOpenChangeComplete(parameters) { const { enabled = true, open, ref, onComplete: onCompleteParam } = parameters; const openRef = useLatestRef(open); const onComplete = useEventCallback(onCompleteParam); const runOnceAnimationsFinish = useAnimationsFinished(ref, open); React.useEffect(() => { if (!enabled) { return; } runOnceAnimationsFinish(() => { if (open === openRef.current) { onComplete(); } }); }, [enabled, open, onComplete, runOnceAnimationsFinish, openRef]); }