injectIcons.ts 932 B

12345678910111213141516171819202122232425262728
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { useIcons } from './useIcons.ts'
  3. const loadSvg = () => {
  4. const { body } = document
  5. let svgDom = document.getElementById('__svg__icons__dom__') as unknown as
  6. | SVGSVGElement
  7. | undefined
  8. if (!svgDom) {
  9. svgDom = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  10. svgDom.style.position = 'absolute'
  11. svgDom.style.width = '0'
  12. svgDom.style.height = '0'
  13. svgDom.id = '__svg__icons__dom__'
  14. svgDom.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
  15. svgDom.setAttribute('xmlns:link', 'http://www.w3.org/1999/xlink')
  16. }
  17. const { symbols } = useIcons()
  18. const html = symbols.map((symb) => symb[1]).join('\n')
  19. svgDom.innerHTML = html
  20. body.insertBefore(svgDom, body.lastChild)
  21. }
  22. if (document.readyState === 'loading') {
  23. document.addEventListener('DOMContentLoaded', loadSvg)
  24. } else {
  25. loadSvg()
  26. }