mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-29 03:53:30 +08:00
feat: initial support for darkmode toggle
This commit is contained in:
parent
3a003eadca
commit
0f9682e4b4
7
assets/icons/toggle-left.svg
Normal file
7
assets/icons/toggle-left.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path stroke="none" d="M0 0h24v24H0z"/>
|
||||||
|
<circle cx="8" cy="12" r="2" />
|
||||||
|
<rect x="2" y="6" width="20" height="12" rx="6" />
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
|
After Width: | Height: | Size: 369 B |
7
assets/icons/toggle-right.svg
Normal file
7
assets/icons/toggle-right.svg
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-right" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path stroke="none" d="M0 0h24v24H0z"/>
|
||||||
|
<circle cx="16" cy="12" r="2" />
|
||||||
|
<rect x="2" y="6" width="20" height="12" rx="6" />
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
|
After Width: | Height: | Size: 371 B |
@ -128,6 +128,7 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
margin-top: 25px;
|
margin-top: 25px;
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
|
flex-grow: 1;
|
||||||
|
|
||||||
@media (min-width: $on-desktop-large) {
|
@media (min-width: $on-desktop-large) {
|
||||||
margin-top: 30px;
|
margin-top: 30px;
|
||||||
@ -205,3 +206,30 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.theme-dark {
|
||||||
|
#dark-mode-toggle {
|
||||||
|
color: var(--accent-color);
|
||||||
|
font-weight: 700;
|
||||||
|
|
||||||
|
.icon-tabler-toggle-left {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.icon-tabler-toggle-right {
|
||||||
|
display: unset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#dark-mode-toggle {
|
||||||
|
margin-top: auto;
|
||||||
|
color: var(--body-text-color);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
cursor: pointer;
|
||||||
|
|
||||||
|
.icon-tabler-toggle-right {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
width: 25%;
|
width: 25%;
|
||||||
margin-right: 1%;
|
margin-right: 1%;
|
||||||
padding: var(--main-top-padding) 15px;
|
padding: var(--main-top-padding) 15px;
|
||||||
max-height: 100vh;
|
height: 100vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (min-width: $on-desktop) {
|
@media (min-width: $on-desktop) {
|
||||||
|
@ -20,7 +20,7 @@ $defaultTagColors: #fff, #fff, #fff, #fff, #fff;
|
|||||||
|
|
||||||
--section-separation: 40px;
|
--section-separation: 40px;
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
.theme-dark {
|
||||||
--body-background: #303030;
|
--body-background: #303030;
|
||||||
--accent-color: #ecf0f1;
|
--accent-color: #ecf0f1;
|
||||||
--accent-color-darker: #bdc3c7;
|
--accent-color-darker: #bdc3c7;
|
||||||
@ -64,7 +64,7 @@ $defaultTagColors: #fff, #fff, #fff, #fff, #fff;
|
|||||||
|
|
||||||
--small-card-padding: 25px;
|
--small-card-padding: 25px;
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
.theme-dark {
|
||||||
--card-background: #424242;
|
--card-background: #424242;
|
||||||
--card-background-selected: rgba(255, 255, 255, 0.16);
|
--card-background-selected: rgba(255, 255, 255, 0.16);
|
||||||
--card-text-color-main: rgba(255, 255, 255, 0.9);
|
--card-text-color-main: rgba(255, 255, 255, 0.9);
|
||||||
@ -108,7 +108,7 @@ $defaultTagColors: #fff, #fff, #fff, #fff, #fff;
|
|||||||
--table-border-color: #dadada;
|
--table-border-color: #dadada;
|
||||||
--tr-even-background-color: #efefee;
|
--tr-even-background-color: #efefee;
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
.theme-dark {
|
||||||
--code-background-color: #272822;
|
--code-background-color: #272822;
|
||||||
--code-text-color: rgba(255, 255, 255, 0.9);
|
--code-text-color: rgba(255, 255, 255, 0.9);
|
||||||
|
|
||||||
@ -129,4 +129,4 @@ $defaultTagColors: #fff, #fff, #fff, #fff, #fff;
|
|||||||
--shadow-l3: 0px 10px 20px rgba(0, 0, 0, 0.04), 0px 2px 6px rgba(0, 0, 0, 0.04), 0px 0px 1px rgba(0, 0, 0, 0.04);
|
--shadow-l3: 0px 10px 20px rgba(0, 0, 0, 0.04), 0px 2px 6px rgba(0, 0, 0, 0.04), 0px 0px 1px rgba(0, 0, 0, 0.04);
|
||||||
--shadow-l4: 0px 24px 32px rgba(0, 0, 0, 0.04), 0px 16px 24px rgba(0, 0, 0, 0.04), 0px 4px 8px rgba(0, 0, 0, 0.04),
|
--shadow-l4: 0px 24px 32px rgba(0, 0, 0, 0.04), 0px 16px 24px rgba(0, 0, 0, 0.04), 0px 4px 8px rgba(0, 0, 0, 0.04),
|
||||||
0px 0px 1px rgba(0, 0, 0, 0.04);
|
0px 0px 1px rgba(0, 0, 0, 0.04);
|
||||||
}
|
}
|
||||||
|
29
assets/ts/darkmode.ts
Normal file
29
assets/ts/darkmode.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
export default () => {
|
||||||
|
const toggleDarkMode = document.getElementById('dark-mode-toggle');
|
||||||
|
const darkModeKey = 'StackDarkMode';
|
||||||
|
const darkModeEnabled = localStorage.getItem(darkModeKey) === 'true';
|
||||||
|
|
||||||
|
let darkMode = false;
|
||||||
|
|
||||||
|
if (darkModeEnabled) {
|
||||||
|
darkMode = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
darkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.body.style.setProperty('transition', 'background-color .3s ease');
|
||||||
|
|
||||||
|
toggleDarkMode.addEventListener('click', (e) => {
|
||||||
|
darkMode = !darkMode;
|
||||||
|
|
||||||
|
if (darkMode) {
|
||||||
|
document.body.classList.add('theme-dark');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
document.body.classList.remove('theme-dark');
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.setItem(darkModeKey, darkMode.toString());
|
||||||
|
})
|
||||||
|
}
|
@ -9,9 +9,12 @@
|
|||||||
import { createGallery } from "./gallery"
|
import { createGallery } from "./gallery"
|
||||||
import { getColor } from './color';
|
import { getColor } from './color';
|
||||||
import menu from './menu';
|
import menu from './menu';
|
||||||
|
import darkmode from "./darkmode";
|
||||||
|
|
||||||
let Stack = {
|
let Stack = {
|
||||||
init: () => {
|
init: () => {
|
||||||
|
darkmode();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bind menu event
|
* Bind menu event
|
||||||
*/
|
*/
|
||||||
|
@ -20,4 +20,7 @@
|
|||||||
other = "Not Found"
|
other = "Not Found"
|
||||||
|
|
||||||
[notFoundSubtitle]
|
[notFoundSubtitle]
|
||||||
other = "This page does not exist."
|
other = "This page does not exist."
|
||||||
|
|
||||||
|
[darkModeToggle]
|
||||||
|
other = "Dark Mode"
|
@ -20,4 +20,7 @@
|
|||||||
other = "404 错误"
|
other = "404 错误"
|
||||||
|
|
||||||
[notFoundSubtitle]
|
[notFoundSubtitle]
|
||||||
other = "页面不存在"
|
other = "页面不存在"
|
||||||
|
|
||||||
|
[darkModeToggle]
|
||||||
|
other = "暗色模式"
|
@ -2,6 +2,19 @@
|
|||||||
<html lang="{{ .Site.LanguageCode }}">
|
<html lang="{{ .Site.LanguageCode }}">
|
||||||
{{- partial "head/head.html" . -}}
|
{{- partial "head/head.html" . -}}
|
||||||
<body class="{{ block `body-class` . }}{{ end }}">
|
<body class="{{ block `body-class` . }}{{ end }}">
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
const darkModeKey = 'StackDarkMode';
|
||||||
|
const darkModeItem = localStorage.getItem(darkModeKey);
|
||||||
|
const darkModeEnabled = localStorage.getItem(darkModeKey) === 'true';
|
||||||
|
const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;
|
||||||
|
|
||||||
|
if(darkModeItem && darkModeEnabled || !darkModeItem && supportDarkMode){
|
||||||
|
document.body.classList.add('theme-dark');
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
|
||||||
<div class="container extended flex on-phone--column align-items--flex-start {{ block `container-class` . }}{{end}}">
|
<div class="container extended flex on-phone--column align-items--flex-start {{ block `container-class` . }}{{end}}">
|
||||||
{{ partial "sidebar/left.html" . }}
|
{{ partial "sidebar/left.html" . }}
|
||||||
<main class="main full-width">
|
<main class="main full-width">
|
||||||
|
@ -39,5 +39,11 @@
|
|||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
<li id="dark-mode-toggle">
|
||||||
|
{{ (resources.Get "icons/toggle-left.svg").Content | safeHTML }}
|
||||||
|
{{ (resources.Get "icons/toggle-right.svg").Content | safeHTML }}
|
||||||
|
<span>{{ T "darkModeToggle" }}</span>
|
||||||
|
</li>
|
||||||
</ol>
|
</ol>
|
||||||
</aside>
|
</aside>
|
Loading…
Reference in New Issue
Block a user