@atlaskit/motion
Version:
A set of utilities to apply motion in your application.
38 lines (37 loc) • 1.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useRequestAnimationFrame = void 0;
var _react = require("react");
var _getHookDeps = require("./get-hook-deps");
/**
* Will return request animation frame as a function which will clean itself up.
*/
var useRequestAnimationFrame = exports.useRequestAnimationFrame = function useRequestAnimationFrame() {
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
cleanup: 'unmount'
};
var frames = (0, _react.useRef)([]);
(0, _react.useEffect)(function () {
return function () {
if (frames.current.length) {
frames.current.forEach(function (id) {
return 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
}, (0, _getHookDeps.getHookDeps)(opts));
return (0, _react.useCallback)(function (handler) {
var id = requestAnimationFrame(function (time) {
frames.current = frames.current.filter(function (frameId) {
return frameId !== id;
});
handler(time);
});
frames.current.push(id);
}, []);
};