@fe6/water-pro
Version:
An enterprise-class UI design language and Vue-based implementation
40 lines (32 loc) • 816 B
text/typescript
/** @format */
import { tryOnMounted, tryOnUnmounted } from '@vueuse/core';
import { useDebounce } from './use-debounce';
interface WindowSizeOptions {
once?: boolean;
immediate?: boolean;
listenerOptions?: AddEventListenerOptions | boolean;
}
export function useWindowSizeFn(fn: any, wait = 150, options?: WindowSizeOptions) {
let handler = () => {
fn();
};
const [handleSize, cancel] = useDebounce(handler, wait, options);
handler = handleSize;
const start = () => {
if (options && options.immediate) {
handler();
}
window.addEventListener('resize', handler);
};
const stop = () => {
window.removeEventListener('resize', handler);
cancel();
};
tryOnMounted(() => {
start();
});
tryOnUnmounted(() => {
stop();
});
return [start, stop];
}