Skip to content

Commit bf9a73e

Browse files
committed
feat(drawer): allow for backdrop to be disabled
Adds the `hasBackdrop` input to the drawer container, allowing users to disable the backdrop. Fixes #5300.
1 parent 60b0625 commit bf9a73e

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

src/lib/sidenav/drawer-container.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()"
1+
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()" *ngIf="hasBackdrop"
22
[class.mat-drawer-shown]="_isShowingBackdrop()"></div>
33

44
<ng-content select="mat-drawer"></ng-content>

src/lib/sidenav/drawer.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ describe('MatDrawerContainer', () => {
483483
DrawerSetToOpenedTrue,
484484
DrawerContainerStateChangesTestApp,
485485
AutosizeDrawer,
486+
BasicTestApp,
486487
],
487488
});
488489

@@ -630,6 +631,18 @@ describe('MatDrawerContainer', () => {
630631
discardPeriodicTasks();
631632
}));
632633

634+
it('should be able to toggle whether the container has a backdrop', fakeAsync(() => {
635+
const fixture = TestBed.createComponent(BasicTestApp);
636+
fixture.detectChanges();
637+
638+
expect(fixture.nativeElement.querySelector('.mat-drawer-backdrop')).toBeTruthy();
639+
640+
fixture.componentInstance.hasBackdrop = false;
641+
fixture.detectChanges();
642+
643+
expect(fixture.nativeElement.querySelector('.mat-drawer-backdrop')).toBeFalsy();
644+
}));
645+
633646
});
634647

635648

@@ -652,13 +665,13 @@ class DrawerContainerTwoDrawerTestApp {
652665
/** Test component that contains an MatDrawerContainer and one MatDrawer. */
653666
@Component({
654667
template: `
655-
<mat-drawer-container (backdropClick)="backdropClicked()">
668+
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
656669
<mat-drawer #drawer position="start"
657670
(opened)="open()"
658671
(openedStart)="openStart()"
659672
(closed)="close()"
660673
(closedStart)="closeStart()">
661-
<button #drawerButton>Content.</button>
674+
<button #drawerButton>Content</button>
662675
</mat-drawer>
663676
<button (click)="drawer.open()" class="open" #openButton></button>
664677
<button (click)="drawer.close()" class="close" #closeButton></button>
@@ -670,6 +683,7 @@ class BasicTestApp {
670683
closeCount = 0;
671684
closeStartCount = 0;
672685
backdropClickedCount = 0;
686+
hasBackdrop = true;
673687

674688
@ViewChild('drawerButton') drawerButton: ElementRef;
675689
@ViewChild('openButton') openButton: ElementRef;

src/lib/sidenav/drawer.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,20 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
446446
set autosize(value: boolean) { this._autosize = coerceBooleanProperty(value); }
447447
private _autosize: boolean;
448448

449+
/** Whether the drawer container should have a backdrop while one of the sidenavs is open. */
450+
@Input()
451+
get hasBackdrop() {
452+
if (this._hasBackdrop == null) {
453+
return !this._start || this._start.mode !== 'side' || !this._end || this._end.mode !== 'side';
454+
}
455+
456+
return this._hasBackdrop;
457+
}
458+
set hasBackdrop(value: any) {
459+
this._hasBackdrop = value == null ? null : coerceBooleanProperty(value);
460+
}
461+
private _hasBackdrop: boolean | null;
462+
449463
/** Event emitted when the drawer backdrop is clicked. */
450464
@Output() backdropClick = new EventEmitter<void>();
451465

@@ -651,8 +665,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
651665
}
652666

653667
_isShowingBackdrop(): boolean {
654-
return (this._isDrawerOpen(this._start) && this._start!.mode != 'side')
655-
|| (this._isDrawerOpen(this._end) && this._end!.mode != 'side');
668+
return this._isDrawerOpen(this._start) || this._isDrawerOpen(this._end);
656669
}
657670

658671
private _isDrawerOpen(drawer: MatDrawer | null): boolean {

src/lib/sidenav/sidenav-container.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()"
1+
<div class="mat-drawer-backdrop" (click)="_onBackdropClicked()" *ngIf="hasBackdrop"
22
[class.mat-drawer-shown]="_isShowingBackdrop()"></div>
33

44
<ng-content select="mat-sidenav"></ng-content>

0 commit comments

Comments
 (0)