Skip to content

Commit 268b0e8

Browse files
crisbetojelbourn
authored andcommittedDec 19, 2018
fix(menu): hasBackdrop not being updated after first open (#14561)
Fixes changing the `hasBackdrop` value not having an effect if it happens after the first time a menu is opened. Fixes #14560.
1 parent 2463200 commit 268b0e8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
 

‎src/lib/menu/menu-trigger.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
226226
this._checkMenu();
227227

228228
const overlayRef = this._createOverlay();
229-
this._setPosition(overlayRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy);
229+
const overlayConfig = overlayRef.getConfig();
230+
231+
this._setPosition(overlayConfig.positionStrategy as FlexibleConnectedPositionStrategy);
232+
overlayConfig.hasBackdrop = this.menu.hasBackdrop == null ? !this.triggersSubmenu() :
233+
this.menu.hasBackdrop;
230234
overlayRef.attach(this._getPortal());
231235

232236
if (this.menu.lazyContent) {
@@ -394,7 +398,6 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
394398
.flexibleConnectedTo(this._element)
395399
.withLockedPosition()
396400
.withTransformOriginOn('.mat-menu-panel'),
397-
hasBackdrop: this.menu.hasBackdrop == null ? !this.triggersSubmenu() : this.menu.hasBackdrop,
398401
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
399402
scrollStrategy: this._scrollStrategy(),
400403
direction: this._dir

‎src/lib/menu/menu.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ describe('MatMenu', () => {
114114
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
115115
}));
116116

117+
it('should be able to remove the backdrop on repeat openings', fakeAsync(() => {
118+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
119+
fixture.detectChanges();
120+
121+
fixture.componentInstance.trigger.openMenu();
122+
fixture.detectChanges();
123+
tick(500);
124+
125+
// Start off with a backdrop.
126+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
127+
128+
fixture.componentInstance.trigger.closeMenu();
129+
fixture.detectChanges();
130+
tick(500);
131+
132+
// Change `hasBackdrop` after the first open.
133+
fixture.componentInstance.menu.hasBackdrop = false;
134+
fixture.detectChanges();
135+
136+
// Reopen the menu.
137+
fixture.componentInstance.trigger.openMenu();
138+
fixture.detectChanges();
139+
tick(500);
140+
141+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
142+
}));
143+
117144
it('should restore focus to the trigger when the menu was opened by keyboard', fakeAsync(() => {
118145
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
119146
fixture.detectChanges();

0 commit comments

Comments
 (0)
Please sign in to comment.