chore(utils): improve code style for global-node (#17675)

refactor: improve code style for global-node
This commit is contained in:
thinkasany
2024-07-30 15:07:59 +08:00
committed by GitHub
parent a9b91d1f0f
commit b1b69ed235

View File

@@ -1,7 +1,7 @@
import { isClient } from '../browser'
const globalNodes: HTMLElement[] = []
let target: HTMLElement = !isClient ? (undefined as any) : document.body
let target: HTMLElement | undefined = !isClient ? undefined : document.body
export function createGlobalNode(id?: string) {
const el = document.createElement('div')
@@ -9,8 +9,10 @@ export function createGlobalNode(id?: string) {
el.setAttribute('id', id)
}
target.appendChild(el)
globalNodes.push(el)
if (target) {
target.appendChild(el)
globalNodes.push(el)
}
return el
}
@@ -25,7 +27,7 @@ export function changeGlobalNodesTarget(el: HTMLElement) {
target = el
globalNodes.forEach((el) => {
if (el.contains(target) === false) {
if (target && !el.contains(target)) {
target.appendChild(el)
}
})