# All files / src/_hooks ScrollLock.ts

100% Statements 15/15
80% Branches 4/5
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    2512x
    12x

    12x
    51x
    19x

    19x
    5x
    5x

    5x
    5x

    5x
    5x
    5x
    5x

    5x

     import React from 'react'
    import { isIE11 } from '../_utils/isIE11'

    export function useScrollLock(shouldScrollLock: boolean) {
      React.useEffect(() => {
        const IE11 = isIE11()

        if (!IE11 && shouldScrollLock) {
          const bodyPosition = document.body.style.position
          const bodyTop = document.body.style.top

          document.body.style.top = `-${window.scrollY}px`
          document.body.style.position = 'fixed'

          return () => {
            const scrollY = document.body.style.top
            document.body.style.position = bodyPosition
            document.body.style.top = bodyTop

            window.scrollTo(0, parseInt(scrollY || '0') * -1)
          }
        }
      }, [shouldScrollLock])
    }