Skip to content

Commit

Permalink
feat(button): require a11y-label for icon buttons
Browse files Browse the repository at this point in the history
Because the icon in an icon button is set to aria-hidden="true", it is
not visible to screen readers. This change requires an a11y-label for
icon buttons to ensure that the button is accessible.
  • Loading branch information
brentswisher committed Mar 7, 2024
1 parent df71253 commit c13a4fc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tough-buckets-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ithaka/pharos': major
---

Require an a11y-label for icon buttons
16 changes: 16 additions & 0 deletions packages/pharos/src/components/button/pharos-button.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,24 @@ describe('pharos-button', () => {
.thrown;
});

it('throws an error for an icon only button with no accessible label', async () => {
let errorThrown = false;
try {
await fixture(html` <test-pharos-button icon="download"></test-pharos-button> `);
} catch (error) {
if (error instanceof Error) {
errorThrown = true;
expect(error?.message).to.be.equal(
"Icon only buttons must have an accessible name. Please provide an 'a11y-label' attribute for the button using the 'download' icon."
);
}
}
expect(errorThrown).to.be.true;
});

it('allows for an icon to be shown as the content of the button', async () => {
component.icon = 'download';
component.a11yLabel = 'Download';
await component.updateComplete;

const icon = component.renderRoot.querySelector(
Expand Down
6 changes: 5 additions & 1 deletion packages/pharos/src/components/button/pharos-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ export class PharosButton extends ScopedRegistryMixin(FocusMixin(AnchorElement))
`${this.variant} is not a valid variant. Valid variants are: ${VARIANTS.join(', ')}`
);
}

if (this.icon && !this.a11yLabel) {
throw new Error(
`Icon only buttons must have an accessible name. Please provide an 'a11y-label' attribute for the button using the '${this.icon}' icon.`
);
}
if (this.label) {
console.warn("The 'label' attribute is deprecated. Use 'a11y-label' instead.");
}
Expand Down

0 comments on commit c13a4fc

Please sign in to comment.