forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
44 lines (41 loc) · 1.04 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { UnwrapNestedRefs } from 'vue-demi'
import { reactive, watch } from 'vue-demi'
import type { MaybeRef } from '@vueuse/shared'
import type { UseScrollOptions } from '../useScroll'
import { useScroll } from '../useScroll'
export interface UseInfiniteScrollOptions extends UseScrollOptions {
/**
* The minimum distance between the bottom of the element and the bottom of the viewport
*
* @default 0
*/
distance?: number
}
/**
* Reactive infinite scroll.
*
* @see https://vueuse.org/useInfiniteScroll
*/
export function useInfiniteScroll(
element: MaybeRef<HTMLElement | SVGElement | Window | Document | null | undefined>,
onLoadMore: (state: UnwrapNestedRefs<ReturnType<typeof useScroll>>) => void,
options: UseInfiniteScrollOptions = {},
) {
const state = reactive(useScroll(
element,
{
...options,
offset: {
bottom: options.distance ?? 0,
...options.offset,
},
},
))
watch(
() => state.arrivedState.bottom,
(v) => {
if (v)
onLoadMore(state)
},
)
}