@atlaskit/motion
Version:
A set of utilities to apply motion in your application.
36 lines (35 loc) • 995 B
JavaScript
import { useCallback, useEffect, useRef } from 'react';
const getHookDeps = opts => {
switch (opts.cleanup) {
case 'next-effect':
return undefined;
case 'unmount':
default:
return [];
}
};
/**
* Will return request animation frame as a function which will clean itself up.
*/
export const useRequestAnimationFrame = (opts = {
cleanup: 'unmount'
}) => {
const frames = useRef([]);
useEffect(() => {
return () => {
if (frames.current.length) {
frames.current.forEach(id => cancelAnimationFrame(id));
frames.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 => {
const id = requestAnimationFrame(time => {
frames.current = frames.current.filter(frameId => frameId !== id);
handler(time);
});
frames.current.push(id);
}, []);
};