feat(darkmode); add colorScheme config

This commit is contained in:
Jimmy Cai 2020-12-23 18:16:32 +01:00
parent e49d77ec91
commit b2683420d9
No known key found for this signature in database
GPG Key ID: 3EA408E527F37B18
5 changed files with 30 additions and 11 deletions

View File

@ -8,8 +8,12 @@ class StackDarkMode {
constructor(toggleEl: HTMLElement) {
this.bindMatchMedia();
this.currentScheme = this.getSavedScheme();
this.setBodyClass();
this.bindClick(toggleEl);
if (toggleEl)
this.bindClick(toggleEl);
if (document.body.style.transition == '')
document.body.style.setProperty('transition', 'background-color .3s ease');
}
private saveScheme() {
@ -49,9 +53,6 @@ class StackDarkMode {
}
private setBodyClass() {
if (document.body.style.transition == '')
document.body.style.setProperty('transition', 'background-color .3s ease');
if (this.isDark()) {
document.body.dataset.scheme = 'dark';
}

View File

@ -79,6 +79,10 @@ params:
local: false
src:
colorScheme:
toggle: true
default: auto
menu:
main:
- identifier: home

View File

@ -5,7 +5,7 @@
{{- block "head" . -}}{{ end }}
</head>
<body class="{{ block `body-class` . }}{{ end }}">
{{- partial "head/darkmode" . -}}
{{- partial "head/colorScheme" . -}}
<div class="container flex on-phone--column {{ if .Site.Params.widgets.enabled }}extended{{ else }}compact{{ end }} {{ block `container-class` . }}{{end}}">
{{ partial "sidebar/left.html" . }}

View File

@ -1,3 +1,15 @@
{{- with .Site.Params.colorScheme -}}
{{/* If toggle is disabled, set color to default */}}
{{- $defaultColorScheme := default "default" .default -}}
{{- if not .toggle -}}
<script>
(function() {
const colorSchemeKey = 'StackColorScheme';
localStorage.setItem(colorSchemeKey, {{ $defaultColorScheme }});
})();
</script>
{{- end -}}
{{- end -}}
<script>
(function() {
const colorSchemeKey = 'StackColorScheme';

View File

@ -46,10 +46,12 @@
</li>
{{ end }}
<li id="dark-mode-toggle">
{{ partial "helper/icon" "toggle-left" }}
{{ partial "helper/icon" "toggle-right" }}
<span>Dark Mode</span>
</li>
{{ if (default false .Site.Params.colorScheme.toggle) }}
<li id="dark-mode-toggle">
{{ partial "helper/icon" "toggle-left" }}
{{ partial "helper/icon" "toggle-right" }}
<span>Dark Mode</span>
</li>
{{ end }}
</ol>
</aside>