Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/lib/sidenav/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Here's a simple example of using the sidenav:
```html
<app>
<mat-sidenav-container>
<mat-sidenav #start (open)="closeStartButton.focus()">
<mat-sidenav #start (openedChange)="closeStartButton.focus()">
Start Sidenav.
<br>
<button mat-button #closeStartButton (click)="start.close()">Close</button>
Expand Down
17 changes: 8 additions & 9 deletions src/lib/sidenav/drawer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,7 @@ class DrawerContainerTwoDrawerTestApp {
template: `
<mat-drawer-container (backdropClick)="backdropClicked()">
<mat-drawer #drawer position="start"
(open)="open()"
(close)="close()">
(openedChange)="open($event)">
<button #drawerButton>Content.</button>
</mat-drawer>
<button (click)="drawer.open()" class="open" #openButton></button>
Expand All @@ -458,12 +457,12 @@ class BasicTestApp {
@ViewChild('openButton') openButton: ElementRef;
@ViewChild('closeButton') closeButton: ElementRef;

open() {
this.openCount++;
}

close() {
this.closeCount++;
open($event) {
if ($event.type === 'open') {
this.openCount++;
} else {
this.closeCount++;
}
}

backdropClicked() {
Expand All @@ -484,7 +483,7 @@ class DrawerSetToOpenedFalse { }
@Component({
template: `
<mat-drawer-container>
<mat-drawer #drawer mode="side" opened="true" (open)="openCallback()">
<mat-drawer #drawer mode="side" opened="true" (openedChange)="openCallback()">
Closed Drawer.
</mat-drawer>
</mat-drawer-container>`,
Expand Down
77 changes: 47 additions & 30 deletions src/lib/sidenav/drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,18 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
private _currentTogglePromise: Promise<MatDrawerToggleResult> | null;

/** Event emitted when the drawer is fully opened. */
@Output('open') onOpen = new EventEmitter<MatDrawerToggleResult | void>();
@Output() openedChange = new EventEmitter<MatDrawerToggleResult | void>();

/** Event emitted when the drawer is fully closed. */
/**
* Event emitted when the drawer is fully opened.
* @deprecated Use `openedChange` instead.
*/
@Output('open') onOpen = this.openedChange;

/**
* Event emitted when the drawer is fully closed.
* @deprecated Use `openedChange` instead.
*/
@Output('close') onClose = new EventEmitter<MatDrawerToggleResult | void>();

/** Event emitted when the drawer's position changes. */
Expand All @@ -210,32 +219,34 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
*/
_modeChanged = new Subject();

get _isFocusTrapEnabled() {
get isFocusTrapEnabled(): boolean {
// The focus trap is only enabled when the drawer is open in any mode other than side.
return this.opened && this.mode !== 'side';
}

constructor(private _elementRef: ElementRef,
private _focusTrapFactory: FocusTrapFactory,
@Optional() @Inject(DOCUMENT) private _doc: any) {
this.onOpen.subscribe(() => {
if (this._doc) {
this._elementFocusedBeforeDrawerWasOpened = this._doc.activeElement as HTMLElement;
}
this.openedChange.subscribe(($event) => {
if ($event.type === 'open') {
if (this._doc) {
this._elementFocusedBeforeDrawerWasOpened = this._doc.activeElement as HTMLElement;
}

if (this._isFocusTrapEnabled && this._focusTrap) {
this._focusTrap.focusInitialElementWhenReady();
if (this.isFocusTrapEnabled && this._focusTrap) {
this._focusTrap.focusInitialElementWhenReady();
}
} else {
this._restoreFocus();
}
});

this.onClose.subscribe(() => this._restoreFocus());
}

/**
* If focus is currently inside the drawer, restores it to where it was before the drawer
* opened.
*/
private _restoreFocus() {
private _restoreFocus(): void {
let activeEl = this._doc && this._doc.activeElement;
if (activeEl && this._elementRef.nativeElement.contains(activeEl)) {
if (this._elementFocusedBeforeDrawerWasOpened instanceof HTMLElement) {
Expand All @@ -248,13 +259,13 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
this._elementFocusedBeforeDrawerWasOpened = null;
}

ngAfterContentInit() {
ngAfterContentInit(): void {
this._focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement);
this._focusTrap.enabled = this._isFocusTrapEnabled;
this._focusTrap.enabled = this.isFocusTrapEnabled;
this._enableAnimations = true;
}

ngOnDestroy() {
ngOnDestroy(): void {
if (this._focusTrap) {
this._focusTrap.destroy();
}
Expand Down Expand Up @@ -296,11 +307,16 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
}

this._currentTogglePromise = new Promise(resolve => {
first.call(isOpen ? this.onOpen : this.onClose).subscribe(resolve);
first.call(this.openedChange).subscribe(resolve);

if (!isOpen) {
// add backward support
first.call(this.onClose).subscribe(resolve);
}
});

if (this._focusTrap) {
this._focusTrap.enabled = this._isFocusTrapEnabled;
this._focusTrap.enabled = this.isFocusTrapEnabled;
}
}

Expand All @@ -313,24 +329,25 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
* Handles the keyboard events.
* @docs-private
*/
handleKeydown(event: KeyboardEvent) {
handleKeydown(event: KeyboardEvent): void {
if (event.keyCode === ESCAPE && !this.disableClose) {
this.close();
event.stopPropagation();
}
}

_onAnimationStart() {
_onAnimationStart(): void {
this._isAnimating = true;
this._animationStarted.emit();
}

_onAnimationEnd(event: AnimationEvent) {
_onAnimationEnd(event: AnimationEvent): void {
const {fromState, toState} = event;

if (toState.indexOf('open') === 0 && fromState === 'void') {
this.onOpen.emit(new MatDrawerToggleResult('open', true));
} else if (toState === 'void' && fromState.indexOf('open') === 0) {
if (toState === 'open' && fromState === 'void') {
this.openedChange.emit(new MatDrawerToggleResult('open', true));
} else if (toState === 'void' && fromState === 'open') {
this.openedChange.emit(new MatDrawerToggleResult('close', true));
this.onClose.emit(new MatDrawerToggleResult('close', true));
}

Expand All @@ -343,7 +360,7 @@ export class MatDrawer implements AfterContentInit, OnDestroy {
});
}

get _width() {
get _width(): number {
return this._elementRef.nativeElement ? (this._elementRef.nativeElement.offsetWidth || 0) : 0;
}
}
Expand Down Expand Up @@ -412,7 +429,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
}
}

ngAfterContentInit() {
ngAfterContentInit(): void {
startWith.call(this._drawers.changes, null).subscribe(() => {
this._validateDrawers();

Expand Down Expand Up @@ -462,8 +479,8 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
});

if (drawer.mode !== 'side') {
takeUntil.call(merge(drawer.onOpen, drawer.onClose), this._drawers.changes).subscribe(() =>
this._setContainerClass(drawer.opened));
takeUntil.call(drawer.openedChange,
this._drawers.changes).subscribe(() => this._setContainerClass(drawer.opened));
}
}

Expand Down Expand Up @@ -505,7 +522,7 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
}

/** Validate the state of the drawer children components. */
private _validateDrawers() {
private _validateDrawers(): void {
this._start = this._end = null;

// Ensure that we have at most one start and one end drawer.
Expand Down Expand Up @@ -535,12 +552,12 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
}
}

_onBackdropClicked() {
_onBackdropClicked(): void {
this.backdropClick.emit();
this._closeModalDrawer();
}

_closeModalDrawer() {
_closeModalDrawer(): void {
// 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')
Expand Down