Skip to content

Commit c51780a

Browse files
authored
Allow disabling dark mode in configuration (#420)
* Allow disabling dark mode in configuration * Set default background according to colorScheme and browser preference
1 parent a0d58df commit c51780a

6 files changed

Lines changed: 33 additions & 9 deletions

File tree

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assets/theme-css/dark-mode.css

assets/js/00_dark.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ var prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
99
*
1010
* @param {event} e
1111
*/
12-
function autoTheme(e) {
13-
targetTheme = prefersDark.matches ? "dark" : "light";
14-
document.documentElement.setAttribute("data-theme", targetTheme);
12+
function setTheme() {
13+
/* One of auto, light, or dark, depending on what the site wants to support */
14+
const themeScheme = document.documentElement.getAttribute("data-colorscheme");
15+
16+
const browserScheme = prefersDark.matches ? "dark" : "light";
17+
18+
// Use the color scheme from the configuration file, if set
19+
document.documentElement.setAttribute(
20+
"data-theme",
21+
themeScheme == "auto" ? browserScheme : themeScheme,
22+
);
1523
}
1624

17-
autoTheme();
18-
prefersDark.onchange = autoTheme;
25+
setTheme();
26+
prefersDark.onchange = setTheme;

assets/theme-css/dark-mode.css

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
/* Dark mode is now defined in pydata-sphinx-theme CSS */
22
/* See html[data-theme='light'] and html[data-theme='dark'] */
33

4-
/* This CSS is to override the background to dark, if that is the browser preference,
5-
instead of defaulting to light mode rendering. */
4+
/* This CSS is to avoid flickering, by setting the background to dark, if that is the browser preference. */
65

6+
/* prettier-ignore-start */
7+
8+
/* Theme supports light and dark; check browser preference for bg color */
9+
10+
{{- if (eq .Site.Params.colorScheme "auto") -}}
711
@media (prefers-color-scheme: dark) {
812
html:not([data-theme]) {
913
--pst-color-background: #14181e;
1014
}
1115
}
16+
{{- end -}}
17+
18+
/* Theme supports only dark; set background dark */
19+
{{- if (eq .Site.Params.colorScheme "dark") -}}
20+
html:not([data-theme]) {
21+
--pst-color-background: #14181e;
22+
}
23+
{{- end -}}
24+
25+
/* prettier-ignore-end */

config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
disableKinds:
22
- taxonomy
33
params:
4+
colorScheme: auto # can be auto (browser setting), light, or dark
45
images:
56
- /images/logo.svg
67
navColor: blue

layouts/_default/baseof.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html lang="{{ .Site.LanguageCode }}">
2+
<html lang="{{ .Site.LanguageCode }}" data-colorscheme="{{ .Site.Params.colorScheme }}" >
33
<head>
44
{{ print "<!-- Generated " (time.Now.UTC.Format "2006-01-02 15:04:05 UTC") " -->" | safeHTML }}
55
{{ partial "meta.html" . }}

layouts/partials/javascript.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!-- Custom JS -->
88
<!-- All JS files under static/js are included -->
9-
{{- $jsFiles := resources.Match "js/*.js" -}}
9+
{{- $jsFiles := collections.Sort (resources.Match "js/*.js") "Name" -}}
1010
{{- if $inServerMode -}}
1111
{{- $js := $jsFiles | resources.Concat "js/bundle.js" }}
1212
<script type="text/javascript" src={{ $js.RelPermalink }}></script>

0 commit comments

Comments
 (0)