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