9
9
import {
10
10
ChangeDetectionStrategy ,
11
11
Component ,
12
- OnDestroy ,
12
+ DestroyRef ,
13
13
OnInit ,
14
14
ViewEncapsulation ,
15
15
inject ,
@@ -21,7 +21,7 @@ import {MatIcon} from '@angular/material/icon';
21
21
import { MatMenu , MatMenuItem , MatMenuTrigger } from '@angular/material/menu' ;
22
22
import { MatTooltip } from '@angular/material/tooltip' ;
23
23
import { ActivatedRoute , ParamMap } from '@angular/router' ;
24
- import { Subscription } from 'rxjs' ;
24
+ import { takeUntilDestroyed } from '@angular/core/ rxjs-interop ' ;
25
25
import { map } from 'rxjs/operators' ;
26
26
import { LiveAnnouncer } from '@angular/cdk/a11y' ;
27
27
@@ -33,13 +33,13 @@ import {LiveAnnouncer} from '@angular/cdk/a11y';
33
33
encapsulation : ViewEncapsulation . None ,
34
34
imports : [ MatIconButton , MatTooltip , MatMenu , MatMenuItem , MatMenuTrigger , MatIcon ] ,
35
35
} )
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 ) ;
36
+ export class ThemePicker implements OnInit {
37
+ readonly styleManager = inject ( StyleManager ) ;
38
+ private readonly _themeStorage = inject ( ThemeStorage ) ;
39
+ private readonly _activatedRoute = inject ( ActivatedRoute ) ;
40
+ private readonly _liveAnnouncer = inject ( LiveAnnouncer ) ;
41
+ private readonly _destroyRef = inject ( DestroyRef ) ;
41
42
42
- private _queryParamSubscription = Subscription . EMPTY ;
43
43
currentTheme : DocsSiteTheme | undefined ;
44
44
45
45
// The below colors need to align with the themes defined in theme-picker.scss
@@ -85,19 +85,18 @@ export class ThemePicker implements OnInit, OnDestroy {
85
85
}
86
86
87
87
ngOnInit ( ) {
88
- this . _queryParamSubscription = this . _activatedRoute . queryParamMap
89
- . pipe ( map ( ( params : ParamMap ) => params . get ( 'theme' ) ) )
88
+ this . _activatedRoute . queryParamMap
89
+ . pipe (
90
+ map ( ( params : ParamMap ) => params . get ( 'theme' ) ) ,
91
+ takeUntilDestroyed ( this . _destroyRef ) ,
92
+ )
90
93
. subscribe ( ( themeName : string | null ) => {
91
94
if ( themeName ) {
92
95
this . selectTheme ( themeName ) ;
93
96
}
94
97
} ) ;
95
98
}
96
99
97
- ngOnDestroy ( ) {
98
- this . _queryParamSubscription . unsubscribe ( ) ;
99
- }
100
-
101
100
selectTheme ( themeName : string ) {
102
101
const theme =
103
102
this . themes . find ( currentTheme => currentTheme . name === themeName ) ||
0 commit comments