injectIcons.ts 984 B

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