8
8
const darkThemes = [ "dark" , "ayu" ] ;
9
9
window . currentTheme = document . getElementById ( "themeStyle" ) ;
10
10
11
- // WARNING: RUSTDOC_MOBILE_BREAKPOINT MEDIA QUERY
12
- // If you update this line, then you also need to update the media query with the same
13
- // warning in rustdoc.css
14
- window . RUSTDOC_MOBILE_BREAKPOINT = 700 ;
15
-
16
11
const settingsDataset = ( function ( ) {
17
12
const settingsElement = document . getElementById ( "default-settings" ) ;
18
- if ( settingsElement === null ) {
19
- return null ;
20
- }
21
- const dataset = settingsElement . dataset ;
22
- if ( dataset === undefined ) {
23
- return null ;
24
- }
25
- return dataset ;
13
+ return settingsElement && settingsElement . dataset ? settingsElement . dataset : null ;
26
14
} ) ( ) ;
27
15
28
16
function getSettingValue ( settingName ) {
29
17
const current = getCurrentValue ( settingName ) ;
30
- if ( current !== null ) {
31
- return current ;
32
- }
33
- if ( settingsDataset !== null ) {
18
+ if ( current === null && settingsDataset !== null ) {
34
19
// See the comment for `default_settings.into_iter()` etc. in
35
20
// `Options::from_matches` in `librustdoc/config.rs`.
36
21
const def = settingsDataset [ settingName . replace ( / - / g, "_" ) ] ;
37
22
if ( def !== undefined ) {
38
23
return def ;
39
24
}
40
25
}
41
- return null ;
26
+ return current ;
42
27
}
43
28
44
29
const localStoredTheme = getSettingValue ( "theme" ) ;
@@ -49,18 +34,16 @@ function hasClass(elem, className) {
49
34
}
50
35
51
36
function addClass ( elem , className ) {
52
- if ( ! elem || ! elem . classList ) {
53
- return ;
37
+ if ( elem && elem . classList ) {
38
+ elem . classList . add ( className ) ;
54
39
}
55
- elem . classList . add ( className ) ;
56
40
}
57
41
58
42
// eslint-disable-next-line no-unused-vars
59
43
function removeClass ( elem , className ) {
60
- if ( ! elem || ! elem . classList ) {
61
- return ;
44
+ if ( elem && elem . classList ) {
45
+ elem . classList . remove ( className ) ;
62
46
}
63
- elem . classList . remove ( className ) ;
64
47
}
65
48
66
49
/**
@@ -127,11 +110,7 @@ function getCurrentValue(name) {
127
110
// Rust to the JS. If there is no such element, return null.
128
111
const getVar = ( function getVar ( name ) {
129
112
const el = document . getElementById ( "rustdoc-vars" ) ;
130
- if ( el ) {
131
- return el . attributes [ "data-" + name ] . value ;
132
- } else {
133
- return null ;
134
- }
113
+ return el ? el . attributes [ "data-" + name ] . value : null ;
135
114
} ) ;
136
115
137
116
function switchTheme ( newThemeName , saveTheme ) {
@@ -158,6 +137,9 @@ function switchTheme(newThemeName, saveTheme) {
158
137
}
159
138
160
139
const updateTheme = ( function ( ) {
140
+ // only listen to (prefers-color-scheme: dark) because light is the default
141
+ const mql = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
142
+
161
143
/**
162
144
* Update the current theme to match whatever the current combination of
163
145
* * the preference for using the system theme
@@ -177,7 +159,7 @@ const updateTheme = (function() {
177
159
const lightTheme = getSettingValue ( "preferred-light-theme" ) || "light" ;
178
160
const darkTheme = getSettingValue ( "preferred-dark-theme" ) || "dark" ;
179
161
180
- if ( isDarkMode ( ) ) {
162
+ if ( mql . matches ) {
181
163
use ( darkTheme , true ) ;
182
164
} else {
183
165
// prefers a light theme, or has no preference
@@ -191,37 +173,7 @@ const updateTheme = (function() {
191
173
}
192
174
}
193
175
194
- // This is always updated below to a function () => bool.
195
- let isDarkMode ;
196
-
197
- // Determine the function for isDarkMode, and if we have
198
- // `window.matchMedia`, set up an event listener on the preferred color
199
- // scheme.
200
- //
201
- // Otherwise, fall back to the prefers-color-scheme value CSS captured in
202
- // the "content" property.
203
- if ( window . matchMedia ) {
204
- // only listen to (prefers-color-scheme: dark) because light is the default
205
- const mql = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
206
-
207
- isDarkMode = ( ) => mql . matches ;
208
-
209
- if ( mql . addEventListener ) {
210
- mql . addEventListener ( "change" , updateTheme ) ;
211
- } else {
212
- // This is deprecated, see:
213
- // https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/addListener
214
- mql . addListener ( updateTheme ) ;
215
- }
216
- } else {
217
- // fallback to the CSS computed value
218
- const cssContent = getComputedStyle ( document . documentElement )
219
- . getPropertyValue ( "content" ) ;
220
- // (Note: the double-quotes come from that this is a CSS value, which
221
- // might be a length, string, etc.)
222
- const cssColorScheme = cssContent || "\"light\"" ;
223
- isDarkMode = ( ) => ( cssColorScheme === "\"dark\"" ) ;
224
- }
176
+ mql . addEventListener ( "change" , updateTheme ) ;
225
177
226
178
return updateTheme ;
227
179
} ) ( ) ;
0 commit comments