Skip to content

Commit 2d03af0

Browse files
crisbetojelbourn
authored andcommitted
feat(drawer): expose CdkScrollable instance (#9183)
Exposes the `CdkScrollable` instance that wraps around the drawer's content. This makes it easier for consumers to react to scrolling inside the container. Fixes #9136.
1 parent 9dba30b commit 2d03af0

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/lib/sidenav/drawer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
QueryList,
3232
ViewEncapsulation,
3333
InjectionToken,
34+
ViewChild,
3435
} from '@angular/core';
3536
import {DOCUMENT} from '@angular/common';
3637
import {merge} from 'rxjs/observable/merge';
@@ -43,6 +44,7 @@ import {map} from 'rxjs/operators/map';
4344
import {Subject} from 'rxjs/Subject';
4445
import {Observable} from 'rxjs/Observable';
4546
import {matDrawerAnimations} from './drawer-animations';
47+
import {CdkScrollable} from '@angular/cdk/scrolling';
4648

4749

4850
/** Throws an exception when two MatDrawer are matching the same position. */
@@ -468,6 +470,9 @@ export class MatDrawerContainer implements AfterContentInit, OnDestroy {
468470

469471
_contentMargins = new Subject<{left: number|null, right: number|null}>();
470472

473+
/** Reference to the CdkScrollable instance that wraps the scrollable content. */
474+
@ViewChild(CdkScrollable) scrollable: CdkScrollable;
475+
471476
constructor(@Optional() private _dir: Directionality,
472477
private _element: ElementRef,
473478
private _ngZone: NgZone,

src/lib/sidenav/sidenav.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ CSS to adjust to either type of device.
172172

173173
<!-- example(sidenav-responsive) -->
174174

175+
### Reacting to scroll events inside the sidenav container
176+
177+
To react to scrolling inside the `<mat-sidenav-container>`, you can get a hold of the underlying
178+
`CdkScrollable` instance through the `MatSidenavContainer`.
179+
180+
```ts
181+
class YourComponent {
182+
@ViewChild(MatSidenavContainer) sidenavContainer: MatSidenavContainer;
183+
184+
constructor() {
185+
this.sidenavContainer.scrollable.elementScrolled().subscribe(() => /* react to scrolling */);
186+
}
187+
}
188+
```
189+
175190
### Accessibility
176191

177192
The `<mat-sidenav>` an `<mat-sidenav-content>` should each be given an appropriate `role` attribute

src/lib/sidenav/sidenav.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
*/
88

99
import {
10-
ChangeDetectionStrategy, ChangeDetectorRef,
11-
Component, ContentChild,
12-
ContentChildren, forwardRef, Inject, Input,
13-
ViewEncapsulation
10+
ChangeDetectionStrategy,
11+
ChangeDetectorRef,
12+
Component,
13+
ContentChild,
14+
ContentChildren,
15+
forwardRef,
16+
Inject,
17+
Input,
18+
ViewEncapsulation,
19+
QueryList,
1420
} from '@angular/core';
1521
import {MatDrawer, MatDrawerContainer, MatDrawerContent} from './drawer';
1622
import {matDrawerAnimations} from './drawer-animations';
@@ -107,7 +113,6 @@ export class MatSidenav extends MatDrawer {
107113
preserveWhitespaces: false,
108114
})
109115
export class MatSidenavContainer extends MatDrawerContainer {
110-
@ContentChildren(MatSidenav) _drawers;
111-
112-
@ContentChild(MatSidenavContent) _content;
116+
@ContentChildren(MatSidenav) _drawers: QueryList<MatSidenav>;
117+
@ContentChild(MatSidenavContent) _content: MatSidenavContent;
113118
}

0 commit comments

Comments
 (0)