Skip to content

Commit 6d4e052

Browse files
crisbetojelbourn
authored andcommitted
feat(drawer): allow for backdrop to be disabled (#9381)
Adds the `hasBackdrop` input to the drawer container, allowing users to disable the backdrop. Fixes #5300.
1 parent 0383704 commit 6d4e052

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,20 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
454454
set autosize(value: boolean) { this._autosize = coerceBooleanProperty(value); }
455455
private _autosize: boolean;
456456

457+
/** Whether the drawer container should have a backdrop while one of the sidenavs is open. */
458+
@Input()
459+
get hasBackdrop() {
460+
if (this._hasBackdrop == null) {
461+
return !this._start || this._start.mode !== 'side' || !this._end || this._end.mode !== 'side';
462+
}
463+
464+
return this._hasBackdrop;
465+
}
466+
set hasBackdrop(value: any) {
467+
this._hasBackdrop = value == null ? null : coerceBooleanProperty(value);
468+
}
469+
private _hasBackdrop: boolean | null;
470+
457471
/** Event emitted when the drawer backdrop is clicked. */
458472
@Output() readonly backdropClick: EventEmitter<void> = new EventEmitter<void>();
459473

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)