Skip to content

Commit 8acdb47

Browse files
author
Zhaolin Liang
authored
fix(useScrollLock): support using window or document (#3319)
1 parent c1b296c commit 8acdb47

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

packages/core/useScrollLock/index.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ function preventDefault(rawEvent: TouchEvent): boolean {
4343
return false
4444
}
4545

46+
function getTargetElement(
47+
element: MaybeRefOrGetter<HTMLElement | SVGElement | Window | Document | null | undefined>,
48+
): HTMLElement | SVGElement | null | undefined {
49+
const el = toValue(element)
50+
if (el instanceof Window)
51+
return window.document.documentElement
52+
53+
if (el instanceof Document)
54+
return document.documentElement
55+
56+
return el
57+
}
58+
4659
/**
4760
* Lock scrolling of the element.
4861
*
@@ -58,8 +71,9 @@ export function useScrollLock(
5871
let initialOverflow: CSSStyleDeclaration['overflow']
5972

6073
watch(toRef(element), (el) => {
61-
if (el) {
62-
const ele = el as HTMLElement
74+
const target = getTargetElement(el)
75+
if (target) {
76+
const ele = target as HTMLElement
6377
initialOverflow = ele.style.overflow
6478
if (isLocked.value)
6579
ele.style.overflow = 'hidden'
@@ -69,7 +83,7 @@ export function useScrollLock(
6983
})
7084

7185
const lock = () => {
72-
const ele = (toValue(element) as HTMLElement)
86+
const ele = getTargetElement(element)
7387
if (!ele || isLocked.value)
7488
return
7589
if (isIOS) {
@@ -85,7 +99,7 @@ export function useScrollLock(
8599
}
86100

87101
const unlock = () => {
88-
const ele = (toValue(element) as HTMLElement)
102+
const ele = getTargetElement(element)
89103
if (!ele || !isLocked.value)
90104
return
91105
isIOS && stopTouchMoveListener?.()

0 commit comments

Comments
 (0)