about summary refs log tree commit diff
path: root/src/lib/hooks/useWebBodyScrollLock.ts
blob: 790ddce630f40f4494c6aeabf70b51d4cd835215 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import {useEffect} from 'react'

import {isWeb} from '#/platform/detection'

let refCount = 0

function incrementRefCount() {
  if (refCount === 0) {
    document.body.style.overflow = 'hidden'
  }
  refCount++
}

function decrementRefCount() {
  refCount--
  if (refCount === 0) {
    document.body.style.overflow = ''
  }
}

export function useWebBodyScrollLock(isLockActive: boolean) {
  useEffect(() => {
    if (!isWeb || !isLockActive) {
      return
    }
    incrementRefCount()
    return () => decrementRefCount()
  })
}