@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
57 lines (54 loc) • 2.08 kB
JavaScript
import { useState, useRef, useEffect } from 'react';
import { useReducedMotion, useDidUpdate } from '@mantine/hooks';
import { useMantineTheme } from '@mantine/styles';
function useTransition({
duration,
exitDuration,
timingFunction,
mounted,
onEnter,
onExit,
onEntered,
onExited
}) {
const theme = useMantineTheme();
const shouldReduceMotion = useReducedMotion();
const reduceMotion = theme.respectReducedMotion ? shouldReduceMotion : false;
const [transitionDuration, setTransitionDuration] = useState(reduceMotion ? 0 : duration);
const [transitionStatus, setStatus] = useState(mounted ? "entered" : "exited");
const timeoutRef = useRef(-1);
const handleStateChange = (shouldMount) => {
const preHandler = shouldMount ? onEnter : onExit;
const handler = shouldMount ? onEntered : onExited;
setStatus(shouldMount ? "pre-entering" : "pre-exiting");
window.clearTimeout(timeoutRef.current);
const newTransitionDuration = reduceMotion ? 0 : shouldMount ? duration : exitDuration;
setTransitionDuration(newTransitionDuration);
if (newTransitionDuration === 0) {
typeof preHandler === "function" && preHandler();
typeof handler === "function" && handler();
setStatus(shouldMount ? "entered" : "exited");
} else {
const preStateTimeout = window.setTimeout(() => {
typeof preHandler === "function" && preHandler();
setStatus(shouldMount ? "entering" : "exiting");
}, 10);
timeoutRef.current = window.setTimeout(() => {
window.clearTimeout(preStateTimeout);
typeof handler === "function" && handler();
setStatus(shouldMount ? "entered" : "exited");
}, newTransitionDuration);
}
};
useDidUpdate(() => {
handleStateChange(mounted);
}, [mounted]);
useEffect(() => () => window.clearTimeout(timeoutRef.current), []);
return {
transitionDuration,
transitionStatus,
transitionTimingFunction: timingFunction || theme.transitionTimingFunction
};
}
export { useTransition };
//# sourceMappingURL=use-transition.js.map