1
- import { Component , HostBinding , OnDestroy } from "@angular/core" ;
1
+ import { Component , HostBinding , OnDestroy , OnInit } from "@angular/core" ;
2
2
import { FormControl , ReactiveFormsModule } from "@angular/forms" ;
3
+ import { Subscription } from "rxjs" ;
3
4
import { SegmentedControlButtonComponent , SegmentedControlComponent } from "../segmented-control" ;
4
5
import { RadioValueAccessorDirective } from "../../library-components/directives" ;
5
6
import { ColorModeService , ColourMode } from "../../library-components/services" ;
6
7
import { ContainerComponent } from "../container" ;
7
8
import { IconComponent } from "../icon" ;
8
- import { distinctUntilChanged } from "rxjs" ;
9
-
10
9
11
10
@Component ( {
12
11
// eslint-disable-next-line @angular-eslint/component-selector
@@ -23,24 +22,15 @@ import {distinctUntilChanged} from "rxjs";
23
22
] ,
24
23
standalone : true
25
24
} )
26
- export class MastheadComponent implements OnDestroy {
25
+ export class MastheadComponent implements OnInit , OnDestroy {
27
26
protected ColorMode = ColourMode ;
27
+ protected mode = new FormControl ( ColourMode . AUTO ) ;
28
28
protected expanded = false ;
29
- protected mode = new FormControl < ColourMode > ( ColourMode . AUTO ) ;
30
-
31
- protected mode$ = this . mode
32
- . valueChanges
33
- . pipe ( distinctUntilChanged ( ) )
34
- . subscribe ( ( value ) => value
35
- ? this . colorModeService . set ( value )
36
- : this . colorModeService . set ( ColourMode . AUTO ) ) ;
37
29
38
- protected globalMode$ = this . colorModeService
39
- . asObservable
40
- . subscribe ( ( value ) => this . mode . setValue ( value ) )
30
+ private mode$ ! : Subscription ;
31
+ private storageMode$ ! : Subscription ;
41
32
42
- @HostBinding ( 'class.masthead' )
43
- readonly hbClassMasthead = true ;
33
+ @HostBinding ( 'class.masthead' ) readonly hbClassMasthead = true ;
44
34
45
35
@HostBinding ( 'class.masthead--expanded' )
46
36
get hbClassMastheadExpanded ( ) {
@@ -50,11 +40,25 @@ export class MastheadComponent implements OnDestroy {
50
40
constructor ( protected colorModeService : ColorModeService ) {
51
41
}
52
42
43
+ ngOnInit ( ) {
44
+ this . mode$ = this . mode . valueChanges . subscribe ( this . onValueChange ) ;
45
+ this . storageMode$ = this . colorModeService . asObservable . subscribe ( this . onStorageChange ) ;
46
+ }
47
+
53
48
ngOnDestroy ( ) {
54
49
this . mode$ . unsubscribe ( ) ;
55
- this . globalMode $. unsubscribe ( ) ;
50
+ this . storageMode $. unsubscribe ( ) ;
56
51
}
57
52
53
+ onStorageChange = ( value : ColourMode ) => {
54
+ this . mode . setValue ( value ) ;
55
+ }
56
+
57
+ onValueChange = ( value : ColourMode | null ) =>
58
+ value
59
+ ? this . colorModeService . set ( value )
60
+ : this . colorModeService . set ( ColourMode . AUTO )
61
+
58
62
toggleExpanded ( ) {
59
63
this . expanded = ! this . expanded ;
60
64
}
0 commit comments