Skip to content

Commit

Permalink
feat(menu): allow for backdrop to be disabled (#10070)
Browse files Browse the repository at this point in the history
Adds the `hasBackdrop` input on the `MatMenu` directive, allowing consumers to remove the backdrop.

Fixes #9938.
  • Loading branch information
crisbeto authored and tinayuangao committed Feb 28, 2018
1 parent b53002c commit e42f0bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/lib/menu/menu-directive.ts
Expand Up @@ -58,6 +58,9 @@ export interface MatMenuDefaultOptions {

/** Class to be applied to the menu's backdrop. */
backdropClass: string;

/** Whether the menu has a backdrop. */
hasBackdrop: boolean;
}

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

/** Whether the menu has a backdrop. */
@Input()
get hasBackdrop(): boolean { return this._hasBackdrop; }
set hasBackdrop(value: boolean) {
this._hasBackdrop = coerceBooleanProperty(value);
}
private _hasBackdrop: boolean = this._defaultOptions.hasBackdrop;

/**
* This method takes classes set on the host mat-menu element and applies them on the
* menu template that displays in the overlay container. Otherwise, it's difficult
Expand Down
1 change: 1 addition & 0 deletions src/lib/menu/menu-panel.ts
Expand Up @@ -30,4 +30,5 @@ export interface MatMenuPanel {
setElevation?(depth: number): void;
lazyContent?: MatMenuContent;
backdropClass?: string;
hasBackdrop?: boolean;
}
2 changes: 1 addition & 1 deletion src/lib/menu/menu-trigger.ts
Expand Up @@ -339,7 +339,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
private _getOverlayConfig(): OverlayConfig {
return new OverlayConfig({
positionStrategy: this._getPosition(),
hasBackdrop: !this.triggersSubmenu(),
hasBackdrop: this.menu.hasBackdrop == null ? !this.triggersSubmenu() : this.menu.hasBackdrop,
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
direction: this.dir,
scrollStrategy: this._scrollStrategy()
Expand Down
12 changes: 12 additions & 0 deletions src/lib/menu/menu.spec.ts
Expand Up @@ -111,6 +111,18 @@ describe('MatMenu', () => {
expect(overlayContainerElement.textContent).toBe('');
}));

it('should be able to remove the backdrop', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();

fixture.componentInstance.menu.hasBackdrop = false;
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
tick(500);

expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
}));

it('should restore focus to the trigger when the menu was opened by keyboard', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();
Expand Down

0 comments on commit e42f0bc

Please sign in to comment.