8000 fix(useScrollLock): initialOverflow is not working (#3798) · vueuse/vueuse@74e86b5 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 74e86b5

Browse files
authored
fix(useScrollLock): initialOverflow is not working (#3798)
1 parent bd946aa commit 74e86b5

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { defineComponent, h } from 'vue-demi'
3+
import { mount } from '../../.test'
4+
import { useScrollLock } from '.'
5+
6+
describe('useScrollLock', () => {
7+
let targetEl: HTMLElement
8+
9+
beforeEach(() => {
10+
targetEl = document.createElement('div')
11+
})
12+
13+
it('should lock the scroll', () => {
14+
const isLock = useScrollLock(targetEl)
15+
16+
isLock.value = true
17+
expect(targetEl.style.overflow).toBe('hidden')
18+
19+
isLock.value = false
20+
expect(targetEl.style.overflow).toBe('')
21+
})
22+
23+
it('should cache the initial overflow setting', () => {
24+
targetEl.style.overflow = 'auto'
25+
26+
const isLock = useScrollLock(targetEl)
27+
28+
isLock.value = true
29+
expect(targetEl.style.overflow).toBe('hidden')
30+
31+
isLock.value = false
32+
expect(targetEl.style.overflow).toBe('auto')
33+
})
34+
35+
it('automatically unlocks on component unmount', async () => {
36+
const vm = mount(defineComponent({
37+
setup() {
38+
const isLock = useScrollLock(targetEl)
39+
40+
return { isLock }
41+
},
42+
render() {
43+
return h('div')
44+
},
45+
}))
46+
47+
vm.isLock = true
48+
expect(targetEl.style.overflow).toBe('hidden')
49+
50+
vm.unmount()
51+
expect(targetEl.style.overflow).toBe('')
52+
})
53+
54+
it('handles touchmove event on IOS devices', () => {
55+
vi.mock('@vueuse/shared', async () => {
56+
const actual = await vi.importActual('@vueuse/shared')
57+
return {
58+
...actual,
59+
isIOS: true,
60+
}
61+
})
62+
63+
const addEventListener = vi.spyOn(targetEl, 'addEventListener')
64+
const removeEventListener = vi.spyOn(targetEl, 'removeEventListener')
65+
const isLock = useScrollLock(targetEl)
66+
67+
expect(addEventListener).toBeCalledTimes(0)
68+
69+
isLock.value = true
70+
expect(addEventListener).toBeCalledTimes(1)
71+
expect(removeEventListener).toBeCalledTimes(0)
72+
73+
isLock.value = false
74+
expect(removeEventListener).toBeCalledTimes(1)
75+
})
76+
77+
it('multiple instances point at the same element, will share the same initialOverflow', () => {
78+
const isLock1 = useScrollLock(targetEl)
79+
const isLock2 = useScrollLock(targetEl)
80+
81+
isLock1.value = true
82+
isLock2.value = true
83+
expect(targetEl.style.overflow).toBe('hidden')
84+
85+
isLock2.value = false
86+
expect(targetEl.style.overflow).toBe('')
87+
})
88+
})

packages/core/useScrollLock/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ export function useScrollLock(
5858
) {
5959
const isLocked = ref(initialState)
6060
let stopTouchMoveListener: Fn | null = null
61-
let initialOverflow: CSSStyleDeclaration['overflow']
6261

6362
watch(toRef(element), (el) => {
6463
const target = resolveElement(toValue(el))
6564
if (target) {
6665
const ele = target as HTMLElement
6766
if (!elInitialOverflow.get(ele))
68-
elInitialOverflow.set(ele, initialOverflow)
67+
elInitialOverflow.set(ele, ele.style.overflow)
68+
6969
if (isLocked.value)
7070
ele.style.overflow = 'hidden'
7171
}

0 commit comments

Comments
 (0)
0