Skip to content

Commit f568af1

Browse files
crisbetowagnermaciel
authored andcommitted
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. (cherry picked from commit 377378b)
1 parent f0dc895 commit f568af1

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
@@ -997,6 +997,26 @@ describe('MDC-based MatMenu', () => {
997997
.toBe(overlayContainerElement.querySelector('.mat-mdc-menu-panel'));
998998
}));
999999

1000+
it('should clear the static aria-label from the menu host', () => {
1001+
const fixture = createComponent(StaticAriaLabelMenu);
1002+
fixture.detectChanges();
1003+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-label')).toBe(false);
1004+
});
1005+
1006+
it('should clear the static aria-labelledby from the menu host', () => {
1007+
const fixture = createComponent(StaticAriaLabelledByMenu);
1008+
fixture.detectChanges();
1009+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-labelledby'))
1010+
.toBe(false);
1011+
});
1012+
1013+
it('should clear the static aria-describedby from the menu host', () => {
1014+
const fixture = createComponent(StaticAriaDescribedbyMenu);
1015+
fixture.detectChanges();
1016+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-describedby'))
1017+
.toBe(false);
1018+
});
1019+
10001020
describe('lazy rendering', () => {
10011021
it('should be able to render the menu content lazily', fakeAsync(() => {
10021022
const fixture = createComponent(SimpleLazyMenu);
@@ -2691,3 +2711,21 @@ class LazyMenuWithOnPush {
26912711
})
26922712
class InvalidRecursiveMenu {
26932713
}
2714+
2715+
2716+
@Component({
2717+
template: '<mat-menu aria-label="label"></mat-menu>'
2718+
})
2719+
class StaticAriaLabelMenu {}
2720+
2721+
2722+
@Component({
2723+
template: '<mat-menu aria-labelledby="some-element"></mat-menu>'
2724+
})
2725+
class StaticAriaLabelledByMenu {}
2726+
2727+
2728+
@Component({
2729+
template: '<mat-menu aria-describedby="some-element"></mat-menu>'
2730+
})
2731+
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
@@ -916,6 +916,26 @@ describe('MatMenu', () => {
916916
expect(document.activeElement).toBe(overlayContainerElement.querySelector('.mat-menu-panel'));
917917
}));
918918

919+
it('should clear the static aria-label from the menu host', () => {
920+
const fixture = createComponent(StaticAriaLabelMenu);
921+
fixture.detectChanges();
922+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-label')).toBe(false);
923+
});
924+
925+
it('should clear the static aria-labelledby from the menu host', () => {
926+
const fixture = createComponent(StaticAriaLabelledByMenu);
927+
fixture.detectChanges();
928+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-labelledby'))
929+
.toBe(false);
930+
});
931+
932+
it('should clear the static aria-describedby from the menu host', () => {
933+
const fixture = createComponent(StaticAriaDescribedbyMenu);
934+
fixture.detectChanges();
935+
expect(fixture.nativeElement.querySelector('mat-menu').hasAttribute('aria-describedby'))
936+
.toBe(false);
937+
});
938+
919939
describe('lazy rendering', () => {
920940
it('should be able to render the menu content lazily', fakeAsync(() => {
921941
const fixture = createComponent(SimpleLazyMenu);
@@ -2654,3 +2674,21 @@ class LazyMenuWithOnPush {
26542674
})
26552675
class InvalidRecursiveMenu {
26562676
}
2677+
2678+
2679+
@Component({
2680+
template: '<mat-menu aria-label="label"></mat-menu>'
2681+
})
2682+
class StaticAriaLabelMenu {}
2683+
2684+
2685+
@Component({
2686+
template: '<mat-menu aria-labelledby="some-element"></mat-menu>'
2687+
})
2688+
class StaticAriaLabelledByMenu {}
2689+
2690+
2691+
@Component({
2692+
template: '<mat-menu aria-describedby="some-element"></mat-menu>'
2693+
})
2694+
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)