You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

36 lines
718 B

2 years ago
import { tryOnMounted, tryOnUnmounted, 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)
2 years ago
handler()
2 years ago
window.addEventListener('resize', handler)
}
const stop = () => {
window.removeEventListener('resize', handler)
}
tryOnMounted(() => {
start()
})
tryOnUnmounted(() => {
stop()
})
return [start, stop]
}