@@ -10,6 +10,9 @@ import {Directionality} from '@angular/cdk/bidi';
10
10
import { ChangeDetectorRef , Component , ElementRef , Inject , ViewEncapsulation } from '@angular/core' ;
11
11
import { DevAppRippleOptions } from './ripple-options' ;
12
12
import { DevAppDirectionality } from './dev-app-directionality' ;
13
+ import { DOCUMENT } from '@angular/common' ;
14
+
15
+ const isDarkThemeKey = 'ANGULAR_COMPONENTS_DEV_APP_DARK_THEME' ;
13
16
14
17
/** Root component for the dev-app demos. */
15
18
@Component ( {
@@ -19,7 +22,8 @@ import {DevAppDirectionality} from './dev-app-directionality';
19
22
encapsulation : ViewEncapsulation . None ,
20
23
} )
21
24
export class DevAppLayout {
22
- dark = false ;
25
+ readonly darkThemeClass = 'demo-unicorn-dark-theme' ;
26
+ _isDark = false ;
23
27
strongFocus = false ;
24
28
navItems = [
25
29
{ name : 'Examples' , route : '/examples' } ,
@@ -97,9 +101,39 @@ export class DevAppLayout {
97
101
98
102
constructor (
99
103
private _element : ElementRef < HTMLElement > , public rippleOptions : DevAppRippleOptions ,
100
- @Inject ( Directionality ) public dir : DevAppDirectionality , cdr : ChangeDetectorRef ) {
104
+ @Inject ( Directionality ) public dir : DevAppDirectionality , cdr : ChangeDetectorRef ,
105
+ @Inject ( DOCUMENT ) private _document : Document ) {
101
106
dir . change . subscribe ( ( ) => cdr . markForCheck ( ) ) ;
102
107
this . updateDensityClasses ( ) ;
108
+ try {
109
+ const isDark = localStorage . getItem ( isDarkThemeKey ) ;
110
+ if ( isDark != null ) {
111
+ // We avoid calling the setter and apply the themes directly here.
112
+ // This avoids writing the same value, that we just read, back to localStorage.
113
+ this . _isDark = isDark === 'true' ;
114
+ this . updateThemeClass ( this . _isDark ) ;
115
+ }
116
+ } catch ( error ) {
117
+ console . error ( `Failed to read ${ isDarkThemeKey } from localStorage: ` , error ) ;
118
+ }
119
+ }
120
+
121
+ get isDark ( ) : boolean {
122
+ return this . _isDark ;
123
+ }
124
+
125
+ set isDark ( value : boolean ) {
126
+ // Noop if the value is the same as is already set.
127
+ if ( value !== this . _isDark ) {
128
+ this . _isDark = value ;
129
+ this . updateThemeClass ( this . _isDark ) ;
130
+
131
+ try {
132
+ localStorage . setItem ( isDarkThemeKey , String ( value ) ) ;
133
+ } catch ( error ) {
134
+ console . error ( `Failed to write ${ isDarkThemeKey } to localStorage: ` , error ) ;
135
+ }
136
+ }
103
137
}
104
138
105
139
toggleFullscreen ( ) {
@@ -116,15 +150,11 @@ export class DevAppLayout {
116
150
}
117
151
}
118
152
119
- toggleTheme ( ) {
120
- const darkThemeClass = 'demo-unicorn-dark-theme' ;
121
-
122
- this . dark = ! this . dark ;
123
-
124
- if ( this . dark ) {
125
- document . body . classList . add ( darkThemeClass ) ;
153
+ updateThemeClass ( isDark ?: boolean ) {
154
+ if ( isDark ) {
155
+ this . _document . body . classList . add ( this . darkThemeClass ) ;
126
156
} else {
127
- document . body . classList . remove ( darkThemeClass ) ;
157
+ this . _document . body . classList . remove ( this . darkThemeClass ) ;
128
158
}
129
159
}
130
160
@@ -134,9 +164,9 @@ export class DevAppLayout {
134
164
this . strongFocus = ! this . strongFocus ;
135
165
136
166
if ( this . strongFocus ) {
137
- document . body . classList . add ( strongFocusClass ) ;
167
+ this . _document . body . classList . add ( strongFocusClass ) ;
138
168
} else {
139
- document . body . classList . remove ( strongFocusClass ) ;
169
+ this . _document . body . classList . remove ( strongFocusClass ) ;
140
170
}
141
171
}
142
172
@@ -160,9 +190,9 @@ export class DevAppLayout {
160
190
for ( let i = 0 ; i < this . densityScales . length ; i ++ ) {
161
191
const className = `demo-density-${ this . densityScales [ i ] } ` ;
162
192
if ( i === this . currentDensityIndex ) {
163
- document . body . classList . add ( className ) ;
193
+ this . _document . body . classList . add ( className ) ;
164
194
} else {
165
- document . body . classList . remove ( className ) ;
195
+ this . _document . body . classList . remove ( className ) ;
166
196
}
167
197
}
168
198
}
0 commit comments