Skip to content

Commit

Permalink
Allow disabling dark mode in configuration (#420)
Browse files Browse the repository at this point in the history
* Allow disabling dark mode in configuration

* Set default background according to colorScheme and browser preference
  • Loading branch information
stefanv authored Nov 17, 2023
1 parent a0d58df commit c51780a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assets/theme-css/dark-mode.css
18 changes: 13 additions & 5 deletions assets/js/00_dark.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ var prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
*
* @param {event} e
*/
function autoTheme(e) {
targetTheme = prefersDark.matches ? "dark" : "light";
document.documentElement.setAttribute("data-theme", targetTheme);
function setTheme() {
/* One of auto, light, or dark, depending on what the site wants to support */
const themeScheme = document.documentElement.getAttribute("data-colorscheme");

const browserScheme = prefersDark.matches ? "dark" : "light";

// Use the color scheme from the configuration file, if set
document.documentElement.setAttribute(
"data-theme",
themeScheme == "auto" ? browserScheme : themeScheme,
);
}

autoTheme();
prefersDark.onchange = autoTheme;
setTheme();
prefersDark.onchange = setTheme;
18 changes: 16 additions & 2 deletions assets/theme-css/dark-mode.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
/* Dark mode is now defined in pydata-sphinx-theme CSS */
/* See html[data-theme='light'] and html[data-theme='dark'] */

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

/* prettier-ignore-start */

/* Theme supports light and dark; check browser preference for bg color */

{{- if (eq .Site.Params.colorScheme "auto") -}}
@media (prefers-color-scheme: dark) {
html:not([data-theme]) {
--pst-color-background: #14181e;
}
}
{{- end -}}

/* Theme supports only dark; set background dark */
{{- if (eq .Site.Params.colorScheme "dark") -}}
html:not([data-theme]) {
--pst-color-background: #14181e;
}
{{- end -}}

/* prettier-ignore-end */
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
disableKinds:
- taxonomy
params:
colorScheme: auto # can be auto (browser setting), light, or dark
images:
- /images/logo.svg
navColor: blue
Expand Down
2 changes: 1 addition & 1 deletion layouts/_default/baseof.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="{{ .Site.LanguageCode }}">
<html lang="{{ .Site.LanguageCode }}" data-colorscheme="{{ .Site.Params.colorScheme }}" >
<head>
{{ print "<!-- Generated " (time.Now.UTC.Format "2006-01-02 15:04:05 UTC") " -->" | safeHTML }}
{{ partial "meta.html" . }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

0 comments on commit c51780a

Please sign in to comment.