@atlaskit/motion
Version:
A set of utilities to apply motion in your application.
39 lines (36 loc) • 1.05 kB
JavaScript
import { useCallback, useEffect, useRef } from 'react';
// Handle both browser and Node.js environments
const getHookDeps = opts => {
switch (opts.cleanup) {
case 'next-effect':
return undefined;
case 'unmount':
default:
return [];
}
};
/**
* Will return set timeout as a function which will clean itself up.
*/
export const useSetTimeout = (opts = {
cleanup: 'unmount'
}) => {
const timeouts = useRef([]);
useEffect(() => {
return () => {
if (timeouts.current.length) {
timeouts.current.forEach(id => clearTimeout(id));
timeouts.current = [];
}
};
// We dynamically set this so we either clean up on the next effect - or on unmount.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, getHookDeps(opts));
return useCallback((handler, timeout, ...args) => {
const id = setTimeout(() => {
timeouts.current = timeouts.current.filter(timeoutId => timeoutId !== id);
handler();
}, timeout, ...args);
timeouts.current.push(id);
}, []);
};