6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import {
10
- ChangeDetectionStrategy ,
11
- Component ,
12
- OnDestroy ,
13
- OnInit ,
14
- ViewEncapsulation ,
15
- inject ,
16
- } from '@angular/core' ;
9
+ import { ChangeDetectionStrategy , Component , ViewEncapsulation , inject } from '@angular/core' ;
17
10
import { StyleManager } from '../style-manager' ;
18
11
import { DocsSiteTheme , ThemeStorage } from './theme-storage/theme-storage' ;
19
12
import { MatIconButton } from '@angular/material/button' ;
20
13
import { MatIcon } from '@angular/material/icon' ;
21
14
import { MatMenu , MatMenuItem , MatMenuTrigger } from '@angular/material/menu' ;
22
15
import { MatTooltip } from '@angular/material/tooltip' ;
23
16
import { ActivatedRoute , ParamMap } from '@angular/router' ;
24
- import { Subscription } from 'rxjs' ;
17
+ import { takeUntilDestroyed } from '@angular/core/ rxjs-interop ' ;
25
18
import { map } from 'rxjs/operators' ;
26
19
import { LiveAnnouncer } from '@angular/cdk/a11y' ;
27
20
@@ -33,13 +26,12 @@ import {LiveAnnouncer} from '@angular/cdk/a11y';
33
26
encapsulation : ViewEncapsulation . None ,
34
27
imports : [ MatIconButton , MatTooltip , MatMenu , MatMenuItem , MatMenuTrigger , MatIcon ] ,
35
28
} )
36
- export class ThemePicker implements OnInit , OnDestroy {
37
- styleManager = inject ( StyleManager ) ;
38
- private _themeStorage = inject ( ThemeStorage ) ;
39
- private _activatedRoute = inject ( ActivatedRoute ) ;
40
- private _liveAnnouncer = inject ( LiveAnnouncer ) ;
29
+ export class ThemePicker {
30
+ readonly styleManager = inject ( StyleManager ) ;
31
+ private readonly _themeStorage = inject ( ThemeStorage ) ;
32
+ private readonly _activatedRoute = inject ( ActivatedRoute ) ;
33
+ private readonly _liveAnnouncer = inject ( LiveAnnouncer ) ;
41
34
42
- private _queryParamSubscription = Subscription . EMPTY ;
43
35
currentTheme : DocsSiteTheme | undefined ;
44
36
45
37
// The below colors need to align with the themes defined in theme-picker.scss
@@ -72,6 +64,17 @@ export class ThemePicker implements OnInit, OnDestroy {
72
64
] ;
73
65
74
66
constructor ( ) {
67
+ this . _activatedRoute . queryParamMap
68
+ . pipe (
69
+ map ( ( params : ParamMap ) => params . get ( 'theme' ) ) ,
70
+ takeUntilDestroyed ( ) ,
71
+ )
72
+ . subscribe ( ( themeName : string | null ) => {
73
+ if ( themeName ) {
74
+ this . selectTheme ( themeName ) ;
75
+ }
76
+ } ) ;
77
+
75
78
const themeName = this . _themeStorage . getStoredThemeName ( ) ;
76
79
if ( themeName ) {
77
80
this . selectTheme ( themeName ) ;
@@ -84,20 +87,6 @@ export class ThemePicker implements OnInit, OnDestroy {
84
87
}
85
88
}
86
89
87
- ngOnInit ( ) {
88
- this . _queryParamSubscription = this . _activatedRoute . queryParamMap
89
- . pipe ( map ( ( params : ParamMap ) => params . get ( 'theme' ) ) )
90
- . subscribe ( ( themeName : string | null ) => {
91
- if ( themeName ) {
92
- this . selectTheme ( themeName ) ;
93
- }
94
- } ) ;
95
- }
96
-
97
- ngOnDestroy ( ) {
98
- this . _queryParamSubscription . unsubscribe ( ) ;
99
- }
100
-
101
90
selectTheme ( themeName : string ) {
102
91
const theme =
103
92
this . themes . find ( currentTheme => currentTheme . name === themeName ) ||
0 commit comments