Skip to content

Commit 377378b

Browse files
authored
fix(material/menu): clear static aria attributes from host node (#21231)
Clears the static `aria-` attributes from the menu host since they're forwarded to the menu overlay panel. Fixes #21152.
1 parent 2076686 commit 377378b

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

src/material-experimental/mdc-menu/menu.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,26 @@ describe('MDC-based MatMenu', () => {
994994
.toBe(overlayContainerElement.querySelector('.mat-mdc-menu-panel'));
995995
}));
996996

997+
it('should clear the static aria-label from the menu host', () => {
998+
const fixture = createComponent(StaticAriaLabelMenu);
999+
fixture.detectChanges();
1000+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-label')).toBe(false);
1001+
});
1002+
1003+
it('should clear the static aria-labelledby from the menu host', () => {
1004+
const fixture = createComponent(StaticAriaLabelledByMenu);
1005+
fixture.detectChanges();
1006+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-labelledby'))
1007+
.toBe(false);
1008+
});
1009+
1010+
it('should clear the static aria-describedby from the menu host', () => {
1011+
const fixture = createComponent(StaticAriaDescribedbyMenu);
1012+
fixture.detectChanges();
1013+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-describedby'))
1014+
.toBe(false);
1015+
});
1016+
9971017
describe('lazy rendering', () => {
9981018
it('should be able to render the menu content lazily', fakeAsync(() => {
9991019
const fixture = createComponent(SimpleLazyMenu);
@@ -2603,3 +2623,21 @@ class LazyMenuWithOnPush {
26032623
})
26042624
class InvalidRecursiveMenu {
26052625
}
2626+
2627+
2628+
@Component({
2629+
template: '<mat-menu aria-label="label"></mat-menu>'
2630+
})
2631+
class StaticAriaLabelMenu {}
2632+
2633+
2634+
@Component({
2635+
template: '<mat-menu aria-labelledby="some-element"></mat-menu>'
2636+
})
2637+
class StaticAriaLabelledByMenu {}
2638+
2639+
2640+
@Component({
2641+
template: '<mat-menu aria-describedby="some-element"></mat-menu>'
2642+
})
2643+
class StaticAriaDescribedbyMenu {}

src/material-experimental/mdc-menu/menu.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export const MAT_MENU_SCROLL_STRATEGY_FACTORY_PROVIDER: Provider = {
4444
changeDetection: ChangeDetectionStrategy.OnPush,
4545
encapsulation: ViewEncapsulation.None,
4646
exportAs: 'matMenu',
47+
host: {
48+
'[attr.aria-label]': 'null',
49+
'[attr.aria-labelledby]': 'null',
50+
'[attr.aria-describedby]': 'null',
51+
},
4752
animations: [
4853
matMenuAnimations.transformMenu,
4954
matMenuAnimations.fadeInItems

src/material/menu/menu.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,26 @@ describe('MatMenu', () => {
937937
expect(document.activeElement).toBe(overlayContainerElement.querySelector('.mat-menu-panel'));
938938
}));
939939

940+
it('should clear the static aria-label from the menu host', () => {
941+
const fixture = createComponent(StaticAriaLabelMenu);
942+
fixture.detectChanges();
943+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-label')).toBe(false);
944+
});
945+
946+
it('should clear the static aria-labelledby from the menu host', () => {
947+
const fixture = createComponent(StaticAriaLabelledByMenu);
948+
fixture.detectChanges();
949+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-labelledby'))
950+
.toBe(false);
951+
});
952+
953+
it('should clear the static aria-describedby from the menu host', () => {
954+
const fixture = createComponent(StaticAriaDescribedbyMenu);
955+
fixture.detectChanges();
956+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-describedby'))
957+
.toBe(false);
958+
});
959+
940960
describe('lazy rendering', () => {
941961
it('should be able to render the menu content lazily', fakeAsync(() => {
942962
const fixture = createComponent(SimpleLazyMenu);
@@ -2692,3 +2712,21 @@ class LazyMenuWithOnPush {
26922712
})
26932713
class InvalidRecursiveMenu {
26942714
}
2715+
2716+
2717+
@Component({
2718+
template: '<mat-menu aria-label="label"></mat-menu>'
2719+
})
2720+
class StaticAriaLabelMenu {}
2721+
2722+
2723+
@Component({
2724+
template: '<mat-menu aria-labelledby="some-element"></mat-menu>'
2725+
})
2726+
class StaticAriaLabelledByMenu {}
2727+
2728+
2729+
@Component({
2730+
template: '<mat-menu aria-describedby="some-element"></mat-menu>'
2731+
})
2732+
class StaticAriaDescribedbyMenu {}

src/material/menu/menu.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ export class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatMenuItem>
488488
changeDetection: ChangeDetectionStrategy.OnPush,
489489
encapsulation: ViewEncapsulation.None,
490490
exportAs: 'matMenu',
491+
host: {
492+
'[attr.aria-label]': 'null',
493+
'[attr.aria-labelledby]': 'null',
494+
'[attr.aria-describedby]': 'null',
495+
},
491496
animations: [
492497
matMenuAnimations.transformMenu,
493498
matMenuAnimations.fadeInItems

0 commit comments

Comments
 (0)