Skip to content

Commit c076d30

Browse files
take into account the system theme
1 parent 7858dc2 commit c076d30

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/librustdoc/html/static/rustdoc.css

+15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@
5454
box-sizing: border-box;
5555
}
5656

57+
/* This part handles the "default" theme being used depending on the system one. */
58+
html {
59+
content: "";
60+
}
61+
@media (prefers-color-scheme: light) {
62+
html {
63+
content: "light";
64+
}
65+
}
66+
@media (prefers-color-scheme: dark) {
67+
html {
68+
content: "dark";
69+
}
70+
}
71+
5772
/* General structure and fonts */
5873

5974
body {

src/librustdoc/html/static/storage.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function getCurrentValue(name) {
8686
return null;
8787
}
8888

89-
function switchTheme(styleElem, mainStyleElem, newTheme) {
89+
function switchTheme(styleElem, mainStyleElem, newTheme, skipStorage) {
9090
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
9191
var fullNewTheme = newTheme + resourcesSuffix + ".css";
9292
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
@@ -109,8 +109,18 @@ function switchTheme(styleElem, mainStyleElem, newTheme) {
109109
});
110110
if (found === true) {
111111
styleElem.href = newHref;
112-
updateLocalStorage("rustdoc-theme", newTheme);
112+
// If this new value comes from a system setting or from the previously saved theme, no
113+
// need to save it.
114+
if (skipStorage !== true) {
115+
updateLocalStorage("rustdoc-theme", newTheme);
116+
}
113117
}
114118
}
115119

116-
switchTheme(currentTheme, mainTheme, getCurrentValue("rustdoc-theme") || "light");
120+
function getSystemValue() {
121+
return getComputedStyle(document.documentElement).getPropertyValue('content');
122+
}
123+
124+
switchTheme(currentTheme, mainTheme,
125+
getCurrentValue("rustdoc-theme") || getSystemValue() || "light",
126+
true);

0 commit comments

Comments
 (0)