Skip to content

Commit fb390fb

Browse files
crisbetojelbourn
authored andcommitted
fix(slide-toggle): clear static aria attributes from host nodes (#17085)
Follow-up from #16938. Clears the `aria-*` attributes from the host node so that they're not duplicated with the underlying `input`.
1 parent 534b9c2 commit fb390fb

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('MatSlideToggle without forms', () => {
1717
SlideToggleWithoutLabel,
1818
SlideToggleProjectedLabel,
1919
TextBindingComponent,
20+
SlideToggleWithStaticAriaAttributes,
2021
]
2122
});
2223

@@ -378,6 +379,15 @@ describe('MatSlideToggle without forms', () => {
378379
expect(testComponent.toggleTriggered).toBe(2, 'Expect toggle twice');
379380
expect(testComponent.dragTriggered).toBe(0);
380381
}));
382+
383+
it('should clear static aria attributes from the host node', () => {
384+
const fixture = TestBed.createComponent(SlideToggleWithStaticAriaAttributes);
385+
fixture.detectChanges();
386+
387+
const host: HTMLElement = fixture.nativeElement.querySelector('mat-slide-toggle');
388+
expect(host.hasAttribute('aria-label')).toBe(false);
389+
expect(host.hasAttribute('aria-labelledby')).toBe(false);
390+
});
381391
});
382392

383393
describe('MatSlideToggle with forms', () => {
@@ -804,3 +814,10 @@ class SlideToggleProjectedLabel {}
804814
class TextBindingComponent {
805815
text: string = 'Some text';
806816
}
817+
818+
@Component({
819+
template: `
820+
<mat-slide-toggle aria-label="Slide toggle" aria-labelledby="something"></mat-slide-toggle>
821+
`
822+
})
823+
class SlideToggleWithStaticAriaAttributes {}

src/material-experimental/mdc-slide-toggle/slide-toggle.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export class MatSlideToggleChange {
6262
'class': 'mat-mdc-slide-toggle',
6363
'[id]': 'id',
6464
'[attr.tabindex]': 'null',
65+
'[attr.aria-label]': 'null',
66+
'[attr.aria-labelledby]': 'null',
6567
'[class.mat-primary]': 'color == "primary"',
6668
'[class.mat-accent]': 'color == "accent"',
6769
'[class.mat-warn]': 'color == "warn"',

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('MatSlideToggle without forms', () => {
3030
SlideToggleWithoutLabel,
3131
SlideToggleProjectedLabel,
3232
TextBindingComponent,
33+
SlideToggleWithStaticAriaAttributes,
3334
],
3435
providers: [
3536
{
@@ -495,6 +496,15 @@ describe('MatSlideToggle without forms', () => {
495496
.toContain('mat-slide-toggle-bar-no-side-margin');
496497
});
497498
});
499+
500+
it('should clear static aria attributes from the host node', () => {
501+
const fixture = TestBed.createComponent(SlideToggleWithStaticAriaAttributes);
502+
fixture.detectChanges();
503+
504+
const host: HTMLElement = fixture.nativeElement.querySelector('mat-slide-toggle');
505+
expect(host.hasAttribute('aria-label')).toBe(false);
506+
expect(host.hasAttribute('aria-labelledby')).toBe(false);
507+
});
498508
});
499509

500510
describe('MatSlideToggle with forms', () => {
@@ -920,3 +930,10 @@ class SlideToggleProjectedLabel {}
920930
class TextBindingComponent {
921931
text: string = 'Some text';
922932
}
933+
934+
@Component({
935+
template: `
936+
<mat-slide-toggle aria-label="Slide toggle" aria-labelledby="something"></mat-slide-toggle>
937+
`
938+
})
939+
class SlideToggleWithStaticAriaAttributes {}

src/material/slide-toggle/slide-toggle.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ const _MatSlideToggleMixinBase:
8686
'[id]': 'id',
8787
// Needs to be `-1` so it can still receive programmatic focus.
8888
'[attr.tabindex]': 'disabled ? null : -1',
89+
'[attr.aria-label]': 'null',
90+
'[attr.aria-labelledby]': 'null',
8991
'[class.mat-checked]': 'checked',
9092
'[class.mat-disabled]': 'disabled',
9193
'[class.mat-slide-toggle-label-before]': 'labelPosition == "before"',

0 commit comments

Comments
 (0)