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
| import { tryOnMounted, tryOnUnmounted } from '@vueuse/core';
| import { useDebounceFn } from '@vueuse/core';
|
| interface WindowSizeOptions {
| once?: boolean;
| immediate?: boolean;
| listenerOptions?: AddEventListenerOptions | boolean;
| }
|
| export function useWindowSizeFn<T>(fn: Fn<T>, wait = 150, options?: WindowSizeOptions) {
| let handler = () => {
| fn();
| };
| const handleSize = useDebounceFn(handler, wait);
| handler = handleSize;
|
| const start = () => {
| if (options && options.immediate) {
| handler();
| }
| window.addEventListener('resize', handler);
| };
|
| const stop = () => {
| window.removeEventListener('resize', handler);
| };
|
| tryOnMounted(() => {
| start();
| });
|
| tryOnUnmounted(() => {
| stop();
| });
| return [start, stop];
| }
|
|