UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

39 lines (33 loc) 1.11 kB
import { useEffect } from "react"; import useTheme from "../useTheme"; import useIsMounted from "../useIsMounted"; import { disableBodyScroll, enableBodyScroll } from "./lock-scrolling"; const useLockScrolling = (ref, lock = true, dependencies = []) => { const { lockScrolling: themeLockScrolling = true, lockScrollingBarGap } = useTheme(); const isMounted = useIsMounted(); useEffect(() => { const el = ref.current; if (el) { if (lock && themeLockScrolling) { disableBodyScroll(el, { reserveScrollBarGap: lockScrollingBarGap }); } if (!lock || !themeLockScrolling) { enableBodyScroll(el); } } return () => { // disabling this because we're interested in precisely that changed value // eslint-disable-next-line react-hooks/exhaustive-deps const didRefChange = el !== ref.current; if (el && (!isMounted() || didRefChange)) { enableBodyScroll(el); } }; }, [ref, lock, themeLockScrolling, lockScrollingBarGap, isMounted, ...dependencies]); }; export default useLockScrolling;