Skip to content

Commit 2ece035

Browse files
crisbetojelbourn
authored andcommitted
feat(menu): allow for backdrop class to be customized (#10097)
Allows for the backdrop class of a menu to be overwritten. Fixes #10062.
1 parent cdd224a commit 2ece035

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

src/lib/menu/menu-directive.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export interface MatMenuDefaultOptions {
5555

5656
/** Whether the menu should overlap the menu trigger. */
5757
overlapTrigger: boolean;
58+
59+
/** Class to be applied to the menu's backdrop. */
60+
backdropClass: string;
5861
}
5962

6063
/** Injection token to be used to override the default options for `mat-menu`. */
@@ -103,6 +106,9 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro
103106
/** Layout direction of the menu. */
104107
direction: Direction;
105108

109+
/** Class to be added to the backdrop element. */
110+
@Input() backdropClass: string = this._defaultOptions.backdropClass;
111+
106112
/** Position of the menu in the X axis. */
107113
@Input()
108114
get xPosition(): MenuPositionX { return this._xPosition; }

src/lib/menu/menu-module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {MatMenuContent} from './menu-content';
3737
overlapTrigger: true,
3838
xPosition: 'after',
3939
yPosition: 'below',
40+
backdropClass: 'cdk-overlay-transparent-backdrop'
4041
},
4142
}
4243
],

src/lib/menu/menu-panel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ export interface MatMenuPanel {
2929
setPositionClasses: (x: MenuPositionX, y: MenuPositionY) => void;
3030
setElevation?(depth: number): void;
3131
lazyContent?: MatMenuContent;
32+
backdropClass?: string;
3233
}

src/lib/menu/menu-trigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
340340
return new OverlayConfig({
341341
positionStrategy: this._getPosition(),
342342
hasBackdrop: !this.triggersSubmenu(),
343-
backdropClass: 'cdk-overlay-transparent-backdrop',
343+
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
344344
direction: this.dir,
345345
scrollStrategy: this._scrollStrategy()
346346
});

src/lib/menu/menu.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ describe('MatMenu', () => {
130130
expect(document.activeElement).toBe(triggerEl);
131131
}));
132132

133+
it('should be able to set a custom class on the backdrop', fakeAsync(() => {
134+
const fixture = TestBed.createComponent(SimpleMenu);
135+
136+
fixture.componentInstance.backdropClass = 'custom-backdrop';
137+
fixture.detectChanges();
138+
fixture.componentInstance.trigger.openMenu();
139+
fixture.detectChanges();
140+
tick(500);
141+
142+
const backdrop = <HTMLElement>overlayContainerElement.querySelector('.cdk-overlay-backdrop');
143+
144+
expect(backdrop.classList).toContain('custom-backdrop');
145+
}));
146+
133147
it('should restore focus to the root trigger when the menu was opened by mouse', fakeAsync(() => {
134148
const fixture = TestBed.createComponent(SimpleMenu);
135149
fixture.detectChanges();
@@ -1336,7 +1350,12 @@ describe('MatMenu default overrides', () => {
13361350
@Component({
13371351
template: `
13381352
<button [matMenuTriggerFor]="menu" #triggerEl>Toggle menu</button>
1339-
<mat-menu class="custom-one custom-two" #menu="matMenu" (closed)="closeCallback($event)">
1353+
<mat-menu
1354+
#menu="matMenu"
1355+
class="custom-one custom-two"
1356+
(closed)="closeCallback($event)"
1357+
[backdropClass]="backdropClass">
1358+
13401359
<button mat-menu-item> Item </button>
13411360
<button mat-menu-item disabled> Disabled </button>
13421361
<button mat-menu-item disableRipple>
@@ -1352,6 +1371,7 @@ class SimpleMenu {
13521371
@ViewChild(MatMenu) menu: MatMenu;
13531372
@ViewChildren(MatMenuItem) items: QueryList<MatMenuItem>;
13541373
closeCallback = jasmine.createSpy('menu closed callback');
1374+
backdropClass: string;
13551375
}
13561376

13571377
@Component({

0 commit comments

Comments
 (0)