Skip to content

Commit

Permalink
feat(useResize): support debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
lmhcoding committed Sep 30, 2020
1 parent 636bbc2 commit 3ccabbf
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/useResize.ts
@@ -1,9 +1,30 @@
import { DeepReadonly, readonly, Ref, ref } from 'vue'
import { useEvent } from './useEvent'
import { useDebounceFn } from './useDebounceFn'

export interface ResizeHandler {
(this: Window, e: WindowEventMap['resize']): any
}

export function useResize(handler: ResizeHandler): void {
useEvent('resize', handler)
export interface IResizeState {
width: DeepReadonly<Ref<number>>
height: DeepReadonly<Ref<number>>
}

export function useResize(handler: ResizeHandler | null = null, delay = 200): IResizeState {
const width = ref(window.innerWidth)
const height = ref(window.innerHeight)
let cb: ResizeHandler = function (this: Window, e: WindowEventMap['resize']) {
handler && handler.call(this, e)
width.value = window.innerWidth
height.value = window.innerHeight
}
if (delay) {
cb = useDebounceFn(cb, delay)
}
useEvent('resize', cb)
return {
width: readonly(width),
height: readonly(height)
}
}

0 comments on commit 3ccabbf

Please sign in to comment.