Skip to content

Commit d5e2a40

Browse files
committed
cleaning up the component
1 parent 9017c58 commit d5e2a40

File tree

4 files changed

+41
-47
lines changed

4 files changed

+41
-47
lines changed

packages/components/src/components/hds/icon/index.ts

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { SafeString } from '@ember/template';
1919
import type { IconName } from '@hashicorp/flight-icons/svg';
2020
import type {
2121
HdsIconModule,
22-
HdsIconName,
22+
HdsIconRegistryEntry,
2323
} from '@hashicorp/flight-icons/symbol-js/registry.ts';
2424
import type { HdsIconSizes, HdsIconColors } from './types';
2525
import type HdsCarbonService from '../../../services/hds-carbon.ts';
@@ -44,8 +44,6 @@ export default class HdsIcon extends Component<HdsIconSignature> {
4444
@service declare readonly hdsCarbon: HdsCarbonService;
4545

4646
@tracked iconModule: HdsIconModule | null = null;
47-
@tracked _hasCarbonEquivalent = false;
48-
@tracked _isCarbon = false;
4947

5048
private _iconId = 'icon-' + guidFor(this);
5149
private _titleId = 'title-' + guidFor(this);
@@ -58,10 +56,11 @@ export default class HdsIcon extends Component<HdsIconSignature> {
5856
{
5957
name,
6058
size,
59+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6160
carbonModeEnabled,
6261
}: { name: IconName; size: HdsIconSizes; carbonModeEnabled: boolean }
6362
) => {
64-
void this.loadIconModuleTask.perform(name, size, carbonModeEnabled);
63+
void this.loadIconModuleTask.perform(name, size);
6564
}
6665
);
6766

@@ -76,6 +75,26 @@ export default class HdsIcon extends Component<HdsIconSignature> {
7675
return name;
7776
}
7877

78+
get registryEntry(): HdsIconRegistryEntry | undefined {
79+
const { name, size } = this.args;
80+
const registryEntry = IconRegistry[this.name];
81+
82+
assert(
83+
`The icon @name "${name}" or @size "${size}" provided to <Hds::Icon> is not correct.`,
84+
registryEntry !== undefined
85+
);
86+
87+
return IconRegistry[this.name];
88+
}
89+
90+
get hasCarbonEquivalent(): boolean {
91+
return this.registryEntry?.carbon !== null;
92+
}
93+
94+
get isCarbon(): boolean {
95+
return this.hdsCarbon.carbonModeEnabled && this.hasCarbonEquivalent;
96+
}
97+
7998
get svgSymbol(): SafeString | undefined {
8099
if (this.iconModule === null) {
81100
return undefined;
@@ -135,14 +154,6 @@ export default class HdsIcon extends Component<HdsIconSignature> {
135154
return this.args.title ? this._titleId : null;
136155
}
137156

138-
get hasCarbonEquivalent(): boolean {
139-
return this._hasCarbonEquivalent;
140-
}
141-
142-
get isCarbon(): boolean {
143-
return this._isCarbon;
144-
}
145-
146157
get classNames() {
147158
const { name } = this.args;
148159
const classes = ['hds-icon'];
@@ -168,43 +179,26 @@ export default class HdsIcon extends Component<HdsIconSignature> {
168179
}
169180

170181
get viewBox(): string {
171-
const { name } = this.args;
172-
const registryEntry = IconRegistry[name as HdsIconName];
173-
174-
if (this.hdsCarbon.carbonModeEnabled && registryEntry?.carbon) {
182+
if (this.isCarbon) {
175183
return '0 0 32 32';
176184
}
177185

178186
return `0 0 ${this.size} ${this.size}`;
179187
}
180188

181189
loadIconModuleTask = task(
182-
async (
183-
name: IconName,
184-
size: HdsIconSizes,
185-
carbonModeEnabled: boolean
186-
): Promise<void> => {
187-
const registryEntry = IconRegistry[name as HdsIconName];
188-
189-
assert(
190-
`The icon @name "${name}" or @size "${size}" provided to <Hds::Icon> is not correct.`,
191-
registryEntry !== undefined
192-
);
193-
194-
this._hasCarbonEquivalent = registryEntry.carbon !== null;
195-
196-
const shouldUseCarbon = carbonModeEnabled && this._hasCarbonEquivalent;
197-
198-
const loader = shouldUseCarbon
199-
? registryEntry.carbon
200-
: registryEntry.flight[size];
201-
202-
if (loader) {
203-
this.iconModule = await loader();
204-
this._isCarbon = shouldUseCarbon;
205-
} else {
206-
assert(`Size "${size}" not found for icon "${name}"`);
190+
async (name: IconName, size: HdsIconSizes): Promise<void> => {
191+
let loader = this.registryEntry?.flight[size];
192+
let loaderAssertDesc = `Flight icon not available for "${name}" with size "${size}".`;
193+
194+
if (this.isCarbon) {
195+
loader = this.registryEntry?.carbon ?? undefined;
196+
loaderAssertDesc = `Carbon icon not available for "${name}".`;
207197
}
198+
199+
assert(loaderAssertDesc, loader !== undefined);
200+
201+
this.iconModule = await loader();
208202
}
209203
);
210204
}

packages/flight-icons/scripts/build-parts/generateBundleSymbolJS.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export async function generateBundleSymbolJS({ config, catalog }: { config: Conf
118118
await fs.writeFile(`${outputFolder}/registry.js`, registryJsContent);
119119

120120
const registryDtsContent = await prettier.format(`
121+
import type { IconName } from '../svg';
122+
121123
export interface HdsIconModule {
122124
default: (id: string) => string;
123125
}
@@ -129,9 +131,7 @@ export async function generateBundleSymbolJS({ config, catalog }: { config: Conf
129131
carbon: (() => Promise<HdsIconModule>) | null;
130132
}
131133
132-
export declare const IconRegistry: Record<string, HdsIconRegistryEntry>;
133-
134-
export type HdsIconName = keyof typeof IconRegistry;
134+
export declare const IconRegistry: Record<IconName, HdsIconRegistryEntry>;
135135
`, { ...prettierConfig, parser: 'typescript' });
136136

137137
await fs.writeFile(`${outputFolder}/registry.d.ts`, registryDtsContent);

packages/flight-icons/symbol-js/registry.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { IconName } from '../svg';
2+
13
export interface HdsIconModule {
24
default: (id: string) => string;
35
}
@@ -9,6 +11,4 @@ export interface HdsIconRegistryEntry {
911
carbon: (() => Promise<HdsIconModule>) | null;
1012
}
1113

12-
export declare const IconRegistry: Record<string, HdsIconRegistryEntry>;
13-
14-
export type HdsIconName = keyof typeof IconRegistry;
14+
export declare const IconRegistry: Record<IconName, HdsIconRegistryEntry>;
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)