element-plus/packages/utils/resize-event.ts
三咲智子 a902be5917
refactor(utils)!: remove resize-observer-polyfill (#4058)
* refactor(utils): remove resize-observer-polyfill

* test: add resize-observer-polyfill for tests only
2021-10-30 21:48:25 +08:00

45 lines
1.2 KiB
TypeScript

import isServer from './isServer'
import type { CustomizedHTMLElement } from './types'
export type ResizableElement = CustomizedHTMLElement<{
__resizeListeners__?: Array<(...args: unknown[]) => unknown>
__ro__?: ResizeObserver
}>
const resizeHandler = function (entries: ResizeObserverEntry[]) {
for (const entry of entries) {
const listeners =
(entry.target as ResizableElement).__resizeListeners__ || []
if (listeners.length) {
listeners.forEach((fn) => {
fn()
})
}
}
}
export const addResizeListener = function (
element: ResizableElement,
fn: (...args: unknown[]) => unknown
): void {
if (isServer || !element) return
if (!element.__resizeListeners__) {
element.__resizeListeners__ = []
element.__ro__ = new ResizeObserver(resizeHandler)
element.__ro__.observe(element)
}
element.__resizeListeners__.push(fn)
}
export const removeResizeListener = function (
element: ResizableElement,
fn: (...args: unknown[]) => unknown
): void {
if (!element || !element.__resizeListeners__) return
element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1)
if (!element.__resizeListeners__.length) {
element.__ro__?.disconnect()
}
}