Skip to content

Commit 61d3cf8

Browse files
crisbetojelbourn
authored andcommittedDec 19, 2018
fix(overlay): not updating hasBackdrop after first open (#14562)
Along the same lines as #14561. The connected overlay directive has an input on whether it should have a backdrop, however we only use it the first time we create the backdrop.
1 parent 268b0e8 commit 61d3cf8

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed
 

Diff for: ‎src/cdk/overlay/overlay-directives.spec.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component, ViewChild} from '@angular/core';
22
import {By} from '@angular/platform-browser';
3-
import {ComponentFixture, TestBed, async, inject} from '@angular/core/testing';
3+
import {ComponentFixture, TestBed, async, inject, fakeAsync, tick} from '@angular/core/testing';
44
import {Directionality} from '@angular/cdk/bidi';
55
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
66
import {ESCAPE, A} from '@angular/cdk/keycodes';
@@ -210,18 +210,37 @@ describe('Overlay directives', () => {
210210
fixture.componentInstance.isOpen = true;
211211
fixture.detectChanges();
212212

213-
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
214-
expect(backdrop).toBeTruthy();
213+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
215214
});
216215

217216
it('should not create the backdrop by default', () => {
218217
fixture.componentInstance.isOpen = true;
219218
fixture.detectChanges();
220219

221-
let backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
222-
expect(backdrop).toBeNull();
220+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeNull();
223221
});
224222

223+
it('should be able to change hasBackdrop after the overlay has been initialized',
224+
fakeAsync(() => {
225+
// Open once with a backdrop
226+
fixture.componentInstance.hasBackdrop = true;
227+
fixture.componentInstance.isOpen = true;
228+
fixture.detectChanges();
229+
230+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
231+
232+
fixture.componentInstance.isOpen = false;
233+
fixture.detectChanges();
234+
tick(500);
235+
236+
// Open again without a backdrop.
237+
fixture.componentInstance.hasBackdrop = false;
238+
fixture.componentInstance.isOpen = true;
239+
fixture.detectChanges();
240+
241+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
242+
}));
243+
225244
it('should set the custom backdrop class', () => {
226245
fixture.componentInstance.hasBackdrop = true;
227246
fixture.componentInstance.isOpen = true;

Diff for: ‎src/cdk/overlay/overlay-directives.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
237237
}
238238

239239
ngOnDestroy() {
240-
this._destroyOverlay();
240+
if (this._overlayRef) {
241+
this._overlayRef.dispose();
242+
}
243+
244+
this._backdropSubscription.unsubscribe();
241245
}
242246

243247
ngOnChanges(changes: SimpleChanges) {
@@ -351,6 +355,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
351355
height: this.height,
352356
minHeight: this.minHeight,
353357
});
358+
359+
this._overlayRef.getConfig().hasBackdrop = this.hasBackdrop;
354360
}
355361

356362
if (!this._overlayRef.hasAttached()) {
@@ -362,6 +368,8 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
362368
this._backdropSubscription = this._overlayRef.backdropClick().subscribe(event => {
363369
this.backdropClick.emit(event);
364370
});
371+
} else {
372+
this._backdropSubscription.unsubscribe();
365373
}
366374
}
367375

@@ -374,15 +382,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
374382

375383
this._backdropSubscription.unsubscribe();
376384
}
377-
378-
/** Destroys the overlay created by this directive. */
379-
private _destroyOverlay() {
380-
if (this._overlayRef) {
381-
this._overlayRef.dispose();
382-
}
383-
384-
this._backdropSubscription.unsubscribe();
385-
}
386385
}
387386

388387

0 commit comments

Comments
 (0)
Please sign in to comment.