Skip to content

Commit a033efc

Browse files
OrbisKantfu
andauthored
feat(useWindowScroll): use useScroll under the hood (#4424)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 0cc45a8 commit a033efc

File tree

4 files changed

+84
-48
lines changed

4 files changed

+84
-48
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { reactive } from 'vue'
3+
import { useScroll } from '.'
4+
5+
describe('useScroll', () => {
6+
it('should be defined', () => {
7+
expect(useScroll).toBeDefined()
8+
})
9+
10+
it('should have default x and y', async () => {
11+
const { x, y } = useScroll(window)
12+
expect(x.value).toBe(0)
13+
expect(y.value).toBe(0)
14+
})
15+
16+
it('should have right default values', () => {
17+
const values = reactive(useScroll(window))
18+
expect(values).toMatchInlineSnapshot(`
19+
{
20+
"arrivedState": {
21+
"bottom": true,
22+
"left": true,
23+
"right": true,
24+
"top": true,
25+
},
26+
"directions": {
27+
"bottom": false,
28+
"left": false,
29+
"right": false,
30+
"top": false,
31+
},
32+
"isScrolling": false,
33+
"measure": [Function],
34+
"x": 0,
35+
"y": 0,
36+
}
37+
`)
38+
})
39+
})

packages/core/useScroll/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const ARRIVED_STATE_THRESHOLD_PIXELS = 1
8383
* @param element
8484
* @param options
8585
*/
86-
8786
export function useScroll(
8887
element: MaybeRefOrGetter<HTMLElement | SVGElement | Window | Document | null | undefined>,
8988
options: UseScrollOptions = {},
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useScroll } from '@vueuse/core'
2+
import { describe, expect, it } from 'vitest'
3+
import { reactive } from 'vue'
4+
import { useWindowScroll } from '.'
5+
6+
describe('useWindowScroll', () => {
7+
it('should be defined', () => {
8+
expect(useWindowScroll).toBeDefined()
9+
})
10+
11+
it('should have default x and y', () => {
12+
const { x, y } = useWindowScroll()
13+
expect(x.value).toBe(0)
14+
expect(y.value).toBe(0)
15+
})
16+
17+
it('should have right default values', () => {
18+
const values = reactive(useScroll(window))
19+
expect(values).toMatchInlineSnapshot(`
20+
{
21+
"arrivedState": {
22+
"bottom": true,
23+
"left": true,
24+
"right": true,
25+
"top": true,
26+
},
27+
"directions": {
28+
"bottom": false,
29+
"left": false,
30+
"right": false,
31+
"top": false,
32+
},
33+
"isScrolling": false,
34+
"measure": [Function],
35+
"x": 0,
36+
"y": 0,
37+
}
38+
`)
39+
})
40+
})
Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { ConfigurableWindow } from '../_configurable'
2-
import { computed, ref } from 'vue'
2+
import type { UseScrollOptions } from '../useScroll'
33
import { defaultWindow } from '../_configurable'
4-
import { useEventListener } from '../useEventListener'
4+
import { useScroll } from '../useScroll'
55

6-
export interface UseWindowScrollOptions extends ConfigurableWindow {
7-
behavior?: ScrollBehavior
6+
export interface UseWindowScrollOptions extends ConfigurableWindow, UseScrollOptions {
87
}
98

109
/**
@@ -13,50 +12,9 @@ export interface UseWindowScrollOptions extends ConfigurableWindow {
1312
* @see https://vueuse.org/useWindowScroll
1413
* @param options
1514
*/
16-
1715
export function useWindowScroll(options: UseWindowScrollOptions = {}) {
18-
const { window = defaultWindow, behavior = 'auto' } = options
19-
if (!window) {
20-
return {
21-
x: ref(0),
22-
y: ref(0),
23-
}
24-
}
25-
26-
const internalX = ref(window.scrollX)
27-
const internalY = ref(window.scrollY)
28-
29-
const x = computed({
30-
get() {
31-
return internalX.value
32-
},
33-
set(x: number) {
34-
scrollTo({ left: x, behavior })
35-
},
36-
})
37-
const y = computed({
38-
get() {
39-
return internalY.value
40-
},
41-
set(y: number) {
42-
scrollTo({ top: y, behavior })
43-
},
44-
})
45-
46-
useEventListener(
47-
window,
48-
'scroll',
49-
() => {
50-
internalX.value = window.scrollX
51-
internalY.value = window.scrollY
52-
},
53-
{
54-
capture: false,
55-
passive: true,
56-
},
57-
)
58-
59-
return { x, y }
16+
const { window = defaultWindow, ...rest } = options
17+
return useScroll(window, rest)
6018
}
6119

6220
export type UseWindowScrollReturn = ReturnType<typeof useWindowScroll>

0 commit comments

Comments
 (0)