Skip to content

fix(bottom-sheet): close on page navigation #12106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/bottom-sheet/bottom-sheet-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ export class MatBottomSheetConfig<D = any> {

/** Aria label to assign to the bottom sheet element. */
ariaLabel?: string | null = null;

/** Whether the bottom sheet should close when the user goes backwards/forwards in history. */
closeOnNavigation?: boolean = true;
}
20 changes: 18 additions & 2 deletions src/lib/bottom-sheet/bottom-sheet-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Location} from '@angular/common';
import {ESCAPE} from '@angular/cdk/keycodes';
import {OverlayRef} from '@angular/cdk/overlay';
import {merge, Observable, Subject} from 'rxjs';
import {merge, Observable, Subject, SubscriptionLike, Subscription} from 'rxjs';
import {filter, take} from 'rxjs/operators';
import {MatBottomSheetContainer} from './bottom-sheet-container';

Expand All @@ -35,7 +36,13 @@ export class MatBottomSheetRef<T = any, R = any> {
/** Result to be passed down to the `afterDismissed` stream. */
private _result: R | undefined;

constructor(containerInstance: MatBottomSheetContainer, private _overlayRef: OverlayRef) {
/** Subscription to changes in the user's location. */
private _locationChanges: SubscriptionLike = Subscription.EMPTY;

constructor(
containerInstance: MatBottomSheetContainer,
private _overlayRef: OverlayRef,
location?: Location) {
this.containerInstance = containerInstance;

// Emit when opening animation completes
Expand All @@ -54,6 +61,7 @@ export class MatBottomSheetRef<T = any, R = any> {
take(1)
)
.subscribe(() => {
this._locationChanges.unsubscribe();
this._overlayRef.dispose();
this._afterDismissed.next(this._result);
this._afterDismissed.complete();
Expand All @@ -65,6 +73,14 @@ export class MatBottomSheetRef<T = any, R = any> {
_overlayRef.keydownEvents().pipe(filter(event => event.keyCode === ESCAPE))
).subscribe(() => this.dismiss());
}

if (location) {
this._locationChanges = location.subscribe(() => {
if (containerInstance.bottomSheetConfig.closeOnNavigation) {
this.dismiss();
}
});
}
}

/**
Expand Down
49 changes: 46 additions & 3 deletions src/lib/bottom-sheet/bottom-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
TestBed,
tick,
} from '@angular/core/testing';
import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {MatBottomSheet} from './bottom-sheet';
import {MAT_BOTTOM_SHEET_DATA, MatBottomSheetConfig} from './bottom-sheet-config';
Expand All @@ -36,19 +38,24 @@ describe('MatBottomSheet', () => {

let testViewContainerRef: ViewContainerRef;
let viewContainerFixture: ComponentFixture<ComponentWithChildViewContainer>;
let mockLocation: SpyLocation;

beforeEach(fakeAsync(() => {
TestBed
.configureTestingModule({imports: [MatBottomSheetModule, BottomSheetTestModule]})
.configureTestingModule({
imports: [MatBottomSheetModule, BottomSheetTestModule],
providers: [{provide: Location, useClass: SpyLocation}]
})
.compileComponents();
}));

beforeEach(inject([MatBottomSheet, OverlayContainer, ViewportRuler],
(bs: MatBottomSheet, oc: OverlayContainer, vr: ViewportRuler) => {
beforeEach(inject([MatBottomSheet, OverlayContainer, ViewportRuler, Location],
(bs: MatBottomSheet, oc: OverlayContainer, vr: ViewportRuler, l: Location) => {
bottomSheet = bs;
overlayContainer = oc;
viewportRuler = vr;
overlayContainerElement = oc.getContainerElement();
mockLocation = l as SpyLocation;
}));

afterEach(() => {
Expand Down Expand Up @@ -349,6 +356,42 @@ describe('MatBottomSheet', () => {
expect(spy).toHaveBeenCalledWith(1337);
}));

it('should close the bottom sheet when going forwards/backwards in history', fakeAsync(() => {
bottomSheet.open(PizzaMsg);

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();

mockLocation.simulateUrlPop('');
viewContainerFixture.detectChanges();
flush();

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));

it('should close the bottom sheet when the location hash changes', fakeAsync(() => {
bottomSheet.open(PizzaMsg);

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();

mockLocation.simulateHashChange('');
viewContainerFixture.detectChanges();
flush();

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeFalsy();
}));

it('should allow the consumer to disable closing a bottom sheet on navigation', fakeAsync(() => {
bottomSheet.open(PizzaMsg, {closeOnNavigation: false});

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();

mockLocation.simulateUrlPop('');
viewContainerFixture.detectChanges();
flush();

expect(overlayContainerElement.querySelector('mat-bottom-sheet-container')).toBeTruthy();
}));

describe('passing in data', () => {
it('should be able to pass in data', () => {
const config = {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/bottom-sheet/bottom-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Directionality} from '@angular/cdk/bidi';
import {Overlay, OverlayConfig, OverlayRef} from '@angular/cdk/overlay';
import {ComponentPortal, ComponentType, PortalInjector, TemplatePortal} from '@angular/cdk/portal';
import {ComponentRef, Injectable, Injector, Optional, SkipSelf, TemplateRef} from '@angular/core';
import {Location} from '@angular/common';
import {of as observableOf} from 'rxjs';
import {MAT_BOTTOM_SHEET_DATA, MatBottomSheetConfig} from './bottom-sheet-config';
import {MatBottomSheetContainer} from './bottom-sheet-container';
Expand Down Expand Up @@ -41,7 +42,8 @@ export class MatBottomSheet {
constructor(
private _overlay: Overlay,
private _injector: Injector,
@Optional() @SkipSelf() private _parentBottomSheet: MatBottomSheet) {}
@Optional() @SkipSelf() private _parentBottomSheet: MatBottomSheet,
@Optional() private _location?: Location) {}

open<T, D = any, R = any>(component: ComponentType<T>,
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T, R>;
Expand All @@ -54,7 +56,7 @@ export class MatBottomSheet {
const _config = _applyConfigDefaults(config);
const overlayRef = this._createOverlay(_config);
const container = this._attachContainer(overlayRef, _config);
const ref = new MatBottomSheetRef<T, R>(container, overlayRef);
const ref = new MatBottomSheetRef<T, R>(container, overlayRef, this._location);

if (componentOrTemplateRef instanceof TemplateRef) {
container.attachTemplatePortal(new TemplatePortal<T>(componentOrTemplateRef, null!, {
Expand Down