Skip to content

Commit 54b9710

Browse files
authored
Merge pull request rust-lang#1037 from Flying-Toast/prefers-color-scheme
Automatically use a dark theme according to 'prefers-color-scheme'
2 parents 2097a55 + eaf34e9 commit 54b9710

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

book-example/src/format/config.md

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ The following configuration options are available:
150150
files with the ones found in the specified folder.
151151
- **default-theme:** The theme color scheme to select by default in the
152152
'Change Theme' dropdown. Defaults to `light`.
153+
- **preferred-dark-theme:** The default dark theme. This theme will be used if
154+
the browser requests the dark version of the site via the
155+
['prefers-color-scheme'](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme)
156+
CSS media query. Defaults to the same theme as `default-theme`.
153157
- **curly-quotes:** Convert straight quotes to curly quotes, except for those
154158
that occur in code blocks and code spans. Defaults to `false`.
155159
- **mathjax-support:** Adds support for [MathJax](mathjax.md). Defaults to
@@ -217,6 +221,7 @@ description = "The example book covers examples."
217221
[output.html]
218222
theme = "my-theme"
219223
default-theme = "light"
224+
preferred-dark-theme = "navy"
220225
curly-quotes = true
221226
mathjax-support = false
222227
google-analytics = "123456"

src/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ pub struct HtmlConfig {
427427
pub theme: Option<PathBuf>,
428428
/// The default theme to use, defaults to 'light'
429429
pub default_theme: Option<String>,
430+
/// The theme to use if the browser requests the dark version of the site.
431+
/// Defaults to the same as 'default_theme'
432+
pub preferred_dark_theme: Option<String>,
430433
/// Use "smart quotes" instead of the usual `"` character.
431434
pub curly_quotes: bool,
432435
/// Should mathjax be enabled?

src/renderer/html_handlebars/hbs_renderer.rs

+9
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,15 @@ fn make_data(
406406
};
407407
data.insert("default_theme".to_owned(), json!(default_theme));
408408

409+
let preferred_dark_theme = match html_config.preferred_dark_theme {
410+
Some(ref theme) => theme,
411+
None => default_theme,
412+
};
413+
data.insert(
414+
"preferred_dark_theme".to_owned(),
415+
json!(preferred_dark_theme),
416+
);
417+
409418
// Add google analytics tag
410419
if let Some(ref ga) = html_config.google_analytics {
411420
data.insert("google_analytics".to_owned(), json!(ga));

src/theme/index.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<!-- Provide site root to javascript -->
4444
<script type="text/javascript">
4545
var path_to_root = "{{ path_to_root }}";
46-
var default_theme = "{{ default_theme }}";
46+
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "{{ preferred_dark_theme }}" : "{{ default_theme }}";
4747
</script>
4848

4949
<!-- Work around some values being stored in localStorage wrapped in quotes -->

0 commit comments

Comments
 (0)