Skip to content

Commit

Permalink
feat(drawer): allow for backdrop to be disabled (#9381)
Browse files Browse the repository at this point in the history
Adds the `hasBackdrop` input to the drawer container, allowing users to disable the backdrop.

Fixes #5300.
  • Loading branch information
crisbeto authored and jelbourn committed Feb 13, 2018
1 parent 0383704 commit 6d4e052
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/sidenav/drawer-container.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()"
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()" *ngIf="hasBackdrop"
[class.mat-drawer-shown]="_isShowingBackdrop()"></div>

<ng-content select="mat-drawer"></ng-content>
Expand Down
18 changes: 16 additions & 2 deletions src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ describe('MatDrawerContainer', () => {
DrawerSetToOpenedTrue,
DrawerContainerStateChangesTestApp,
AutosizeDrawer,
BasicTestApp,
],
});

Expand Down Expand Up @@ -630,6 +631,18 @@ describe('MatDrawerContainer', () => {
discardPeriodicTasks();
}));

it('should be able to toggle whether the container has a backdrop', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicTestApp);
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('.mat-drawer-backdrop')).toBeTruthy();

fixture.componentInstance.hasBackdrop = false;
fixture.detectChanges();

expect(fixture.nativeElement.querySelector('.mat-drawer-backdrop')).toBeFalsy();
}));

});


Expand All @@ -652,13 +665,13 @@ class DrawerContainerTwoDrawerTestApp {
/** Test component that contains an MatDrawerContainer and one MatDrawer. */
@Component({
template: `
<mat-drawer-container (backdropClick)="backdropClicked()">
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
<mat-drawer #drawer position="start"
(opened)="open()"
(openedStart)="openStart()"
(closed)="close()"
(closedStart)="closeStart()">
<button #drawerButton>Content.</button>
<button #drawerButton>Content</button>
</mat-drawer>
<button (click)="drawer.open()" class="open" #openButton></button>
<button (click)="drawer.close()" class="close" #closeButton></button>
Expand All @@ -670,6 +683,7 @@ class BasicTestApp {
closeCount = 0;
closeStartCount = 0;
backdropClickedCount = 0;
hasBackdrop = true;

@ViewChild('drawerButton') drawerButton: ElementRef;
@ViewChild('openButton') openButton: ElementRef;
Expand Down
14 changes: 14 additions & 0 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
set autosize(value: boolean) { this._autosize = coerceBooleanProperty(value); }
private _autosize: boolean;

/** Whether the drawer container should have a backdrop while one of the sidenavs is open. */
@Input()
get hasBackdrop() {
if (this._hasBackdrop == null) {
return !this._start || this._start.mode !== 'side' || !this._end || this._end.mode !== 'side';
}

return this._hasBackdrop;
}
set hasBackdrop(value: any) {
this._hasBackdrop = value == null ? null : coerceBooleanProperty(value);
}
private _hasBackdrop: boolean | null;

/** Event emitted when the drawer backdrop is clicked. */
@Output() readonly backdropClick: EventEmitter<void> = new EventEmitter<void>();

Expand Down
2 changes: 1 addition & 1 deletion src/lib/sidenav/sidenav-container.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()"
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()" *ngIf="hasBackdrop"
[class.mat-drawer-shown]="_isShowingBackdrop()"></div>

<ng-content select="mat-sidenav"></ng-content>
Expand Down

0 comments on commit 6d4e052

Please sign in to comment.