1
1
import { MediaMatcher } from '@angular/cdk/layout' ;
2
- import { ChangeDetectorRef , Component , OnDestroy , inject } from '@angular/core' ;
2
+ import { Component , OnDestroy , inject , signal } from '@angular/core' ;
3
3
import { MatListModule } from '@angular/material/list' ;
4
4
import { MatSidenavModule } from '@angular/material/sidenav' ;
5
5
import { MatIconModule } from '@angular/material/icon' ;
6
6
import { MatButtonModule } from '@angular/material/button' ;
7
7
import { MatToolbarModule } from '@angular/material/toolbar' ;
8
+ import { RouterLink } from '@angular/router' ;
8
9
9
10
/** @title Responsive sidenav */
10
11
@Component ( {
11
12
selector : 'sidenav-responsive-example' ,
12
13
templateUrl : 'sidenav-responsive-example.html' ,
13
14
styleUrl : 'sidenav-responsive-example.css' ,
14
- imports : [ MatToolbarModule , MatButtonModule , MatIconModule , MatSidenavModule , MatListModule ] ,
15
+ imports : [
16
+ MatToolbarModule ,
17
+ MatButtonModule ,
18
+ MatIconModule ,
19
+ MatSidenavModule ,
20
+ MatListModule ,
21
+ RouterLink ,
22
+ ] ,
15
23
} )
16
24
export class SidenavResponsiveExample implements OnDestroy {
17
- mobileQuery : MediaQueryList ;
25
+ protected readonly fillerNav = Array . from ( { length : 50 } , ( _ , i ) => `Nav Item ${ i + 1 } ` ) ;
18
26
19
- fillerNav = Array . from ( { length : 50 } , ( _ , i ) => `Nav Item ${ i + 1 } ` ) ;
20
-
21
- fillerContent = Array . from (
27
+ protected readonly fillerContent = Array . from (
22
28
{ length : 50 } ,
23
29
( ) =>
24
30
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
@@ -28,20 +34,25 @@ export class SidenavResponsiveExample implements OnDestroy {
28
34
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.` ,
29
35
) ;
30
36
31
- private _mobileQueryListener : ( ) => void ;
37
+ protected readonly isMobile = signal ( true ) ;
38
+
39
+ private readonly _mobileQuery : MediaQueryList ;
40
+ private readonly _mobileQueryListener : ( ) => void ;
32
41
33
42
constructor ( ) {
34
- const changeDetectorRef = inject ( ChangeDetectorRef ) ;
35
43
const media = inject ( MediaMatcher ) ;
36
44
37
- this . mobileQuery = media . matchMedia ( '(max-width: 600px)' ) ;
38
- this . _mobileQueryListener = ( ) => changeDetectorRef . detectChanges ( ) ;
39
- this . mobileQuery . addListener ( this . _mobileQueryListener ) ;
45
+ this . _mobileQuery = media . matchMedia ( '(max-width: 600px)' ) ;
46
+ this . isMobile . set ( this . _mobileQuery . matches ) ;
47
+ this . _mobileQueryListener = ( ) => this . isMobile . set ( this . _mobileQuery . matches ) ;
48
+ this . _mobileQuery . addEventListener ( 'change' , this . _mobileQueryListener ) ;
40
49
}
41
50
42
51
ngOnDestroy ( ) : void {
43
- this . mobileQuery . removeListener ( this . _mobileQueryListener ) ;
52
+ this . _mobileQuery . removeEventListener ( 'change' , this . _mobileQueryListener ) ;
44
53
}
45
54
46
- shouldRun = / ( ^ | .) ( s t a c k b l i t z | w e b c o n t a i n e r ) .( i o | c o m ) $ / . test ( window . location . host ) ;
55
+ protected readonly shouldRun = / ( ^ | .) ( s t a c k b l i t z | w e b c o n t a i n e r ) .( i o | c o m ) $ / . test (
56
+ window . location . host ,
57
+ ) ;
47
58
}
0 commit comments