Skip to content

Commit ac11819

Browse files
authored
feat(uui-icon): Set aria-hidden if no label has been set (#612)
1 parent b440860 commit ac11819

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export class UUIIconElement extends LitElement {
1919
@state()
2020
private _nameSvg: string | null = null;
2121

22+
/**
23+
* An alternate description to use for assistive devices.
24+
* If omitted, the icon will be considered presentational and ignored by assistive devices.
25+
* @type {string}
26+
* @attr
27+
* @default
28+
*/
29+
@property() label = '';
30+
2231
/**
2332
* Icon name is used to retrieve the icon from a parent Icon Registry.
2433
* If no Icon Registry responds to the given name, the fallback svg will be used.
@@ -36,6 +45,7 @@ export class UUIIconElement extends LitElement {
3645
this.requestIcon();
3746
}
3847
}
48+
3949
private requestIcon() {
4050
if (this._name !== '' && this._name !== null) {
4151
const event = new UUIIconRequestEvent(UUIIconRequestEvent.ICON_REQUEST, {
@@ -92,6 +102,19 @@ export class UUIIconElement extends LitElement {
92102
if (this._retrievedNameIcon === false) {
93103
this.requestIcon();
94104
}
105+
106+
const hasLabel = typeof this.label === 'string' && this.label.length > 0;
107+
108+
if (hasLabel) {
109+
this.setAttribute('role', 'img');
110+
this.setAttribute('aria-label', this.label);
111+
this.removeAttribute('aria-hidden');
112+
} else {
113+
this.removeAttribute('role');
114+
this.removeAttribute('aria-label');
115+
this.setAttribute('aria-hidden', 'true');
116+
}
117+
95118
}
96119

97120
render() {

0 commit comments

Comments
 (0)