mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-29 03:53:30 +08:00
feat(darkmode); add colorScheme config
This commit is contained in:
parent
e49d77ec91
commit
b2683420d9
@ -8,8 +8,12 @@ class StackDarkMode {
|
||||
constructor(toggleEl: HTMLElement) {
|
||||
this.bindMatchMedia();
|
||||
this.currentScheme = this.getSavedScheme();
|
||||
this.setBodyClass();
|
||||
|
||||
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';
|
||||
}
|
||||
|
@ -79,6 +79,10 @@ params:
|
||||
local: false
|
||||
src:
|
||||
|
||||
colorScheme:
|
||||
toggle: true
|
||||
default: auto
|
||||
|
||||
menu:
|
||||
main:
|
||||
- identifier: home
|
||||
|
@ -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" . }}
|
||||
|
@ -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';
|
@ -46,10 +46,12 @@
|
||||
</li>
|
||||
{{ end }}
|
||||
|
||||
{{ 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>
|
||||
|
Loading…
Reference in New Issue
Block a user