feat: initial support for darkmode toggle

This commit is contained in:
Jimmy Cai 2020-09-19 00:23:40 +02:00
parent 3a003eadca
commit 0f9682e4b4
No known key found for this signature in database
GPG Key ID: 3EA408E527F37B18
11 changed files with 106 additions and 7 deletions

View 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

View 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

View File

@ -128,6 +128,7 @@
flex-direction: column;
margin-top: 25px;
overflow-y: auto;
flex-grow: 1;
@media (min-width: $on-desktop-large) {
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;
}
}

View File

@ -26,7 +26,7 @@
width: 25%;
margin-right: 1%;
padding: var(--main-top-padding) 15px;
max-height: 100vh;
height: 100vh;
}
@media (min-width: $on-desktop) {

View File

@ -20,7 +20,7 @@ $defaultTagColors: #fff, #fff, #fff, #fff, #fff;
--section-separation: 40px;
@media (prefers-color-scheme: dark) {
.theme-dark {
--body-background: #303030;
--accent-color: #ecf0f1;
--accent-color-darker: #bdc3c7;
@ -64,7 +64,7 @@ $defaultTagColors: #fff, #fff, #fff, #fff, #fff;
--small-card-padding: 25px;
@media (prefers-color-scheme: dark) {
.theme-dark {
--card-background: #424242;
--card-background-selected: rgba(255, 255, 255, 0.16);
--card-text-color-main: rgba(255, 255, 255, 0.9);
@ -108,7 +108,7 @@ $defaultTagColors: #fff, #fff, #fff, #fff, #fff;
--table-border-color: #dadada;
--tr-even-background-color: #efefee;
@media (prefers-color-scheme: dark) {
.theme-dark {
--code-background-color: #272822;
--code-text-color: rgba(255, 255, 255, 0.9);

29
assets/ts/darkmode.ts Normal file
View 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());
})
}

View File

@ -9,9 +9,12 @@
import { createGallery } from "./gallery"
import { getColor } from './color';
import menu from './menu';
import darkmode from "./darkmode";
let Stack = {
init: () => {
darkmode();
/**
* Bind menu event
*/

View File

@ -21,3 +21,6 @@
[notFoundSubtitle]
other = "This page does not exist."
[darkModeToggle]
other = "Dark Mode"

View File

@ -21,3 +21,6 @@
[notFoundSubtitle]
other = "页面不存在"
[darkModeToggle]
other = "暗色模式"

View File

@ -2,6 +2,19 @@
<html lang="{{ .Site.LanguageCode }}">
{{- partial "head/head.html" . -}}
<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}}">
{{ partial "sidebar/left.html" . }}
<main class="main full-width">

View File

@ -39,5 +39,11 @@
</a>
</li>
{{ 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>
</aside>