Skip to content

Commit 4e2e87f

Browse files
committed
feat(menu): allow for backdrop to be disabled
Adds the `hasBackdrop` input on the `MatMenu` directive, allowing consumers to remove the backdrop. Fixes #9938.
1 parent 74116cf commit 4e2e87f

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/lib/menu/menu-directive.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ export class MatMenu implements OnInit, AfterContentInit, MatMenuPanel, OnDestro
145145
}
146146
private _overlapTrigger: boolean = this._defaultOptions.overlapTrigger;
147147

148+
/** Whether the menu has a backdrop. */
149+
@Input()
150+
get hasBackdrop(): boolean { return this._hasBackdrop; }
151+
set hasBackdrop(value: boolean) {
152+
this._hasBackdrop = coerceBooleanProperty(value);
153+
}
154+
private _hasBackdrop: boolean = this._defaultOptions.hasBackdrop;
155+
148156
/**
149157
* This method takes classes set on the host mat-menu element and applies them on the
150158
* 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
@@ -29,4 +29,5 @@ export interface MatMenuPanel {
2929
setPositionClasses: (x: MenuPositionX, y: MenuPositionY) => void;
3030
setElevation?(depth: number): void;
3131
lazyContent?: MatMenuContent;
32+
hasBackdrop?: boolean;
3233
}

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: '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)