"use client"; import { useEffect } from "react"; /** * A custom hook that locks the body scroll based on the `isOpen` and `hideScrollOnIsOpenFalse` flags. * * @param {boolean} isOpen - Determines if the scroll should be locked (hidden). * @param {boolean} hideScrollOnIsOpenFalse - Determines if the scroll should be reset when `isOpen` is false. */ function useBodyScrollLock(isOpen: boolean, hideScrollOnIsOpenFalse: boolean) { useEffect(() => { if (typeof document === "undefined") return; // To avoid content shifting when the scrollbar appears/disappears const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth; document.body.style.marginRight = isOpen ? `${scrollbarWidth}px` : "unset"; // Set the body overflow style based on the `isOpen` and `hideScrollOnIsOpenFalse` values. document.body.style.overflow = isOpen ? "hidden" : hideScrollOnIsOpenFalse ? "unset" : document.body.style.overflow; // Cleanup function to reset body overflow when the component unmounts or dependencies change. return () => { document.body.style.overflow = "unset"; }; }, [isOpen, hideScrollOnIsOpenFalse]); } export default useBodyScrollLock;