UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

47 lines (46 loc) 1.76 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useHandleLayoutEffect; var _react = require("react"); function useHandleLayoutEffect(_ref) { let { elementRef, stepElementRef } = _ref; 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(() => { var _stepElementRef$curre, _stepElementRef$curre2; (_stepElementRef$curre = stepElementRef.current) === null || _stepElementRef$curre === void 0 ? void 0 : (_stepElementRef$curre2 = _stepElementRef$curre.focus) === null || _stepElementRef$curre2 === void 0 ? void 0 : _stepElementRef$curre2.call(_stepElementRef$curre, { preventScroll: true }); }); }, [action, stepElementRef]); const scrollToTop = (0, _react.useCallback)(() => { action(() => { var _elementRef$current, _elementRef$current$s; (_elementRef$current = elementRef.current) === null || _elementRef$current === void 0 ? void 0 : (_elementRef$current$s = _elementRef$current.scrollIntoView) === null || _elementRef$current$s === void 0 ? void 0 : _elementRef$current$s.call(_elementRef$current); }); }, [action, elementRef]); return { setFocus, scrollToTop, isInteractionRef }; } //# sourceMappingURL=useHandleLayoutEffect.js.map