UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

39 lines (38 loc) 1.03 kB
"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(() => { stepElementRef.current?.focus?.({ preventScroll: true }); }); }, [action, stepElementRef]); const scrollToTop = useCallback(() => { action(() => { elementRef.current?.scrollIntoView?.(); }); }, [action, elementRef]); return { setFocus, scrollToTop, isInteractionRef }; } //# sourceMappingURL=useHandleLayoutEffect.js.map