From 8ac887eaaa57e83b508fc337f15ca12123915ed5 Mon Sep 17 00:00:00 2001 From: erictooth <1023110+erictooth@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:42:33 -0700 Subject: [PATCH] fix!: improve performance of element creation --- src/BaseAdapter.ts | 9 ++++++--- src/adapters/SVGFetchAdapter.ts | 23 +++++++++++++++++------ src/adapters/SVGUseAdapter.ts | 14 +++++++++----- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/BaseAdapter.ts b/src/BaseAdapter.ts index 92b1c70..79fef5f 100644 --- a/src/BaseAdapter.ts +++ b/src/BaseAdapter.ts @@ -27,8 +27,8 @@ export const BaseAdapter = (config: SmartIconOptions, eventBus: EventBus) => this.setAttribute("name", name); } - generateTemplate(): string | PromiseLike { - return ""; + generateTemplate(): Node | PromiseLike | null { + return null; } update = (): void | Promise => { @@ -36,7 +36,10 @@ export const BaseAdapter = (config: SmartIconOptions, eventBus: EventBus) => }; async connectedCallback() { - this.innerHTML = await this.generateTemplate(); + const template = await this.generateTemplate(); + if (template) { + this.replaceChildren(template); + } eventBus.addEventListener(Events.UPDATED, this.update); } diff --git a/src/adapters/SVGFetchAdapter.ts b/src/adapters/SVGFetchAdapter.ts index 0e8d8e4..317c3fc 100644 --- a/src/adapters/SVGFetchAdapter.ts +++ b/src/adapters/SVGFetchAdapter.ts @@ -9,11 +9,16 @@ export const SVGFetchAdapter = (options: SVGFetchAdapterOptions = {}) => (config: SmartIconOptions, eventBus: EventBus) => { return class SVGFetchAdapter extends BaseAdapter(config, eventBus) { - async getSvgText(): Promise { + async getSvgText(): Promise { const svgText = await fetch(this.getPath()).then((res) => res.text()); + const el = document.createElementNS("http://www.w3.org/2000/svg", "svg"); + el.setAttribute("width", "100%"); + el.setAttribute("height", "100%"); + if (!options.querySelector) { - return svgText; + el.innerHTML = svgText; + return el.children[0]; } const fragment = document @@ -23,15 +28,21 @@ export const SVGFetchAdapter = const viewBox = (fragment && fragment.getAttribute("viewBox")) || ""; - return `${(fragment && fragment.innerHTML) || null}`; + if (viewBox) { + el.setAttribute("viewBox", viewBox); + } + + if (fragment) { + el.innerHTML = fragment.innerHTML; + } + + return el; } generateTemplate() { return this.getSvgText(); } update = async () => { - this.innerHTML = await this.generateTemplate(); + this.replaceChildren(await this.getSvgText()); }; }; }; diff --git a/src/adapters/SVGUseAdapter.ts b/src/adapters/SVGUseAdapter.ts index e6292be..e35221d 100644 --- a/src/adapters/SVGUseAdapter.ts +++ b/src/adapters/SVGUseAdapter.ts @@ -5,13 +5,17 @@ export const SVGUseAdapter = (config: SmartIconOptions, eventBus: EventBus) => class SVGUseAdapter extends BaseAdapter(config, eventBus) { generateTemplate() { const href = this.getPath(); - return ``; + const el = document.createElementNS("http://www.w3.org/2000/svg", "svg"); + el.setAttribute("width", "100%"); + el.setAttribute("height", "100%"); + const useEl = document.createElementNS("http://www.w3.org/2000/svg", "use"); + useEl.setAttribute("href", href); + el.replaceChildren(useEl); + return el; } update = () => { - const useElem = this.querySelector("#smart-icon__use")!; - const href = this.getPath(); - useElem.setAttribute("xlink:href", href); - useElem.setAttribute("href", href); + const useElem = this.children[0].children[0]; + useElem.setAttribute("href", this.getPath()); }; };