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.

41 lines
859 B

2 years ago
import { tryOnMounted, tryOnUnmounted, useDebounceFn } from '@vueuse/core'
import type { AnyFunction } from '@/utils/types'
2 years ago
interface UseWindowSizeOptions {
wait?: number
2 years ago
once?: boolean
immediate?: boolean
listenerOptions?: AddEventListenerOptions | boolean
}
function useWindowSizeFn(fn: AnyFunction, options: UseWindowSizeOptions = {}) {
const { wait = 150, immediate } = options
2 years ago
let handler = () => {
fn()
}
const handleSize = useDebounceFn(handler, wait)
handler = handleSize
const start = () => {
if (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 }
2 years ago
}
export { useWindowSizeFn, type UseWindowSizeOptions }