Skip to content

Commit e42f0bc

Browse files
crisbetotinayuangao
authored andcommitted
feat(menu): allow for backdrop to be disabled (#10070)
Adds the `hasBackdrop` input on the `MatMenu` directive, allowing consumers to remove the backdrop. Fixes #9938.
1 parent b53002c commit e42f0bc

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

src/lib/menu/menu-directive.ts

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

5959
/** Class to be applied to the menu's backdrop. */
6060
backdropClass: string;
61+
62+
/** Whether the menu has a backdrop. */
63+
hasBackdrop: boolean;
6164
}
6265

6366
/** Injection token to be used to override the default options for `mat-menu`. */
@@ -151,6 +154,14 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro
151154
}
152155
private _overlapTrigger: boolean = this._defaultOptions.overlapTrigger;
153156

157+
/** Whether the menu has a backdrop. */
158+
@Input()
159+
get hasBackdrop(): boolean { return this._hasBackdrop; }
160+
set hasBackdrop(value: boolean) {
161+
this._hasBackdrop = coerceBooleanProperty(value);
162+
}
163+
private _hasBackdrop: boolean = this._defaultOptions.hasBackdrop;
164+
154165
/**
155166
* This method takes classes set on the host mat-menu element and applies them on the
156167
* menu template that displays in the overlay container. Otherwise, it's difficult

src/lib/menu/menu-panel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export interface MatMenuPanel {
3030
setElevation?(depth: number): void;
3131
lazyContent?: MatMenuContent;
3232
backdropClass?: string;
33+
hasBackdrop?: boolean;
3334
}

src/lib/menu/menu-trigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
339339
private _getOverlayConfig(): OverlayConfig {
340340
return new OverlayConfig({
341341
positionStrategy: this._getPosition(),
342-
hasBackdrop: !this.triggersSubmenu(),
342+
hasBackdrop: this.menu.hasBackdrop == null ? !this.triggersSubmenu() : this.menu.hasBackdrop,
343343
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
344344
direction: this.dir,
345345
scrollStrategy: this._scrollStrategy()

src/lib/menu/menu.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ describe('MatMenu', () => {
111111
expect(overlayContainerElement.textContent).toBe('');
112112
}));
113113

114+
it('should be able to remove the backdrop', fakeAsync(() => {
115+
const fixture = TestBed.createComponent(SimpleMenu);
116+
fixture.detectChanges();
117+
118+
fixture.componentInstance.menu.hasBackdrop = false;
119+
fixture.componentInstance.trigger.openMenu();
120+
fixture.detectChanges();
121+
tick(500);
122+
123+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
124+
}));
125+
114126
it('should restore focus to the trigger when the menu was opened by keyboard', fakeAsync(() => {
115127
const fixture = TestBed.createComponent(SimpleMenu);
116128
fixture.detectChanges();

0 commit comments

Comments
 (0)