@@ -19,7 +19,7 @@ import type { SafeString } from '@ember/template';
1919import type { IconName } from '@hashicorp/flight-icons/svg' ;
2020import type {
2121 HdsIconModule ,
22- HdsIconName ,
22+ HdsIconRegistryEntry ,
2323} from '@hashicorp/flight-icons/symbol-js/registry.ts' ;
2424import type { HdsIconSizes , HdsIconColors } from './types' ;
2525import 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}
0 commit comments