@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) • 931 B
JavaScript
'use client';
import * as React from 'react';
import { useStableCallback } from '@base-ui-components/utils/useStableCallback';
import { useValueAsRef } from '@base-ui-components/utils/useValueAsRef';
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 = useValueAsRef(open);
const onComplete = useStableCallback(onCompleteParam);
const runOnceAnimationsFinish = useAnimationsFinished(ref, open);
React.useEffect(() => {
if (!enabled) {
return;
}
runOnceAnimationsFinish(() => {
if (open === openRef.current) {
onComplete();
}
});
}, [enabled, open, onComplete, runOnceAnimationsFinish, openRef]);
}