UNPKG

@lesnoypudge/utils-react

Version:

lesnoypudge's utils-react

30 lines (29 loc) 985 B
import { useFunction } from "../useFunction/useFunction.js"; import { useLatest } from "../useLatest/useLatest.js"; import { useLayoutEffect } from "react"; const useScrollIntoView = (...[elementRef, options]) => { const enabled = !!(options == null ? void 0 : options.enabled); const lastOptions = useLatest({ behavior: (options == null ? void 0 : options.behavior) ?? "instant", block: (options == null ? void 0 : options.vertical) ?? "center", inline: (options == null ? void 0 : options.horizontal) ?? "center" }); const scrollIntoView = useFunction(() => { const element = elementRef.current; if (!element) return; element.scrollIntoView(lastOptions.current); }); useLayoutEffect(() => { if (!enabled) return; return elementRef.effect(() => { scrollIntoView(); }); }, [elementRef, enabled, scrollIntoView]); return { scrollIntoView }; }; export { useScrollIntoView }; //# sourceMappingURL=useScrollIntoView.js.map