Skip to content

Commit

Permalink
feat(menu): allow for backdrop class to be customized (#10097)
Browse files Browse the repository at this point in the history
Allows for the backdrop class of a menu to be overwritten.

Fixes #10062.
  • Loading branch information
crisbeto authored and jelbourn committed Feb 26, 2018
1 parent cdd224a commit 2ece035
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/lib/menu/menu-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export interface MatMenuDefaultOptions {

/** Whether the menu should overlap the menu trigger. */
overlapTrigger: boolean;

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

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

/** Class to be added to the backdrop element. */
@Input() backdropClass: string = this._defaultOptions.backdropClass;

/** Position of the menu in the X axis. */
@Input()
get xPosition(): MenuPositionX { return this._xPosition; }
Expand Down
1 change: 1 addition & 0 deletions src/lib/menu/menu-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {MatMenuContent} from './menu-content';
overlapTrigger: true,
xPosition: 'after',
yPosition: 'below',
backdropClass: 'cdk-overlay-transparent-backdrop'
},
}
],
Expand Down
1 change: 1 addition & 0 deletions src/lib/menu/menu-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export interface MatMenuPanel {
setPositionClasses: (x: MenuPositionX, y: MenuPositionY) => void;
setElevation?(depth: number): void;
lazyContent?: MatMenuContent;
backdropClass?: string;
}
2 changes: 1 addition & 1 deletion src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
return new OverlayConfig({
positionStrategy: this._getPosition(),
hasBackdrop: !this.triggersSubmenu(),
backdropClass: 'cdk-overlay-transparent-backdrop',
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
direction: this.dir,
scrollStrategy: this._scrollStrategy()
});
Expand Down
22 changes: 21 additions & 1 deletion src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ describe('MatMenu', () => {
expect(document.activeElement).toBe(triggerEl);
}));

it('should be able to set a custom class on the backdrop', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleMenu);

fixture.componentInstance.backdropClass = 'custom-backdrop';
fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
tick(500);

const backdrop = <HTMLElement>overlayContainerElement.querySelector('.cdk-overlay-backdrop');

expect(backdrop.classList).toContain('custom-backdrop');
}));

it('should restore focus to the root trigger when the menu was opened by mouse', fakeAsync(() => {
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();
Expand Down Expand Up @@ -1336,7 +1350,12 @@ describe('MatMenu default overrides', () => {
@Component({
template: `
<button [matMenuTriggerFor]="menu" #triggerEl>Toggle menu</button>
<mat-menu class="custom-one custom-two" #menu="matMenu" (closed)="closeCallback($event)">
<mat-menu
#menu="matMenu"
class="custom-one custom-two"
(closed)="closeCallback($event)"
[backdropClass]="backdropClass">
<button mat-menu-item> Item </button>
<button mat-menu-item disabled> Disabled </button>
<button mat-menu-item disableRipple>
Expand All @@ -1352,6 +1371,7 @@ class SimpleMenu {
@ViewChild(MatMenu) menu: MatMenu;
@ViewChildren(MatMenuItem) items: QueryList<MatMenuItem>;
closeCallback = jasmine.createSpy('menu closed callback');
backdropClass: string;
}

@Component({
Expand Down

0 comments on commit 2ece035

Please sign in to comment.