v0.6.22

SVG Inlining

Last updated


This page covers how <temp-svg> swaps an external SVG file for an inline <svg> element, so CSS can style it.

Syntax


  1. <div id="app">
  2. <temp-svg :src="icon" class="icon" @click="close"></temp-svg>
  3. <temp-svg src="/icons/menu.svg"></temp-svg>
  4. </div>
  5. <script>
  6. const app = new QUI({
  7. id: "app",
  8. data: { icon: "/icons/close.svg" },
  9. event: { close: () => {} },
  10. });
  11. </script>
Live preview · @pardnchiu/quickui@0.6.22

StageBehavior
Render<temp-svg :src> sets src and hands the element to an IntersectionObserver
Enters the viewportThe SVG text is fetched and its first child element is parsed
ReplacementThe new <svg> takes the original element's id, every class and onclick, is inserted in its place, and <temp-svg> is removed
FailureThe content of <temp-svg> becomes ☒

<temp-svg> elements already on the page with a static src are processed too when the listener is created.

Rules


RuleDescription
Carried overOnly id, classes and onclick; other attributes and events are not transferred
File contentOnly the first element in the response is used and it must be <svg>; sibling elements after it are discarded
Cross-originRequires CORS headers

Disabling


  1. new QUI({ id: "app", option: { svg: false } });

With it disabled, no listener is created; using <temp-svg :src> in the template then throws because the observer does not exist.
中文