Skip to content

Commit 345f693

Browse files
authored
Hds::Icon Groundwork (#2198)
1 parent bc55b7b commit 345f693

File tree

7 files changed

+79
-4
lines changed

7 files changed

+79
-4
lines changed

.changeset/fuzzy-suits-call.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hashicorp/design-system-components": minor
3+
---
4+
5+
Loads the `hds-icon` sprite in the `components` package, and ensures it is only loaded once.

packages/components/addon-main.cjs

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,19 @@
44
*/
55

66
const { addonV1Shim } = require('@embroider/addon-shim');
7-
module.exports = addonV1Shim(__dirname);
7+
const flightIconSprite = require('@hashicorp/flight-icons/svg-sprite/svg-sprite-module');
8+
9+
module.exports = {
10+
...addonV1Shim(__dirname),
11+
contentFor(type, config) {
12+
if (
13+
!config.emberFlightIcons?.lazyEmbed &&
14+
!config.__flightIconsSpriteLoaded &&
15+
type === 'body-footer'
16+
) {
17+
config.__flightIconsSpriteLoaded = true;
18+
19+
return flightIconSprite;
20+
}
21+
},
22+
};

packages/components/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@floating-ui/dom": "^1.6.3",
4141
"@hashicorp/design-system-tokens": "^2.1.0",
4242
"@hashicorp/ember-flight-icons": "^5.1.2",
43+
"@hashicorp/flight-icons": "^3.4.0",
4344
"decorator-transforms": "^1.1.0",
4445
"ember-a11y-refocus": "^4.1.0",
4546
"ember-cli-sass": "^11.0.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import config from 'ember-get-config';
7+
import type ApplicationInstance from '@ember/application/instance';
8+
9+
export async function initialize(
10+
appInstance: ApplicationInstance & {
11+
__flightIconsSpriteLoaded?: boolean;
12+
}
13+
) {
14+
if (
15+
config?.emberFlightIcons?.lazyEmbed &&
16+
// we use this flag to avoid loading the sprite multiple times
17+
appInstance.__flightIconsSpriteLoaded !== true
18+
) {
19+
const { default: svgSprite } = await import(
20+
'@hashicorp/flight-icons/svg-sprite/svg-sprite-module'
21+
);
22+
23+
// in test environments we can inject the sprite directly into the ember testing container
24+
// to avoid issues with tools like Percy that only consider content inside that element
25+
if (config.environment === 'test') {
26+
window.document
27+
?.getElementById('ember-testing')
28+
?.insertAdjacentHTML('afterbegin', svgSprite);
29+
} else {
30+
window.document?.body?.insertAdjacentHTML('beforeend', svgSprite);
31+
}
32+
33+
// set the flag to avoid loading the sprite multiple times
34+
appInstance.__flightIconsSpriteLoaded = true;
35+
}
36+
}
37+
38+
export default {
39+
initialize,
40+
};

packages/ember-flight-icons/addon-main.cjs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ const flightIconSprite = require('@hashicorp/flight-icons/svg-sprite/svg-sprite-
88
module.exports = {
99
...addonV1Shim(__dirname),
1010
contentFor(type, config) {
11-
if (!config.emberFlightIcons?.lazyEmbed && type === 'body-footer') {
11+
if (
12+
!config.emberFlightIcons?.lazyEmbed &&
13+
!config.__flightIconsSpriteLoaded &&
14+
type === 'body-footer'
15+
) {
16+
config.__flightIconsSpriteLoaded = true;
17+
1218
return flightIconSprite;
1319
}
1420
},

packages/ember-flight-icons/src/instance-initializers/load-sprite.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
import config from 'ember-get-config';
77

8-
export async function initialize() {
9-
if (config?.emberFlightIcons?.lazyEmbed) {
8+
export async function initialize(appInstance) {
9+
if (
10+
config?.emberFlightIcons?.lazyEmbed &&
11+
// we use this flag to avoid loading the sprite multiple times
12+
appInstance.__flightIconsSpriteLoaded !== true
13+
) {
1014
const { default: svgSprite } = await import(
1115
'@hashicorp/flight-icons/svg-sprite/svg-sprite-module'
1216
);
@@ -20,6 +24,9 @@ export async function initialize() {
2024
} else {
2125
window.document?.body?.insertAdjacentHTML('beforeend', svgSprite);
2226
}
27+
28+
// set the flag to avoid loading the sprite multiple times
29+
appInstance.__flightIconsSpriteLoaded = true;
2330
}
2431
}
2532

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4110,6 +4110,7 @@ __metadata:
41104110
"@glint/template": "npm:^1.4.0"
41114111
"@hashicorp/design-system-tokens": "npm:^2.1.0"
41124112
"@hashicorp/ember-flight-icons": "npm:^5.1.2"
4113+
"@hashicorp/flight-icons": "npm:^3.4.0"
41134114
"@rollup/plugin-babel": "npm:^6.0.4"
41144115
"@tsconfig/ember": "npm:^3.0.8"
41154116
"@types/ember-qunit": "npm:^6.1.1"

0 commit comments

Comments
 (0)