Skip to content

Commit b911f55

Browse files
committed
fix lock body scroll paddingRight
1 parent e0ec17c commit b911f55

File tree

1 file changed

+6
-5
lines changed
  • packages/use-lock-body-scroll/src

1 file changed

+6
-5
lines changed

packages/use-lock-body-scroll/src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { getClosestScrollable, isIosDevice } from './utils'
33

44
export const useLockBodyScroll = function useLockBody() {
55
const [locked, setLocked] = React.useState(false)
6-
const initialBodyStyles = React.useRef<React.CSSProperties>({})
6+
const originalBodyStyles = React.useRef<React.CSSProperties>({})
77

88
const unlock = React.useCallback(() => {
99
const { body } = document
1010
if (body.style.overflow !== 'hidden') return // already unlocked
11-
Object.assign(body.style, initialBodyStyles.current)
11+
Object.assign(body.style, originalBodyStyles.current)
1212
if (isIosDevice()) {
1313
document.removeEventListener('touchmove', preventTouchBodyScroll)
1414
}
@@ -17,14 +17,15 @@ export const useLockBodyScroll = function useLockBody() {
1717

1818
const lock = React.useCallback(() => {
1919
const { body } = document
20-
if (body.style.overflow === 'hidden') return // already locked
21-
initialBodyStyles.current = {
20+
const computedBodyStyles = getComputedStyle(body)
21+
if (computedBodyStyles.overflow === 'hidden') return // already locked
22+
originalBodyStyles.current = {
2223
overflow: body.style.overflow,
2324
paddingRight: body.style.paddingRight,
2425
}
2526
Object.assign(body.style, {
2627
overflow: 'hidden',
27-
paddingRight: `${window.innerWidth - body.clientWidth}px`,
28+
paddingRight: `calc(${window.innerWidth - body.clientWidth}px + ${computedBodyStyles.paddingRight})`,
2829
})
2930
if (isIosDevice()) {
3031
document.addEventListener('touchmove', preventTouchBodyScroll, {

0 commit comments

Comments
 (0)