Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set aria-hidden if no label has been set #612

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading