Skip to content

Commit

Permalink
refactor(icon): retrieve icons through dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
clukhei committed Dec 19, 2024
1 parent eaacad9 commit 2734472
Show file tree
Hide file tree
Showing 153 changed files with 637 additions and 511 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"sort-exports/sort-exports": ["error", { "sortDir": "asc" }]
// "jsx-quotes": [2, "prefer-single"],
},
"ignorePatterns": ["**/*.js", "mocks", "**/*.mjs", "cypress", "cypress.config.ts"]
"ignorePatterns": ["**/*.js", "mocks", "**/*.mjs", "cypress", "cypress.config.ts", "**/*/icon-registry.ts"]
}
2 changes: 1 addition & 1 deletion cypress/apps/angular-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",
"module": "ESNext",
"useDefineForClassFields": false,
"lib": ["ES2022", "dom"]
},
Expand Down
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ <h2>Icon</h2>
<sgds-icon name="twitter-x"></sgds-icon>
<sgds-icon name="youtube"></sgds-icon>
<sgds-icon name="google"></sgds-icon>
<sgds-icon name="dash"></sgds-icon>
<sgds-icon name="plus"></sgds-icon>
<sgds-icon name="plusdfsdfs"></sgds-icon>
</div>
<br />
</div>
Expand Down Expand Up @@ -1006,7 +1009,6 @@ <h2>Form Validation</h2>
<sgds-icon slot="invalidIcon" size="md" name="placeholder"></sgds-icon>
</sgds-input>
<sgds-quantity-toggle name="quant" min="1" max="2" value="4" hinttext="type hello" hasFeedback="both">
<sgds-icon slot="invalidIcon" size="md" name="placeholder"></sgds-icon>
</sgds-quantity-toggle>
<sgds-checkbox-group hasFeedback hintText="hint for checkbox" label="checkbox">
<sgds-checkbox name="checkbox" value="checkboxhello" required>she</sgds-checkbox>
Expand Down
6 changes: 4 additions & 2 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ const buildUMDComponentBundles = () => {
name: `${meta.name}Bundle`,
file: `lib/${meta.outputPath}.umd.js`,
format: "umd",
sourcemap: true
sourcemap: true,
inlineDynamicImports: true
},
plugins: wcPlugins
}));
Expand Down Expand Up @@ -112,7 +113,8 @@ const buildSgdsPackage = () => {
entryFileNames: "[name].umd.js",
dir: "lib",
format: "umd",
sourcemap: true
sourcemap: true,
inlineDynamicImports: true
},
plugins: wcPlugins
},
Expand Down
15 changes: 8 additions & 7 deletions src/components/Dropdown/sgds-dropdown-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export class SgdsDropdownItem extends SgdsElement {

@queryAssignedElements({ flatten: true }) anchor: HTMLAnchorElement[];

/** when true, sets the active stylings of .nav-link */
@property({ type: Boolean })
active = false;
/** Disables the SgdsMainnavItem */
@property({ type: Boolean, reflect: true })
disabled = false;
/** when true, sets the active stylings of .nav-link */
@property({ type: Boolean })
active = false;

/** Disables the SgdsMainnavItem */
@property({ type: Boolean, reflect: true })
disabled = false;

connectedCallback(): void {
super.connectedCallback();
Expand All @@ -33,6 +33,7 @@ export class SgdsDropdownItem extends SgdsElement {
}
});
this.setAttribute("role", "menuitem");
this.setAttribute("aria-disabled", `${this.disabled}`);
}

render() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/FileUpload/sgds-file-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class SgdsFileUpload extends SgdsFormValidatorMixin(FormControlElement) {
}
render() {
const getCheckedIcon = () => {
return html`<sgds-icon name="check-fill-circle"></sgds-icon>`;
return html`<sgds-icon name="check-circle-fill"></sgds-icon>`;
};

const listItems = this.selectedFiles.map(
Expand Down
586 changes: 586 additions & 0 deletions src/components/Icon/icon-registry.ts

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions src/components/Icon/sgds-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ export class SgdsIcon extends SgdsElement {

async firstUpdated() {
if (this.name) {
this.loadSvg(this.name);
await this.loadSvg(this.name);
}
}

updated() {
this.style.display = this._svgContent ? "flex" : "none";
}

async loadSvg(name: string) {
async loadSvg(name: string): Promise<void> {
if (name) {
// Dynamically import the SVG if not cached
const pascalName = name
.split("-")
.map(name => String(name).charAt(0).toUpperCase() + String(name).slice(1))
.join("");
try {
const iconPath = new URL(`../../icons/${name}.svg`, import.meta.url).href;
const response = await fetch(iconPath);

if (response.ok) {
const svgContent = await response.text();
// Render the SVG
// this.renderSvg(svgContent);
this._svgContent = svgContent;
const iconRegistry = await import("./icon-registry");
const svg = iconRegistry[pascalName];
if (svg) {
this._svgContent = svg;
} else {
throw new Error("icon `name` is undefined");
}
} catch (error) {
console.error(`Error loading SVG: ${name}`, error);
Expand Down
8 changes: 4 additions & 4 deletions src/components/QuantityToggle/sgds-quantity-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export class SgdsQuantityToggle extends SgdsFormValidatorMixin(FormControlElemen
/** @internal */
static dependencies = {
"sgds-input": SgdsInput,
"sgds-icon-button": SgdsIconButton,
"sgds-icon": SgdsIcon
"sgds-icon-button": SgdsIconButton
};
/** @internal */
@query("sgds-icon-button[ariaLabel^='increase by']") private plusBtn: HTMLButtonElement;
Expand Down Expand Up @@ -237,8 +236,8 @@ export class SgdsQuantityToggle extends SgdsFormValidatorMixin(FormControlElemen
ariaLabel=${`decrease by ${this.step}`}
?disabled=${this.disabled || (this.min !== undefined ? this.value <= this.min : this.value < 1)}
@click=${this._onMinus}
name="dash"
>
<sgds-icon name="dash"></sgds-icon>
</sgds-icon-button>
<sgds-input
type="number"
Expand All @@ -263,7 +262,8 @@ export class SgdsQuantityToggle extends SgdsFormValidatorMixin(FormControlElemen
ariaLabel=${`increase by ${this.step}`}
@click=${this._onPlus}
?disabled=${this.disabled || (this.max !== undefined && this.max && this.value >= this.max)}
><sgds-icon name="plus"></sgds-icon>
name="plus"
>
</sgds-icon-button>
</div>
<div id="announcer" role="region" aria-live="assertive" class="visually-hidden">${this.value}</div>
Expand Down
6 changes: 3 additions & 3 deletions src/components/Tab/sgds-tab-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export class SgdsTabPanel extends SgdsElement {
return html`
<slot
class=${classMap({
"tab-panel": true,
"tab-panel--active": this.active
})}
"tab-panel": true,
"tab-panel--active": this.active
})}
></slot>
`;
}
Expand Down
3 changes: 0 additions & 3 deletions src/icons/arrow-bar-down.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-bar-left.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-bar-right.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-bar-up.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-clockwise.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-down.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-left.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-repeat.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-right.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/arrow-up.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/bank-fill.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/bell-slash.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/bell.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/bi-funnel.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/bookmark-fill.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/bookmark.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/box-arrow-up-right.svg

This file was deleted.

3 changes: 0 additions & 3 deletions src/icons/box-seam.svg

This file was deleted.

Loading

0 comments on commit 2734472

Please sign in to comment.