@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
41 lines (40 loc) • 1.56 kB
JavaScript
"use client";
import { useCallback, useEffect, useRef } from 'react';
export default function useHandleLayoutEffect({
elementRef,
stepElementRef
}) {
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 || (_stepElementRef$curre2 = _stepElementRef$curre.focus) === null || _stepElementRef$curre2 === 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 || (_elementRef$current$s = _elementRef$current.scrollIntoView) === null || _elementRef$current$s === void 0 || _elementRef$current$s.call(_elementRef$current);
});
}, [action, elementRef]);
return {
setFocus,
scrollToTop,
isInteractionRef
};
}
//# sourceMappingURL=useHandleLayoutEffect.js.map