@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
42 lines (41 loc) • 1.62 kB
JavaScript
"use client";
import { useCallback, useEffect, useRef } from 'react';
export default function useHandleLayoutEffect(_ref) {
let {
elementRef,
stepElementRef
} = _ref;
const isInteractionRef = useRef(false);
useEffect(() => {
const delay = process.env.NODE_ENV === 'test' ? 8 : 100;
const timeout = setTimeout(() => {
isInteractionRef.current = true;
}, delay);
return () => clearTimeout(timeout);
});
const action = useCallback(fn => {
window.requestAnimationFrame(() => window.requestAnimationFrame(() => {
isInteractionRef.current && fn();
}));
}, []);
const setFocus = 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 = 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