Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .changeset/fix-switch-a11y.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@patternfly/elements": patch
---

`<pf-switch>`: the switch now has a proper static accessible label
independent of its on/off state text.
2 changes: 1 addition & 1 deletion elements/pf-switch/demo/without-label.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<form>
<fieldset>
<legend>Without label</legend>
<pf-switch checked></pf-switch>
<pf-switch accessible-label="Toggle option" checked></pf-switch>
</fieldset>
</form>
</section>
Expand Down
5 changes: 3 additions & 2 deletions elements/pf-switch/pf-switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class PfSwitch extends LitElement {

#internals = InternalsController.of(this, { role: 'switch' });

/** Accessible label text for the switch */
@property({ reflect: true }) label?: string;
/** Accessible label for the switch when there is no associated `<label>` element. */
@property({ reflect: true, attribute: 'accessible-label' }) accessibleLabel?: string;
Comment thread
bennypowers marked this conversation as resolved.

/** Flag to show if the switch has a check icon. */
@property({ reflect: true, type: Boolean, attribute: 'show-check-icon' }) showCheckIcon = false;
Expand Down Expand Up @@ -57,6 +57,7 @@ export class PfSwitch extends LitElement {
override willUpdate(): void {
this.#internals.ariaChecked = String(!!this.checked);
this.#internals.ariaDisabled = String(!!this.disabled);
this.#internals.ariaLabel = this.accessibleLabel || null;
}

override render(): TemplateResult<1> {
Expand Down
21 changes: 21 additions & 0 deletions elements/pf-switch/test/pf-switch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ describe('<pf-switch>', function() {
});
});

describe('with accessible-label attribute', function() {
let element: PfSwitch;
let snapshot: A11yTreeSnapshot;
beforeEach(async function() {
element = await createFixture<PfSwitch>(html`
<pf-switch accessible-label="Dark Mode"></pf-switch>
`);
snapshot = await a11ySnapshot({ selector: 'pf-switch' });
});
it('has an accessible name from accessible-label', function() {
expect(snapshot.name).to.equal('Dark Mode');
});
it('keeps the same accessible name regardless of checked state', async function() {
element.click();
await element.updateComplete;
await nextFrame();
snapshot = await a11ySnapshot({ selector: 'pf-switch' });
expect(snapshot.name).to.equal('Dark Mode');
});
});

describe('with labels for on and off state', function() {
let element: PfSwitch;
let snapshot: A11yTreeSnapshot;
Expand Down
Loading