import { useEffect , useState } from 'react' ; declare global { interface Window { __useScrollLockStyle : string | undefined | null ; __useScrollLockInstances : Set < { } > | undefined | null ; } } let instances = new Set ( ) ; if ( typeof window !== 'undefined' ) { window . __useScrollLockInstances = window . __useScrollLockInstances || new Set < { } > ( ) ; instances = window . __useScrollLockInstances ; } const registerInstance = ( instance ) => { if ( instances . size === 0 ) { setBodyOverflow ( true ) ; } instances . add ( instance ) ; } ; const unregisterInstance = ( instance ) => { instances . delete ( instance ) ; if ( instances . size === 0 ) { setBodyOverflow ( false ) ; } } ; const setBodyOverflow = ( shouldLock ) => { if ( shouldLock ) { document . body . classList . add ( 'no-scroll' ) ; } else { document . body . classList . remove ( 'no-scroll' ) ; } } ; export const useScrollLock = ( shouldLock ) => { const [ elementId ] = useState ( { } ) ; useEffect ( ( ) => { if ( shouldLock ) { registerInstance ( elementId ) ; } return ( ) => unregisterInstance ( elementId ) ; } , [ elementId , shouldLock ] ) ; } ;