@@ -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' } ,
@@ -101,9 +105,39 @@ export class DevAppLayout {
101
105
102
106
constructor (
103
107
private _element : ElementRef < HTMLElement > , public rippleOptions : DevAppRippleOptions ,
104
- @Inject ( Directionality ) public dir : DevAppDirectionality , cdr : ChangeDetectorRef ) {
108
+ @Inject ( Directionality ) public dir : DevAppDirectionality , cdr : ChangeDetectorRef ,
109
+ @Inject ( DOCUMENT ) private _document : Document ) {
105
110
dir . change . subscribe ( ( ) => cdr . markForCheck ( ) ) ;
106
111
this . updateDensityClasses ( ) ;
112
+ try {
113
+ const isDark = localStorage . getItem ( isDarkThemeKey ) ;
114
+ if ( isDark != null ) {
115
+ // We avoid calling the setter and apply the themes directly here.
116
+ // This avoids writing the same value, that we just read, back to localStorage.
117
+ this . _isDark = isDark === 'true' ;
118
+ this . updateThemeClass ( this . _isDark ) ;
119
+ }
120
+ } catch ( error ) {
121
+ console . error ( `Failed to read ${ isDarkThemeKey } from localStorage: ` , error ) ;
122
+ }
123
+ }
124
+
125
+ get isDark ( ) : boolean {
126
+ return this . _isDark ;
127
+ }
128
+
129
+ set isDark ( value : boolean ) {
130
+ // Noop if the value is the same as is already set.
131
+ if ( value !== this . _isDark ) {
132
+ this . _isDark = value ;
133
+ this . updateThemeClass ( this . _isDark ) ;
134
+
135
+ try {
136
+ localStorage . setItem ( isDarkThemeKey , String ( value ) ) ;
137
+ } catch ( error ) {
138
+ console . error ( `Failed to write ${ isDarkThemeKey } to localStorage: ` , error ) ;
139
+ }
140
+ }
107
141
}
108
142
109
143
toggleFullscreen ( ) {
@@ -120,15 +154,11 @@ export class DevAppLayout {
120
154
}
121
155
}
122
156
123
- toggleTheme ( ) {
124
- const darkThemeClass = 'demo-unicorn-dark-theme' ;
125
-
126
- this . dark = ! this . dark ;
127
-
128
- if ( this . dark ) {
129
- document . body . classList . add ( darkThemeClass ) ;
157
+ updateThemeClass ( isDark ?: boolean ) {
158
+ if ( isDark ) {
159
+ this . _document . body . classList . add ( this . darkThemeClass ) ;
130
160
} else {
131
- document . body . classList . remove ( darkThemeClass ) ;
161
+ this . _document . body . classList . remove ( this . darkThemeClass ) ;
132
162
}
133
163
}
134
164
@@ -138,9 +168,9 @@ export class DevAppLayout {
138
168
this . strongFocus = ! this . strongFocus ;
139
169
140
170
if ( this . strongFocus ) {
141
- document . body . classList . add ( strongFocusClass ) ;
171
+ this . _document . body . classList . add ( strongFocusClass ) ;
142
172
} else {
143
- document . body . classList . remove ( strongFocusClass ) ;
173
+ this . _document . body . classList . remove ( strongFocusClass ) ;
144
174
}
145
175
}
146
176
@@ -164,9 +194,9 @@ export class DevAppLayout {
164
194
for ( let i = 0 ; i < this . densityScales . length ; i ++ ) {
165
195
const className = `demo-density-${ this . densityScales [ i ] } ` ;
166
196
if ( i === this . currentDensityIndex ) {
167
- document . body . classList . add ( className ) ;
197
+ this . _document . body . classList . add ( className ) ;
168
198
} else {
169
- document . body . classList . remove ( className ) ;
199
+ this . _document . body . classList . remove ( className ) ;
170
200
}
171
201
}
172
202
}
0 commit comments