Skip to content

Commit

Permalink
fix!: improve performance of element creation
Browse files Browse the repository at this point in the history
  • Loading branch information
erictooth committed Aug 27, 2024
1 parent d991dc4 commit 8ac887e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/BaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ export const BaseAdapter = (config: SmartIconOptions, eventBus: EventBus) =>
this.setAttribute("name", name);
}

generateTemplate(): string | PromiseLike<string> {
return "";
generateTemplate(): Node | PromiseLike<Node> | null {
return null;
}

update = (): void | Promise<void> => {
return;
};

async connectedCallback() {
this.innerHTML = await this.generateTemplate();
const template = await this.generateTemplate();
if (template) {
this.replaceChildren(template);
}
eventBus.addEventListener(Events.UPDATED, this.update);
}

Expand Down
23 changes: 17 additions & 6 deletions src/adapters/SVGFetchAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ export const SVGFetchAdapter =
(options: SVGFetchAdapterOptions = {}) =>
(config: SmartIconOptions, eventBus: EventBus) => {

Check warning on line 10 in src/adapters/SVGFetchAdapter.ts

View workflow job for this annotation

GitHub Actions / Test & Build

Missing return type on function
return class SVGFetchAdapter extends BaseAdapter(config, eventBus) {
async getSvgText(): Promise<string> {
async getSvgText(): Promise<Node> {
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
Expand All @@ -23,15 +28,21 @@ export const SVGFetchAdapter =

const viewBox = (fragment && fragment.getAttribute("viewBox")) || "";

return `<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" ${
(viewBox && `viewBox="${viewBox}"`) || ""
}>${(fragment && fragment.innerHTML) || null}</svg>`;
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());
};
};
};
14 changes: 9 additions & 5 deletions src/adapters/SVGUseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ export const SVGUseAdapter = (config: SmartIconOptions, eventBus: EventBus) =>
class SVGUseAdapter extends BaseAdapter(config, eventBus) {
generateTemplate() {
const href = this.getPath();
return `<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%"><use id="smart-icon__use" xlink:href="${href}" href="${href}" /></svg>`;
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());
};
};

0 comments on commit 8ac887e

Please sign in to comment.