File size: 597 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { resizeElement } from './resizeElement'
import { resizeWindow } from './resizeWindow'

export interface OnResizeOptions {
  container?: HTMLElement
}

export type OnResizeCallback = (
  rect: Pick<DOMRectReadOnly, 'width' | 'height'> &
    Partial<Omit<DOMRectReadOnly, 'width' | 'height'>>
) => void

export const onResize = (
  callback: OnResizeCallback,
  { container = document.documentElement }: OnResizeOptions = {}
): (() => void) => {
  if (container === document.documentElement) {
    return resizeWindow(callback)
  } else {
    return resizeElement(callback, container)
  }
}