Skip to content

Commit

Permalink
fix(menu): hasBackdrop not being updated after first open (#14561)
Browse files Browse the repository at this point in the history
Fixes changing the `hasBackdrop` value not having an effect if it happens after the first time a menu is opened.

Fixes #14560.
  • Loading branch information
crisbeto authored and jelbourn committed Dec 19, 2018
1 parent 2463200 commit 268b0e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
this._checkMenu();

const overlayRef = this._createOverlay();
this._setPosition(overlayRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy);
const overlayConfig = overlayRef.getConfig();

this._setPosition(overlayConfig.positionStrategy as FlexibleConnectedPositionStrategy);
overlayConfig.hasBackdrop = this.menu.hasBackdrop == null ? !this.triggersSubmenu() :
this.menu.hasBackdrop;
overlayRef.attach(this._getPortal());

if (this.menu.lazyContent) {
Expand Down Expand Up @@ -394,7 +398,6 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
.flexibleConnectedTo(this._element)
.withLockedPosition()
.withTransformOriginOn('.mat-menu-panel'),
hasBackdrop: this.menu.hasBackdrop == null ? !this.triggersSubmenu() : this.menu.hasBackdrop,
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
scrollStrategy: this._scrollStrategy(),
direction: this._dir
Expand Down
27 changes: 27 additions & 0 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,33 @@ describe('MatMenu', () => {
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
}));

it('should be able to remove the backdrop on repeat openings', fakeAsync(() => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();

fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
tick(500);

// Start off with a backdrop.
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();

fixture.componentInstance.trigger.closeMenu();
fixture.detectChanges();
tick(500);

// Change `hasBackdrop` after the first open.
fixture.componentInstance.menu.hasBackdrop = false;
fixture.detectChanges();

// Reopen the menu.
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 = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
Expand Down

0 comments on commit 268b0e8

Please sign in to comment.