@neosjs/cli
Version:
Neos(奈欧斯)是一个帮助开发者快速地创建 Vue3 应用并自动配置项目编译的脚手架
39 lines (31 loc) • 770 B
text/typescript
import { tryOnMounted, tryOnUnmounted, useDebounceFn } from '@vueuse/core'
interface WindowSizeOptions {
once?: boolean
immediate?: boolean
listenerOptions?: AddEventListenerOptions | boolean
}
interface Fn<T = any, R = T> {
(...arg: T[]): R
}
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]
}