Skip to content

Commit 6331e28

Browse files
authored
Merge pull request #129 from umbraco/bugfix/uui-icon-request-at-firstupdate
Fix uui-icon for light-DOM context
2 parents b68edfd + 07691d7 commit 6331e28

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

packages/uui-button/lib/uui-button.element.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
iconCheck,
1212
iconWrong,
1313
} from '@umbraco-ui/uui-icon-registry-essential/lib/svgs';
14+
import '@umbraco-ui/uui-icon/lib';
1415
import { css, html, LitElement } from 'lit';
1516
import { property } from 'lit/decorators.js';
1617

@@ -592,10 +593,14 @@ export class UUIButtonElement extends LabelMixin('', LitElement) {
592593
element = html`<uui-loader-circle id="loader"></uui-loader-circle>`;
593594
break;
594595
case 'success':
595-
element = html`<div id="icon-check" style="">${iconCheck}</div>`;
596+
element = html`<uui-icon
597+
name="check"
598+
.fallback=${iconCheck.strings[0]}></uui-icon>`;
596599
break;
597600
case 'failed':
598-
element = html`<div id="icon-wrong" style="">${iconWrong}</div>`;
601+
element = html`<uui-icon
602+
name="wrong"
603+
.fallback=${iconWrong.strings[0]}></uui-icon>`;
599604
break;
600605
default:
601606
return '';

packages/uui-icon-registry-essential/lib/uui-icon-registry-essential.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { html, fixture, expect } from '@open-wc/testing';
22
import { UUIIconRegistryEssentialElement } from './uui-icon-registry-essential.element';
33
import '.';
4+
import { UUIIconElement } from '@umbraco-ui/uui-icon/lib';
45

56
describe('UUIIconRegistryEssentialElement', () => {
67
let element: UUIIconRegistryEssentialElement;
@@ -14,4 +15,23 @@ describe('UUIIconRegistryEssentialElement', () => {
1415
it('passes the a11y audit', async () => {
1516
await expect(element).shadowDom.to.be.accessible();
1617
});
18+
19+
describe('UUIIcon retrieves SVG data from icon registry', () => {
20+
let registryElement: UUIIconRegistryEssentialElement;
21+
let iconElement: UUIIconElement;
22+
23+
beforeEach(async () => {
24+
registryElement = await fixture(
25+
html`<uui-icon-registry-essential
26+
><uui-icon name="check"></uui-icon
27+
></uui-icon-registry-essential>`
28+
);
29+
30+
iconElement = registryElement.querySelector('uui-icon') as UUIIconElement;
31+
});
32+
33+
it('Child uui-icon retrieved some SVG data', () => {
34+
expect(iconElement.shadowRoot!.querySelector('svg')).to.exist;
35+
});
36+
});
1737
});

packages/uui-icon-registry/lib/uui-icon-registry.element.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { UUIIconRegistry } from './UUIIconRegistry';
1111
export class UUIIconRegistryElement extends LitElement {
1212
/**
1313
* Provide a Dictionary/Record/Object where key is the icon name and the value is the SVGs to define in the icon registry.
14+
* @type {Record<string, string>}
15+
* @default {}
1416
*/
1517
@property({ attribute: false })
1618
private _icons: Record<string, string> = {};
@@ -20,38 +22,28 @@ export class UUIIconRegistryElement extends LitElement {
2022
set icons(icons) {
2123
this._icons = icons;
2224
if (this._registry) {
23-
this.defineIconsInRegistry();
25+
Object.entries(this._icons).forEach(([key, value]) =>
26+
this._registry.defineIcon(key, value)
27+
);
2428
}
2529
}
2630

27-
private defineIconsInRegistry() {
28-
Object.entries(this._icons).forEach(([key, value]) =>
29-
this._registry.defineIcon(key, value)
30-
);
31-
}
32-
3331
private _registry: UUIIconRegistry = new UUIIconRegistry();
3432

3533
public get registry(): UUIIconRegistry {
3634
return this._registry;
3735
}
3836
public set registry(newRegistry: UUIIconRegistry) {
39-
if (this.shadowRoot) {
40-
if (this.registry) {
41-
this.registry.detach(this);
42-
}
43-
newRegistry.attach(this);
37+
if (this.registry) {
38+
this.registry.detach(this);
4439
}
40+
newRegistry.attach(this);
4541
this._registry = newRegistry;
4642
}
4743

48-
connectedCallback(): void {
49-
super.connectedCallback();
50-
this.registry.attach(this);
51-
}
52-
disconnectedCallback(): void {
53-
super.disconnectedCallback();
54-
this.registry.detach(this);
44+
constructor() {
45+
super();
46+
this._registry.attach(this);
5547
}
5648

5749
render() {

packages/uui-icon/lib/uui-icon.element.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class UUIIconElement extends LitElement {
2828
];
2929

3030
private _name: string | null = null;
31+
private _retrievedNameIcon: boolean = false;
3132

3233
@state()
3334
private _nameSvg: string | null = null;
@@ -45,7 +46,9 @@ export class UUIIconElement extends LitElement {
4546
}
4647
set name(newValue) {
4748
this._name = newValue;
48-
this.requestIcon();
49+
if (this.shadowRoot) {
50+
this.requestIcon();
51+
}
4952
}
5053
private requestIcon() {
5154
if (this._name !== '' && this._name !== null) {
@@ -54,11 +57,13 @@ export class UUIIconElement extends LitElement {
5457
});
5558
this.dispatchEvent(event);
5659
if (event.icon !== null) {
60+
this._retrievedNameIcon = true;
5761
event.icon.then((iconSvg: string) => {
5862
this._useFallback = false;
5963
this._nameSvg = iconSvg;
6064
});
6165
} else {
66+
this._retrievedNameIcon = false;
6267
this._useFallback = true;
6368
}
6469
}
@@ -87,15 +92,20 @@ export class UUIIconElement extends LitElement {
8792

8893
connectedCallback() {
8994
super.connectedCallback();
90-
if (this._name !== '' && this._name !== null) {
91-
if (this._nameSvg === null) {
92-
this.requestIcon();
93-
}
95+
if (this._retrievedNameIcon === false) {
96+
this.requestIcon();
9497
}
9598
}
9699

97100
disconnectedCallback(): void {
98-
this._nameSvg = null;
101+
this._retrievedNameIcon = false;
102+
}
103+
104+
firstUpdated() {
105+
// Registry might not have been created then this icon was connected, so therefor we will make another attempt to retrieve the icon.
106+
if (this._retrievedNameIcon === false) {
107+
this.requestIcon();
108+
}
99109
}
100110

101111
render() {

0 commit comments

Comments
 (0)