Skip to content

Commit

Permalink
Set aria-hidden if no label has been set
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarnef committed Oct 14, 2023
1 parent fc61c8b commit 8bdfbec
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/uui-icon/lib/uui-icon.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ export class UUIIconElement extends LitElement {
@state()
private _nameSvg: string | null = null;

/**
* An alternate description to use for assistive devices.
* If omitted, the icon will be considered presentational and ignored by assistive devices.
* @type {string}
* @attr
* @default
*/
@property() label = '';

/**
* Icon name is used to retrieve the icon from a parent Icon Registry.
* If no Icon Registry responds to the given name, the fallback svg will be used.
Expand All @@ -60,6 +69,7 @@ export class UUIIconElement extends LitElement {
this.requestIcon();
}
}

private requestIcon() {
if (this._name !== '' && this._name !== null) {
const event = new UUIIconRequestEvent(UUIIconRequestEvent.ICON_REQUEST, {
Expand Down Expand Up @@ -116,6 +126,19 @@ export class UUIIconElement extends LitElement {
if (this._retrievedNameIcon === false) {
this.requestIcon();
}

const hasLabel = typeof this.label === 'string' && this.label.length > 0;

if (hasLabel) {
this.setAttribute('role', 'img');
this.setAttribute('aria-label', this.label);
this.removeAttribute('aria-hidden');
} else {
this.removeAttribute('role');
this.removeAttribute('aria-label');
this.setAttribute('aria-hidden', 'true');
}

}

render() {
Expand Down

0 comments on commit 8bdfbec

Please sign in to comment.