global-node.ts 704 B

1234567891011121314151617181920212223242526272829303132
  1. import { isClient } from '@vueuse/core'
  2. const globalNodes: HTMLElement[] = []
  3. let target: HTMLElement = !isClient ? (undefined as any) : document.body
  4. export function createGlobalNode(id?: string) {
  5. const el = document.createElement('div')
  6. if (id !== undefined) {
  7. el.setAttribute('id', id)
  8. }
  9. target.appendChild(el)
  10. globalNodes.push(el)
  11. return el
  12. }
  13. export function removeGlobalNode(el: HTMLElement) {
  14. globalNodes.splice(globalNodes.indexOf(el), 1)
  15. el.remove()
  16. }
  17. export function changeGlobalNodesTarget(el: HTMLElement) {
  18. if (el === target) return
  19. target = el
  20. globalNodes.forEach((el) => {
  21. if (el.contains(target) === false) {
  22. target.appendChild(el)
  23. }
  24. })
  25. }