Skip to content

Commit ee08c9e

Browse files
feat: useBodyScrollLock
1 parent fb2ec29 commit ee08c9e

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

packages/vue-primitives/src/dialog/DialogOverlay.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import type { DialogOverlayProps } from './DialogOverlay.ts'
3-
import { shallowRef } from 'vue'
4-
import { useForwardElement } from '../hooks/index.ts'
3+
import { onWatcherCleanup, shallowRef, watchEffect } from 'vue'
4+
import { useBodyScrollLock, useForwardElement } from '../hooks/index.ts'
55
import { usePresence } from '../presence/index.ts'
66
import { Primitive } from '../primitive/index.ts'
77
import { useDialogContext } from './DialogRoot.ts'
@@ -19,6 +19,12 @@ const $el = shallowRef<HTMLElement>()
1919
const forwardElement = useForwardElement($el)
2020
2121
const isPresent = usePresence($el, () => props.forceMount || context.open.value)
22+
23+
watchEffect(() => {
24+
if (isPresent.value) {
25+
onWatcherCleanup(useBodyScrollLock())
26+
}
27+
})
2228
</script>
2329

2430
<template>

packages/vue-primitives/src/hooks/useBodyScrollLock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function useBodyScrollLock(): () => void {
5353
}
5454
}
5555

56-
function onlock() {
56+
const unlock = () => {
5757
bodyStyle.overflow = initialOverflow ?? ''
5858
body.removeAttribute('data-scroll-lock')
5959

@@ -70,7 +70,7 @@ export function useBodyScrollLock(): () => void {
7070
stopTouchMoveListener?.()
7171
}
7272

73-
return onlock
73+
return unlock
7474
}
7575

7676
function preventDefault(event: TouchEvent): boolean {

packages/vue-primitives/src/menu/MenuContentImpl.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { onBeforeUnmount, shallowRef } from 'vue'
44
import { useDismissableLayer } from '../dismissable-layer/index.ts'
55
import { useFocusGuards } from '../focus-guards/index.ts'
66
import { useFocusScope } from '../focus-scope/index.ts'
7-
import { useForwardElement, useRef } from '../hooks/index.ts'
7+
import { useBodyScrollLock, useForwardElement, useRef } from '../hooks/index.ts'
88
import { PopperContent, usePopperContext } from '../popper/index.ts'
99
import { useRovingFocusGroupRoot } from '../roving-focus/index.ts'
1010
import { focusFirst } from '../utils/focusFirst.ts'
@@ -40,11 +40,7 @@ let pointerGraceIntentRef: GraceIntent | undefined
4040
let pointerDirRef: Side = 'right'
4141
let lastPointerXRef = 0
4242
43-
// TODO: ScrollLock
44-
// const ScrollLockWrapper = disableOutsideScroll ? RemoveScroll : React.Fragment
45-
// const scrollLockWrapperProps = disableOutsideScroll
46-
// ? { as: Slot, allowPinchZoom: true }
47-
// : undefined
43+
const unlock = props.disableOutsideScroll ? useBodyScrollLock() : undefined
4844
4945
function handleTypeaheadSearch(key: string) {
5046
const search = searchRef.current + key
@@ -76,6 +72,7 @@ function handleTypeaheadSearch(key: string) {
7672
7773
onBeforeUnmount(() => {
7874
window.clearTimeout(timerRef)
75+
unlock?.()
7976
})
8077
8178
// Make sure the whole tree has focus guards as our `MenuContent` may be

packages/vue-primitives/src/popover/PopoverContentModal.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { onBeforeUnmount } from 'vue'
66
import { useDismissableLayer } from '../dismissable-layer/index.ts'
77
import { useFocusGuards } from '../focus-guards/index.ts'
88
import { useFocusScope } from '../focus-scope/index.ts'
9+
import { useBodyScrollLock } from '../hooks/useBodyScrollLock.ts'
910
import { PopperContent, usePopperContext } from '../popper/index.ts'
1011
import { composeEventHandlers } from '../utils/vue.ts'
1112
import { usePopoverContext } from './PopoverRoot.ts'
@@ -50,7 +51,10 @@ const onFocusOutside = composeEventHandlers<FocusOutsideEvent>((event) => {
5051
emit('focusOutside', event)
5152
}, event => event.preventDefault(), { checkForDefaultPrevented: false })
5253
54+
const unlock = useBodyScrollLock()
55+
5356
onBeforeUnmount(() => {
57+
unlock()
5458
if (popperContext.content.value)
5559
hideOthers(popperContext.content.value)
5660
})

0 commit comments

Comments
 (0)