element-plus/packages/utils/vue/global-node.ts
btea 105d79b0d9
refactor: extract isClient and isNumber isBoolean (#12504)
* refactor: extract isClient and isNumber isBoolean

* test: update test

* refactor: extract isClient to browser
2023-04-20 20:00:49 +08:00

33 lines
702 B
TypeScript

import { isClient } from '../browser'
const globalNodes: HTMLElement[] = []
let target: HTMLElement = !isClient ? (undefined as any) : document.body
export function createGlobalNode(id?: string) {
const el = document.createElement('div')
if (id !== undefined) {
el.setAttribute('id', id)
}
target.appendChild(el)
globalNodes.push(el)
return el
}
export function removeGlobalNode(el: HTMLElement) {
globalNodes.splice(globalNodes.indexOf(el), 1)
el.remove()
}
export function changeGlobalNodesTarget(el: HTMLElement) {
if (el === target) return
target = el
globalNodes.forEach((el) => {
if (el.contains(target) === false) {
target.appendChild(el)
}
})
}