Skip to content

Commit 01dcf7e

Browse files
devversionjelbourn
authored andcommitted
fix(slide-toggle): input element should use switch role (#15073)
Based on the discussion in #14949 and the feedback from a Google a11y expert on what role the underlying input of the `slide-toggle` should use, we now explicitly specify the new `switch` role. This is should improve accessibility because practically the `slide-toggle` is neither always causing an immediately effective value change (like a `button`), nor does its value only become effective on form submission (like a `role="checkbox`) Read more here: https://www.w3.org/TR/wai-aria-1.1/#switch Fixes #14949.
1 parent 5a84e3e commit 01dcf7e

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/lib/slide-toggle/slide-toggle.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
[class.mat-slide-toggle-bar-no-side-margin]="!labelContent.textContent || !labelContent.textContent.trim()">
44

55
<input #input class="mat-slide-toggle-input cdk-visually-hidden" type="checkbox"
6+
role="switch"
67
[id]="inputId"
78
[required]="required"
89
[tabIndex]="tabIndex"
910
[checked]="checked"
1011
[disabled]="disabled"
1112
[attr.name]="name"
13+
[attr.aria-checked]="checked.toString()"
1214
[attr.aria-label]="ariaLabel"
1315
[attr.aria-labelledby]="ariaLabelledby"
1416
(change)="_onChangeEvent($event)"

src/lib/slide-toggle/slide-toggle.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,26 @@ describe('MatSlideToggle without forms', () => {
102102

103103
it('should correctly update the checked property', () => {
104104
expect(slideToggle.checked).toBeFalsy();
105+
expect(inputElement.getAttribute('aria-checked')).toBe('false');
105106

106107
testComponent.slideChecked = true;
107108
fixture.detectChanges();
108109

109110
expect(inputElement.checked).toBeTruthy();
111+
expect(inputElement.getAttribute('aria-checked')).toBe('true');
110112
});
111113

112114
it('should set the toggle to checked on click', () => {
113115
expect(slideToggle.checked).toBe(false);
116+
expect(inputElement.getAttribute('aria-checked')).toBe('false');
114117
expect(slideToggleElement.classList).not.toContain('mat-checked');
115118

116119
labelElement.click();
117120
fixture.detectChanges();
118121

119122
expect(slideToggleElement.classList).toContain('mat-checked');
120123
expect(slideToggle.checked).toBe(true);
124+
expect(inputElement.getAttribute('aria-checked')).toBe('true');
121125
});
122126

123127
it('should not trigger the click event multiple times', () => {

0 commit comments

Comments
 (0)