@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
44 lines (43 loc) • 1.17 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useHandleLayoutEffect;
var _react = require("react");
function useHandleLayoutEffect({
elementRef,
stepElementRef
}) {
const isInteractionRef = (0, _react.useRef)(false);
(0, _react.useEffect)(() => {
const delay = process.env.NODE_ENV === 'test' ? 8 : 100;
const timeout = setTimeout(() => {
isInteractionRef.current = true;
}, delay);
return () => clearTimeout(timeout);
});
const action = (0, _react.useCallback)(fn => {
window.requestAnimationFrame(() => window.requestAnimationFrame(() => {
isInteractionRef.current && fn();
}));
}, []);
const setFocus = (0, _react.useCallback)(() => {
action(() => {
stepElementRef.current?.focus?.({
preventScroll: true
});
});
}, [action, stepElementRef]);
const scrollToTop = (0, _react.useCallback)(() => {
action(() => {
elementRef.current?.scrollIntoView?.();
});
}, [action, elementRef]);
return {
setFocus,
scrollToTop,
isInteractionRef
};
}
//# sourceMappingURL=useHandleLayoutEffect.js.map