Skip to content
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

feat(sidenav): add disableClose option #2501

Merged
merged 2 commits into from
Jan 13, 2017
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
52 changes: 50 additions & 2 deletions src/lib/sidenav/sidenav.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('MdSidenav', () => {
}).not.toThrow();
}));

it('should emit the backdrop-clicked event when the backdrop is clicked', fakeAsync(() => {
it('should emit the backdropClick event when the backdrop is clicked', fakeAsync(() => {
let fixture = TestBed.createComponent(BasicTestApp);

let testComponent: BasicTestApp = fixture.debugElement.componentInstance;
Expand Down Expand Up @@ -264,6 +264,54 @@ describe('MdSidenav', () => {
expect(testComponent.closeCount).toBe(1);
}));

it('should not close by pressing escape when disableClose is set', fakeAsync(() => {
let fixture = TestBed.createComponent(BasicTestApp);
let testComponent = fixture.debugElement.componentInstance;
let sidenav = fixture.debugElement.query(By.directive(MdSidenav)).componentInstance;

sidenav.disableClose = true;
sidenav.open();

fixture.detectChanges();
endSidenavTransition(fixture);
tick();

sidenav.handleKeydown({
keyCode: ESCAPE,
stopPropagation: () => {}
});

fixture.detectChanges();
endSidenavTransition(fixture);
tick();

expect(testComponent.closeCount).toBe(0);
}));

it('should not close by clicking on the backdrop when disableClose is set', fakeAsync(() => {
let fixture = TestBed.createComponent(BasicTestApp);
let testComponent = fixture.debugElement.componentInstance;
let sidenav = fixture.debugElement.query(By.directive(MdSidenav)).componentInstance;

sidenav.disableClose = true;
sidenav.open();

fixture.detectChanges();
endSidenavTransition(fixture);
tick();

let backdropEl = fixture.debugElement.query(By.css('.md-sidenav-backdrop')).nativeElement;
backdropEl.click();
fixture.detectChanges();
tick();

fixture.detectChanges();
endSidenavTransition(fixture);
tick();

expect(testComponent.closeCount).toBe(0);
}));

it('should restore focus to the trigger element on close', fakeAsync(() => {
let fixture = TestBed.createComponent(BasicTestApp);
let sidenav: MdSidenav = fixture.debugElement
Expand Down Expand Up @@ -414,7 +462,7 @@ class SidenavContainerTwoSidenavTestApp { }
/** Test component that contains an MdSidenavContainer and one MdSidenav. */
@Component({
template: `
<md-sidenav-container (backdrop-clicked)="backdropClicked()">
<md-sidenav-container (backdropClick)="backdropClicked()">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxime1992 The solution to our problem : backdropClick

<md-sidenav #sidenav align="start"
(open-start)="openStart()"
(open)="open()"
Expand Down
22 changes: 13 additions & 9 deletions src/lib/sidenav/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ export class MdSidenav implements AfterContentInit {
/** Mode of the sidenav; whether 'over' or 'side'. */
@Input() mode: 'over' | 'push' | 'side' = 'over';

/** Whether the sidenav can be closed with the escape key or not. */
@Input()
get disableClose(): boolean { return this._disableClose; }
set disableClose(value: boolean) { this._disableClose = coerceBooleanProperty(value); }
private _disableClose: boolean = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disableClose should also prevent close from backdrop clicks.

While you're doing this, can you also change the @Output to backdropClick? Looks like we missed it when getting rid of dash-case stuff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jelbourn Made the changes you requested.


/** Whether the sidenav is opened. */
_opened: boolean = false;

Expand Down Expand Up @@ -233,7 +239,7 @@ export class MdSidenav implements AfterContentInit {
* @docs-private
*/
handleKeydown(event: KeyboardEvent) {
if (event.keyCode === ESCAPE) {
if (event.keyCode === ESCAPE && !this.disableClose) {
this.close();
event.stopPropagation();
}
Expand Down Expand Up @@ -328,7 +334,7 @@ export class MdSidenavContainer implements AfterContentInit {
get end() { return this._end; }

/** Event emitted when the sidenav backdrop is clicked. */
@Output('backdrop-clicked') onBackdropClicked = new EventEmitter<void>();
@Output() backdropClick = new EventEmitter<void>();

/** The sidenav at the start/end alignment, independent of direction. */
private _start: MdSidenav;
Expand Down Expand Up @@ -435,17 +441,15 @@ export class MdSidenavContainer implements AfterContentInit {
}

_onBackdropClicked() {
this.onBackdropClicked.emit();
this.backdropClick.emit();
this._closeModalSidenav();
}

_closeModalSidenav() {
if (this._start != null && this._start.mode != 'side') {
this._start.close();
}
if (this._end != null && this._end.mode != 'side') {
this._end.close();
}
// Close all open sidenav's where closing is not disabled and the mode is not `side`.
[this._start, this._end]
.filter(sidenav => sidenav && !sidenav.disableClose && sidenav.mode !== 'side')
.forEach(sidenav => sidenav.close());
}

_isShowingBackdrop(): boolean {
Expand Down