Skip to content

Commit 1dcbf92

Browse files
authored
Merge 69b2128 into 8190f8a
2 parents 8190f8a + 69b2128 commit 1dcbf92

File tree

10 files changed

+335
-262
lines changed

10 files changed

+335
-262
lines changed

.changeset/dirty-bears-win.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
"@patternfly/elements": major
3+
---
4+
`<pf-icon>`: removed the `getIconUrl` static method, and replaced it with the
5+
`resolve` static method
6+
7+
The steps for overriding icon loading behaviour have changed. Before, you had to
8+
return a string from the `getIconUrl` method, or the second argument to
9+
`addIconSet`. Now, both of those functions must return a Node, or any lit-html
10+
renderable value, or a Promise thereof.
11+
12+
BEFORE:
13+
14+
```js
15+
PfIcon.addIconSet('local', (set, icon) =>
16+
new URL(`/assets/icons/${set}-${icon}.js`));
17+
18+
// or
19+
PfIcon.getIconUrl = (set, icon) =>
20+
new URL(`/assets/icons/${set}-${icon}.js`))
21+
```
22+
23+
AFTER
24+
```js
25+
PfIcon.addIconSet('local', (set, icon) =>
26+
import(`/assets/icons/${set}-${icon}.js`))
27+
.then(mod => mod.default);
28+
29+
// or
30+
PfIcon.resolve = (set, icon) =>
31+
import(`/assets/icons/${set}-${icon}.js`))
32+
.then(mod => mod.default);
33+
```
34+

.changeset/fresh-shrimps-work.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/elements": major
3+
---
4+
`<pf-icon>`: removed the `defaultIconSet` static field.

elements/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
],
131131
"dependencies": {
132132
"@lit/context": "^1.1.0",
133-
"@patternfly/icons": "^1.0.2",
133+
"@patternfly/icons": "^1.0.3",
134134
"@patternfly/pfe-core": "^3.0.0",
135135
"lit": "^3.1.2",
136136
"tslib": "^2.6.2"

elements/pf-icon/README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Icon comes with three built-in icon sets:
4949

5050
1. `fas`: Font Awesome Free Solid icons (the default set)
5151
1. `far`: Font Awesome Free Regular icons
52+
1. `fab`: Font Awesome Free Bold icons
5253
1. `patternfly`: PatternFly icons
5354

5455
Use the `set` attribute to pick an alternative icon set.
@@ -61,19 +62,31 @@ Use the `set` attribute to pick an alternative icon set.
6162
It is possible to add custom icon sets or override the default sets.
6263
Icon sets are defined in detail in [the docs][icon-sets].
6364

64-
### Bundling
65+
### Bundling and custom loading behaviour
6566

66-
When bundling PfIcon with other modules, the default icon imports will be
67-
relative to the bundle, not the source file, so be sure to either register all
68-
the icon sets you'll need, or override the default getter.
67+
When bundling `<pf-icon>` with other modules (e.g. using webpack, rollup,
68+
esbuild, vite, or similar tools), icon imports will be code-split into chunks,
69+
as they are imported from the `@patternfly/icons` package. Ensure that your
70+
bundler is configured to permit dynamic imports, or mark the `@patternfly/icons`
71+
package as "external" and apply an [import map][importmap] to your page instead.
72+
If you would like to
73+
customize the loading behaviour, override the `PfIcon.resolve()` static method.
74+
This methods takes two arguments: the icon set (a string) and the icon name
75+
(a string), and returns a promise of the icon contents, which is a DOM node, or
76+
[anything else that lit-html can render][renderable].
6977

7078
```js
71-
// Workaround for bundled pf-icon: make icon imports absolute, instead of
72-
relative to the bundle
7379
import { PfIcon } from '/pfe.min.js';
74-
PfIcon.getIconUrl = (set, icon) =>
75-
new URL(`/assets/icons/${set}/${icon}.js`, import.meta.url);
76-
// default: new URL(`./icons/${set}/${icon}.js`, import.meta.url);
80+
PfIcon.resolve = async function(set, icon) {
81+
try {
82+
const { default: content } = await import(`/assets/icons/${set}/${icon}.js`);
83+
if (content instanceof Node) {
84+
return content.cloneNode(true);
85+
}
86+
} catch (e) {
87+
return '';
88+
}
89+
}
7790
```
7891

7992
## Loading
@@ -84,3 +97,5 @@ see the [docs][docs] for more info.
8497

8598
[docs]: https://patternflyelements.org/components/icon/
8699
[icon-sets]: https://patternflyelements.org/components/icon/#icon-sets
100+
[renderable]: https://lit.dev/docs/components/rendering/#renderable-values
101+
[importmap]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap

elements/pf-icon/demo/custom-icon-sets.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ <h3>Custom Icon Sets</h3>
1515
```
1616
```js
1717
import { PfIcon } from '@patternfly/elements/pf-icon/pf-icon.js';
18-
PfIcon.addIconSet('rh', (set, icon) =>
19-
new URL(`./icons/${set}/${icon}.js`, import.meta.url));
18+
PfIcon.addIconSet('rh', async (set, icon) =>
19+
import(`./icons/${set}/${icon}.js`)
20+
.then(mod => mod.default));
2021
```
2122
</script>
2223
</zero-md>
@@ -25,8 +26,9 @@ <h3>Custom Icon Sets</h3>
2526
<script type="module">
2627
import 'zero-md';
2728
import { PfIcon } from '@patternfly/elements/pf-icon/pf-icon.js';
28-
PfIcon.addIconSet('rh', (set, icon) =>
29-
new URL(`../icons/${set}/${icon}.js`, import.meta.url));
29+
PfIcon.addIconSet('rh', async (set, icon) =>
30+
import(`../icons/${set}/${icon}.js`)
31+
.then(mod => mod.default));
3032
</script>
3133

3234
<style>

elements/pf-icon/docs/pf-icon.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ Icons are JavaScript module which export a [lit renderable][renderable],
6060
typically an inline SVG element [template literal][template-literals] tagged
6161
with the Lit [`svg`][svg-tag] template tag. To register a new icon set, call
6262
the static `addIconSet` method with the set name and a getter function. The
63-
getter function takes the icon set and icon name and returns a URL object or a
64-
string that points to the icon's JavaScript module.
63+
getter function takes the icon set and icon name and returns a promise containing
64+
the icon node, or any other [renderable][renderable] value.
6565

6666
```ts
67-
type getter = (set: string, icon: string) => URL | string
67+
type IconResolveFunction = (set: string, icon: string) => Promise<Node> | Node;
6868
```
6969

7070
```javascript
@@ -74,7 +74,8 @@ import { PfIcon } from '@patternfly/pf-icon';
7474
// const PfIcon = await customElements.whenDefined('pf-icon');
7575

7676
PfIcon.addIconSet('local', (set, icon) =>
77-
new URL(`/assets/icons/${set}-${icon}.js`));
77+
import(`/assets/icons/${set}-${icon}.js`))
78+
.then(mod => mod.default);
7879
```
7980

8081
### Updating an Existing Icon Set
@@ -89,7 +90,8 @@ PfIcon.addIconSet('patternfly', (set, icon) => {
8990
// add your custom icons
9091
case 'my-custom-icon':
9192
case 'other-custom-icon':
92-
return new URL(`/icons/patternfly-custom/${icon}.js`, window.location.href);
93+
return import(`/icon-overrides/patternfly-custom/${icon}.js`)
94+
.then(mod => mod.default);
9395
// fall back to built-in icons
9496
default:
9597
return PfIcon.getIconUrl(set, icon);
@@ -107,14 +109,9 @@ like to override the default icon sets across the entire page, you can use
107109
```js
108110
import { PfIcon } from '@patternfly/pf-icon';
109111

110-
PfIcon.getIconUrl = (set, icon) =>
111-
new URL(`/icons/js/${set}/${icon}.js`, 'https://static.redhat.com');
112-
```
113-
114-
To change the default set name, you can also override `PfIcon.defaultIconSet`
115-
116-
```js
117-
PfIcon.defaultIconSet = 'patternfly';
112+
PfIcon.resolve = (set, icon) =>
113+
import(`/icons/${set}-${icon}.js`))
114+
.then(mod => mod.default);
118115
```
119116

120117
Now when `<pf-icon>` is loaded from the [RedHat DX

0 commit comments

Comments
 (0)