Skip to content

Commit

Permalink
feat(icon): use a11y-title instead of a11y-label
Browse files Browse the repository at this point in the history
The most accesssible way to label an svg icon is to use a title
element in the svg itself. This is better than using aria-label
which should be reserved for cases where an accessible name can
not be derived. See this page for more details:
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title
  • Loading branch information
brentswisher committed Nov 27, 2023
1 parent 95270be commit 4dd8bfb
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/pharos/src/components/icon/pharos-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export class PharosIcon extends PharosElement {
public description = '';

/**
* Indicates the aria-label to apply to the icon.
* @attr a11y-label
* Indicates the svg title to apply to the icon.
* @attr a11y-title
*/
@property({ type: String, reflect: true, attribute: 'a11y-label' })
public a11yLabel?: string;
@property({ type: String, reflect: true, attribute: 'a11y-title' })
public a11yTitle?: string;

/**
* Indicates whether the icon should be hidden from assistive technology.
Expand All @@ -57,7 +57,7 @@ export class PharosIcon extends PharosElement {
super.update && super.update(changedProperties);
if (this.description.length) {
console.warn(
"The 'description' attribute of pharos-icon is deprecated and will be removed in the next major release. Please use a11y-label or mark the icon as descriptive by using a11y-hidden instead."
"The 'description' attribute of pharos-icon is deprecated and will be removed in the next major release. Please use a11y-title or mark the icon as descriptive by using a11y-hidden instead."
);
}
}
Expand All @@ -84,7 +84,7 @@ export class PharosIcon extends PharosElement {

protected override render(): TemplateResult {
const size = this._getIconSize();
const accessiblityLabel = this.a11yLabel ?? this.description;
const accessibilityLabel = this.a11yTitle ?? this.description;
return html`
<svg
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -93,11 +93,11 @@ export class PharosIcon extends PharosElement {
class="icon"
role="img"
aria-hidden=${this.a11yHidden || this.description === ''}
aria-label=${accessiblityLabel}
height="${size}"
width="${size}"
focusable="false"
>
<title>${accessibilityLabel}</title>
${unsafeSVG(this._svg)}
</svg>
`;
Expand Down

0 comments on commit 4dd8bfb

Please sign in to comment.