6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import {
10
- Component ,
11
- OnDestroy ,
12
- OnInit ,
13
- ViewEncapsulation ,
14
- forwardRef ,
15
- inject ,
16
- viewChild ,
17
- } from '@angular/core' ;
9
+ import { Component , ViewEncapsulation , forwardRef , inject , viewChild } from '@angular/core' ;
18
10
import { BreakpointObserver } from '@angular/cdk/layout' ;
19
11
import { AsyncPipe } from '@angular/common' ;
20
12
import { MatListItem , MatNavList } from '@angular/material/list' ;
21
13
import { MatSidenav , MatSidenavContainer } from '@angular/material/sidenav' ;
22
14
import { ActivatedRoute , Routes , RouterOutlet , RouterLinkActive , RouterLink } from '@angular/router' ;
23
- import { Observable , of , Subscription } from 'rxjs' ;
15
+ import { Observable , of } from 'rxjs' ;
24
16
import { map , switchMap } from 'rxjs/operators' ;
25
17
26
18
import { DocumentationItems } from '../../shared/documentation-items/documentation-items' ;
@@ -37,6 +29,7 @@ import {
37
29
ComponentViewer ,
38
30
} from '../component-viewer/component-viewer' ;
39
31
import { ComponentStyling } from '../component-viewer/component-styling' ;
32
+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
40
33
41
34
// These constants are used by the ComponentSidenav for orchestrating the MatSidenav in a responsive
42
35
// way. This includes hiding the sidenav, defaulting it to open, changing the mode from over to
@@ -63,14 +56,13 @@ const SMALL_WIDTH_BREAKPOINT = 959;
63
56
AsyncPipe ,
64
57
] ,
65
58
} )
66
- export class ComponentSidenav implements OnInit , OnDestroy {
59
+ export class ComponentSidenav {
67
60
docItems = inject ( DocumentationItems ) ;
68
61
private _navigationFocusService = inject ( NavigationFocusService ) ;
69
62
70
63
readonly sidenav = viewChild ( MatSidenav ) ;
71
64
isExtraScreenSmall : Observable < boolean > ;
72
65
isScreenSmall : Observable < boolean > ;
73
- private _subscriptions = new Subscription ( ) ;
74
66
75
67
constructor ( ) {
76
68
const breakpoints = inject ( BreakpointObserver ) ;
@@ -81,23 +73,19 @@ export class ComponentSidenav implements OnInit, OnDestroy {
81
73
this . isScreenSmall = breakpoints
82
74
. observe ( `(max-width: ${ SMALL_WIDTH_BREAKPOINT } px)` )
83
75
. pipe ( map ( breakpoint => breakpoint . matches ) ) ;
84
- }
85
-
86
- ngOnInit ( ) {
87
- this . _subscriptions . add (
88
- this . _navigationFocusService . navigationEndEvents
89
- . pipe ( map ( ( ) => this . isScreenSmall ) )
90
- . subscribe ( shouldCloseSideNav => {
91
- const sidenav = this . sidenav ( ) ;
92
- if ( shouldCloseSideNav && sidenav ) {
93
- sidenav . close ( ) ;
94
- }
95
- } ) ,
96
- ) ;
97
- }
98
76
99
- ngOnDestroy ( ) {
100
- this . _subscriptions . unsubscribe ( ) ;
77
+ // Close the sidenav on navigation when the screen is small.
78
+ this . _navigationFocusService . navigationEndEvents
79
+ . pipe (
80
+ takeUntilDestroyed ( ) ,
81
+ map ( ( ) => this . isScreenSmall ) ,
82
+ )
83
+ . subscribe ( shouldCloseSideNav => {
84
+ const sidenav = this . sidenav ( ) ;
85
+ if ( shouldCloseSideNav && sidenav ) {
86
+ sidenav . close ( ) ;
87
+ }
88
+ } ) ;
101
89
}
102
90
103
91
toggleSidenav ( ) : void {
0 commit comments