Skip to content

Commit 12c59e9

Browse files
authored
fix(icon): use icons package (#2502)
* fix(icon): use icons package * fix(icon): build out fa6 icons * feat(icon): getIconUrl: string * chore: fix text command dependencies * docs(icon): typos, icon sets, search
1 parent c5d9588 commit 12c59e9

File tree

17 files changed

+484
-592
lines changed

17 files changed

+484
-592
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/elements": minor
3+
---
4+
`<pf-icon>`: allow `getIconUrl` to return a string, permitting users to import
5+
icons from 'bare module specifiers'.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@patternfly/elements": patch
3+
---
4+
`<pf-icon>`: use fontawesome 5 icons. **NOTE**: imports from
5+
`@patternfly/elements/pf-icon/icons/` are deprecated and will be removed in the
6+
next major version.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ tools/*/test/**/*.png
6363
!core/*/demo/*
6464
!tools/*/demo/*
6565

66+
elements/pf-icon/icons
6667
!elements/pf-icon/demo/icons/**/*.js
6768

6869
!scripts/**/*.js

elements/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"Wes Ruvalcaba"
118118
],
119119
"dependencies": {
120+
"@patternfly/icons": "^1.0.2",
120121
"@patternfly/pfe-core": "^2.3.0",
121122
"lit": "2.6.1",
122123
"tslib": "^2.4.1"

elements/pf-icon/BaseIcon.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Logger } from '@patternfly/pfe-core/controllers/logger.js';
77

88
import style from './BaseIcon.css';
99

10-
export type URLGetter = (set: string, icon: string) => URL;
10+
export type URLGetter = (set: string, icon: string) => URL | string;
1111

1212
/** requestIdleCallback when available, requestAnimationFrame when not */
1313
const ric = window.requestIdleCallback ?? window.requestAnimationFrame;
@@ -140,16 +140,21 @@ export abstract class BaseIcon extends LitElement {
140140
protected async load() {
141141
const { set, icon, } = this;
142142
const getter = this.#class.getters.get(set) ?? this.#class.getIconUrl;
143-
let pathname = 'UNKNOWN ICON';
143+
let spec = 'UNKNOWN ICON';
144144
if (set && icon) {
145145
try {
146-
({ pathname } = getter(set, icon));
147-
const mod = await import(pathname);
146+
const gotten = getter(set, icon);
147+
if (gotten instanceof URL) {
148+
spec = gotten.pathname;
149+
} else {
150+
spec = gotten;
151+
}
152+
const mod = await import(spec);
148153
this.content = mod.default instanceof Node ? mod.default.cloneNode(true) : mod.default;
149154
await this.updateComplete;
150155
this.dispatchEvent(new Event('load', { bubbles: true }));
151156
} catch (error: unknown) {
152-
const event = new IconLoadError(pathname, error as Error);
157+
const event = new IconLoadError(spec, error as Error);
153158
this.#logger.error((error as IconLoadError).message);
154159
this.dispatchEvent(event);
155160
}

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
<link rel="stylesheet" href="demo.css">
2-
<script type="module" src="pf-icon.js"></script>
3-
41
<section id="sizes">
52
<h2>Icon Sizes and Colours</h2>
63
<dl>
@@ -19,13 +16,8 @@ <h2>Icon Sizes and Colours</h2>
1916
</dl>
2017
</section>
2118

22-
<section id="sets">
23-
<h2>Default Icon Sets</h2>
24-
<label>Icon Name
25-
<input id="icon-search" list="icons-list" placeholder="icon-name">
26-
<datalist id="icons-list"></datalist>
27-
</label>
28-
<p>Use the text field above to search for an icon name. Click the icon to copy it's HTML.</p>
29-
<p>PatternFly Icon comes with three built-in icon sets, the default is fontawesome solid</p>
30-
<output id="pf-icon-demo-output"></output>
31-
</section>
19+
<link rel="stylesheet" href="demo.css">
20+
<script type="module">
21+
import '@patternfly/elements/pf-icon/pf-icon.js';
22+
</script>
23+

elements/pf-icon/demo/pf-icon.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ async function copy(event) {
2222
}
2323

2424
function renderIcons(results) {
25+
render(repeat(iconSets, ([setName, icons]) => repeat(icons, icon => `${setName}-${icon}`, icon => html`
26+
<option value="${icon}">${setName} - ${icon}</option>
27+
`)), list);
2528
render(repeat(iconSets, ([setName, icons]) => html`
26-
<h3 id=${setName}>${names.get(setName)}</h3>
29+
<h2 id=${setName}>${names.get(setName)}</h2>
2730
<ul>${repeat(icons, icon => `${setName}-${icon}`, icon => html`
2831
<li ?hidden=${(typeof results === 'string' ? icon !== results : (results && !results[setName]?.[icon]))}>
2932
<button title="${icon}"
@@ -47,22 +50,18 @@ const search = document.getElementById('icon-search');
4750

4851
const list = document.getElementById('icons-list');
4952

50-
search.autocompleteRequest = function({ query }, cb) {
51-
const results = fuse.search(query);
52-
cb(results.map(x => x.item.icon));
53-
renderIcons(results.reduce((acc, { item: { set, icon } }) => ({
54-
...acc,
55-
[set]: {
56-
...acc[set],
57-
[icon]: true
58-
}
59-
}), {}));
60-
};
61-
62-
search.addEventListener('input', e => renderIcons(e.target.value || undefined));
53+
search.addEventListener('input', /** @this{HTMLInputElement}*/function() {
54+
renderIcons(!this.value ? undefined : fuse
55+
.search(this.value)
56+
.reduce((acc, { item: { set, icon } }) => ({
57+
...acc,
58+
[set]: {
59+
...acc[set],
60+
[icon]: true
61+
}
62+
}), {}));
63+
});
6364

6465
renderIcons(search.value || undefined);
6566

66-
render(repeat(iconSets, ([setName, icons]) => repeat(icons, icon => `${setName}-${icon}`, icon => html`
67-
<option value="${icon}">${setName} - ${icon}</option>
68-
`)), list);
67+
document.querySelector('form').addEventListener('submit', e => e.preventDefault());

elements/pf-icon/demo/sets.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<h1>Default Icon Sets</h1>
2+
<form>
3+
<p>Use the text field to search for an icon name. Click the icon to copy it's HTML.</p>
4+
<p>PatternFly Icon comes with three built-in icon sets, the default is fontawesome solid</p>
5+
6+
<label>Icon Name
7+
<input id="icon-search" list="icons-list" placeholder="icon-name">
8+
<datalist id="icons-list"></datalist>
9+
</label>
10+
11+
<output id="pf-icon-demo-output" name="output"></output>
12+
13+
</form>
14+
15+
<link rel="stylesheet" href="demo.css">
16+
<script type="module" src="pf-icon.js"></script>
17+

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ Icons are JavaScript module which export a [lit renderable][renderable],
5858
typically an inline SVG element [template literal][template-literals] tagged
5959
with the Lit [`svg`][svg-tag] template tag. To register a new icon set, call
6060
the static `addIconSet` method with the set name and a getter function. The
61-
getter function takes the icon set and icon name and returns a URL object that
62-
points to the icon's JavaScript module.
61+
getter function takes the icon set and icon name and returns a URL object or a
62+
string that points to the icon's JavaScript module.
6363

6464
```ts
65-
type getter = (set: string, icon: string) => URL
65+
type getter = (set: string, icon: string) => URL | string
6666
```
6767
6868
```javascript
@@ -98,7 +98,7 @@ PfIcon.addIconSet('patternfly', (set, icon) => {
9898
### Override the Default Icon Sets
9999

100100
Icons are [loaded lazily](#loading) by default, so there's no performance
101-
penalty for keeping the default icon sets arond and unused. However, if you'd
101+
penalty for keeping the default icon sets around and unused. However, if you'd
102102
like to override the default icon sets across the entire page, you can use
103103
`addIconSet` with the `fas`, `far`, and `patternfly` set names:
104104

@@ -115,8 +115,9 @@ To change the default set name, you can also override `PfIcon.defaultIconSet`
115115
PfIcon.defaultIconSet = 'patternfly';
116116
```
117117

118-
Now when `<pf-icon>` is used, it will automatically load icon modules from
119-
`https://static.redhat.com/icons/js/patternfly/...`.
118+
Now when `<pf-icon>` is loaded from the [RedHat DX
119+
CDN](https://redhatstatic.com/dx/), it will automatically load icon modules from
120+
the CDN as well.
120121

121122
{% endband %}
122123

0 commit comments

Comments
 (0)