Skip to content

Commit 2d9298c

Browse files
committed
fix: #352
1 parent 7de93e9 commit 2d9298c

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

packages/vue-final-modal/src/components/CoreModal/CoreModal.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { computed, inject, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
2+
import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
33
import { coreModalProps } from './CoreModalProps'
44
import { useTransition } from './useTransition'
55
import { useToClose } from './useToClose'
@@ -76,15 +76,20 @@ const {
7676
leaveTransition,
7777
} = useTransition(props, {
7878
modelValueLocal,
79+
onEntering() {
80+
nextTick(() => {
81+
disableBodyScroll()
82+
})
83+
},
7984
onEnter() {
8085
focus()
81-
disableBodyScroll()
8286
emitEvent('opened')
8387
resolveToggle('opened')
8488
},
8589
onLeave() {
8690
deleteFromOpenedModals(getModalInstance())
8791
resetZIndex()
92+
enableBodyScroll()
8893
emitEvent('closed')
8994
resolveToggle('closed')
9095
},
@@ -148,7 +153,6 @@ async function open() {
148153
149154
function close() {
150155
emitEvent('beforeClose')
151-
enableBodyScroll()
152156
deleteFromOpenedModalOverlays(modalInstance)
153157
openLastOverlay()
154158
blur()

packages/vue-final-modal/src/components/CoreModal/useBodyScrollLock.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import { onBeforeUnmount, watch } from 'vue'
33
import type CoreModal from './CoreModal.vue'
44

55
type BodyScrollOptions = {
6-
reserveScrollBarGap: boolean
7-
allowTouchMove: (el?: null | HTMLElement) => boolean
6+
reserveScrollBarGap?: boolean
7+
allowTouchMove?: (el?: null | HTMLElement) => boolean
8+
}
9+
10+
type Lock = {
11+
targetElement: HTMLElement
12+
options?: BodyScrollOptions
813
}
914

1015
// stolen from body-scroll-lock
@@ -33,7 +38,7 @@ const isIosDevice
3338
&& (/iP(ad|hone|od)/.test(window.navigator.platform)
3439
|| (window.navigator.platform === 'MacIntel' && window.navigator.maxTouchPoints > 1))
3540

36-
let locks: any[] = []
41+
let locks: Lock[] = []
3742
let documentListenerAdded = false
3843
let clientY = 0
3944
let initialClientY = -1
@@ -232,6 +237,13 @@ export function useLockScroll(props: InstanceType<typeof CoreModal>['$props'], o
232237
lockScrollEl: Ref<undefined | HTMLElement>
233238
}) {
234239
const { lockScrollEl } = options
240+
241+
let _lockScrollEl: HTMLElement
242+
watch(lockScrollEl, (val) => {
243+
if (val)
244+
_lockScrollEl = val
245+
}, { immediate: true })
246+
235247
watch(() => props.lockScroll, (val) => {
236248
val ? _disableBodyScroll() : _enableBodyScroll()
237249
})
@@ -241,12 +253,12 @@ export function useLockScroll(props: InstanceType<typeof CoreModal>['$props'], o
241253
})
242254

243255
function _enableBodyScroll() {
244-
lockScrollEl.value && enableBodyScroll(lockScrollEl.value)
256+
_lockScrollEl && enableBodyScroll(_lockScrollEl)
245257
}
246258

247259
function _disableBodyScroll() {
248-
props.lockScroll && lockScrollEl.value
249-
&& disableBodyScroll(lockScrollEl.value, {
260+
props.lockScroll && _lockScrollEl
261+
&& disableBodyScroll(_lockScrollEl, {
250262
reserveScrollBarGap: true,
251263
allowTouchMove: (el) => {
252264
while (el && el !== document.body) {

0 commit comments

Comments
 (0)