Skip to content

fix(drawer): show backdrop in "side" mode if hasBackdrop is explicitly set to true #10251

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
Mar 6, 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: 2 additions & 1 deletion src/demo-app/sidenav/sidenav-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<div class="demo-sidenav-area" *ngIf="isLaunched">
<mat-toolbar *ngIf="showHeader && !coverHeader">Header</mat-toolbar>
<mat-sidenav-container>
<mat-sidenav-container [hasBackdrop]="hasBackdrop">
<mat-sidenav #start (open)="myinput.focus()" [mode]="mode"
[fixedInViewport]="fixed" [fixedTopGap]="fixedTop" [fixedBottomGap]="fixedBottom">
Start Side Sidenav
Expand Down Expand Up @@ -41,6 +41,7 @@
<h3>Sidenav</h3>
<button mat-button (click)="start.toggle(undefined, 'mouse')">Toggle Start Side Sidenav</button>
<button mat-button (click)="end.toggle(undefined, 'mouse')">Toggle End Side Sidenav</button>
<mat-checkbox [(ngModel)]="hasBackdrop">Has backdrop</mat-checkbox>
<mat-checkbox [(ngModel)]="fixed">Fixed mode</mat-checkbox>
<mat-checkbox [(ngModel)]="coverHeader">Sidenav covers header/footer</mat-checkbox>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/demo-app/sidenav/sidenav-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class SidenavDemo {
showHeader = false;
showFooter = false;
modeIndex = 0;
hasBackdrop: boolean;
get mode() { return ['side', 'over', 'push'][this.modeIndex]; }
get fixedTop() { return this.fixed && this.showHeader && !this.coverHeader ? 64 : 0; }
get fixedBottom() { return this.fixed && this.showFooter && !this.coverHeader ? 64 : 0; }
Expand Down
6 changes: 6 additions & 0 deletions src/lib/sidenav/drawer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ $mat-drawer-over-drawer-z-index: 4;
overflow: hidden;
}
}

// When the consumer explicitly enabled the backdrop,
// we have to pull the side drawers above it.
&.mat-drawer-container-explicit-backdrop .mat-drawer-side {
z-index: $mat-drawer-backdrop-z-index;
}
}

.mat-drawer-backdrop {
Expand Down
34 changes: 32 additions & 2 deletions src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,35 @@ describe('MatDrawerContainer', () => {
expect(fixture.nativeElement.querySelector('.mat-drawer-backdrop')).toBeFalsy();
}));

it('should be able to explicitly enable the backdrop in `side` mode', fakeAsync(() => {
const fixture = TestBed.createComponent(BasicTestApp);
const root = fixture.nativeElement;

fixture.componentInstance.drawer.mode = 'side';
fixture.detectChanges();
fixture.componentInstance.drawer.open();
fixture.detectChanges();
tick();
fixture.detectChanges();

let backdrop = root.querySelector('.mat-drawer-backdrop.mat-drawer-shown');

expect(backdrop).toBeFalsy();

fixture.componentInstance.hasBackdrop = true;
fixture.detectChanges();
backdrop = root.querySelector('.mat-drawer-backdrop.mat-drawer-shown');

expect(backdrop).toBeTruthy();
expect(fixture.componentInstance.drawer.opened).toBe(true);

backdrop.click();
fixture.detectChanges();
tick();

expect(fixture.componentInstance.drawer.opened).toBe(false);
}));

});


Expand All @@ -666,7 +695,7 @@ class DrawerContainerTwoDrawerTestApp {
@Component({
template: `
<mat-drawer-container (backdropClick)="backdropClicked()" [hasBackdrop]="hasBackdrop">
<mat-drawer #drawer position="start"
<mat-drawer #drawer="matDrawer" position="start"
(opened)="open()"
(openedStart)="openStart()"
(closed)="close()"
Expand All @@ -683,8 +712,9 @@ class BasicTestApp {
closeCount = 0;
closeStartCount = 0;
backdropClickedCount = 0;
hasBackdrop = true;
hasBackdrop: boolean | null = null;

@ViewChild('drawer') drawer: MatDrawer;
@ViewChild('drawerButton') drawerButton: ElementRef;
@ViewChild('openButton') openButton: ElementRef;
@ViewChild('closeButton') closeButton: ElementRef;
Expand Down
31 changes: 20 additions & 11 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ export class MatDrawer implements AfterContentInit, AfterContentChecked, OnDestr
styleUrls: ['drawer.css'],
host: {
'class': 'mat-drawer-container',
'[class.mat-drawer-container-explicit-backdrop]': '_backdropOverride',
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
Expand Down Expand Up @@ -458,19 +459,23 @@ 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. */
/**
* Whether the drawer container should have a backdrop while one of the sidenavs is open.
* If explicitly set to `true`, the backdrop will be enabled for drawers in the `side`
* mode as well.
*/
@Input()
get hasBackdrop() {
if (this._hasBackdrop == null) {
if (this._backdropOverride == null) {
return !this._start || this._start.mode !== 'side' || !this._end || this._end.mode !== 'side';
}

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

/** Event emitted when the drawer backdrop is clicked. */
@Output() readonly backdropClick: EventEmitter<void> = new EventEmitter<void>();
Expand Down Expand Up @@ -663,8 +668,8 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {

/** Whether the container is being pushed to the side by one of the drawers. */
private _isPushed() {
return (this._isDrawerOpen(this._start) && this._start!.mode != 'over') ||
(this._isDrawerOpen(this._end) && this._end!.mode != 'over');
return (this._isDrawerOpen(this._start) && this._start.mode != 'over') ||
(this._isDrawerOpen(this._end) && this._end.mode != 'over');
}

_onBackdropClicked() {
Expand All @@ -675,16 +680,20 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
_closeModalDrawer() {
// Close all open drawers where closing is not disabled and the mode is not `side`.
[this._start, this._end]
.filter(drawer => drawer && !drawer.disableClose && drawer.mode !== 'side')
.filter(drawer => drawer && !drawer.disableClose && this._canHaveBackdrop(drawer))
.forEach(drawer => drawer!.close());
}

_isShowingBackdrop(): boolean {
return (this._isDrawerOpen(this._start) && this._start!.mode != 'side')
|| (this._isDrawerOpen(this._end) && this._end!.mode != 'side');
return (this._isDrawerOpen(this._start) && this._canHaveBackdrop(this._start)) ||
(this._isDrawerOpen(this._end) && this._canHaveBackdrop(this._end));
}

private _canHaveBackdrop(drawer: MatDrawer): boolean {
return drawer.mode !== 'side' || !!this._backdropOverride;
}

private _isDrawerOpen(drawer: MatDrawer | null): boolean {
private _isDrawerOpen(drawer: MatDrawer | null): drawer is MatDrawer {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a mistake or some crazy typescript thing I don't know about?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a crazy TS thing. It lets TS know that from now on the drawer is going to be a MatDrawer rather than a MatDrawer | null.

Copy link
Contributor

Choose a reason for hiding this comment

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

very cool

return drawer != null && drawer.opened;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/sidenav/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class MatSidenav extends MatDrawer {
styleUrls: ['drawer.css'],
host: {
'class': 'mat-drawer-container mat-sidenav-container',
'[class.mat-drawer-container-explicit-backdrop]': '_backdropOverride',
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
Expand Down