Skip to content

Commit f706372

Browse files
committed
Use CSS instead of JS for auto color theme detection
This enables clients who don't have JavaScript enabled to still use the dark theme, by detecting it with a CSS @media query rather than with JS. This only applies if disableThemeToggle is set and the default theme is 'auto' or unset.
1 parent e76154d commit f706372

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

assets/css/theme-vars.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,22 @@
3636
.dark.list {
3737
background: var(--theme);
3838
}
39+
40+
{{- if (and (and (ne .Site.Params.defaultTheme "light") (ne .Site.Params.defaultTheme "dark")) .Site.Params.disableThemeToggle) }}
41+
@media (prefers-color-scheme: dark) {
42+
:root {
43+
--theme: #1d1e20;
44+
--entry: #2e2e33;
45+
--primary: rgba(255, 255, 255, 0.84);
46+
--secondary: rgba(255, 255, 255, 0.56);
47+
--tertiary: rgba(255, 255, 255, 0.16);
48+
--content: rgba(255, 255, 255, 0.74);
49+
--hljs-bg: #2e2e33;
50+
--code-bg: #37383e;
51+
--border: #333;
52+
}
53+
.list {
54+
background: var(--theme);
55+
}
56+
}
57+
{{- end }}

layouts/partials/header.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@
2929

3030
</script>
3131
{{- end }}
32-
{{- /* theme-toggle is disabled and theme is auto */}}
33-
{{- else if (and (ne .Site.Params.defaultTheme "light") (ne .Site.Params.defaultTheme "dark"))}}
34-
<script>
35-
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
36-
document.body.classList.add('dark');
37-
}
38-
39-
</script>
4032
{{- end }}
4133
<noscript>
4234
<style type="text/css">

0 commit comments

Comments
 (0)