Skip to content

Commit 18d7508

Browse files
authored
支持切换颜色模式 (#256)
1 parent c0d1c09 commit 18d7508

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

_config.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
# You can create any custom variable you would like, and they will be accessible
1919
# in the templates via {{ site.myvariable }}.
2020

21-
remote_theme : "mmistakes/minimal-mistakes@4.26.2"
22-
minimal_mistakes_skin : "default" # "air", "aqua", "contrast", "dark", "dirt", "neon", "mint", "plum", "sunrise"
21+
remote_theme: "mmistakes/minimal-mistakes@4.26.2"
22+
minimal_mistakes_skin: "default" # "air", "aqua", "contrast", "dark", "dirt", "neon", "mint", "plum", "sunrise"
23+
minimal_mistakes_skin_dark: "dark" # "air", "aqua", "contrast", "dark", "dirt", "neon", "mint", "plum", "sunrise"
2324
locale: "zh-CN"
2425

2526
title: Hello Minecraft! Launcher 帮助文档
@@ -126,6 +127,9 @@ compress_html:
126127
ignore:
127128
envs: development
128129

130+
head_scripts:
131+
- /assets/js/theme.js
132+
129133
footer:
130134
links:
131135
- label: "粤ICP备18071565号"
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
---
2-
# Only the main Sass file needs front matter (the dashes are enough)
3-
search: false
4-
---
5-
6-
@charset "utf-8";
7-
8-
$sans-serif: -apple-system, BlinkMacSystemFont, "Roboto", "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
9-
@import "minimal-mistakes/skins/{{ site.minimal_mistakes_skin | default: 'default' }}"; // skin
10-
@import "minimal-mistakes"; // main partials
1+
---
2+
search: false
3+
---
4+
5+
@charset "utf-8";
6+
7+
$sans-serif: -apple-system, BlinkMacSystemFont, "Roboto", "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
8+
@import "minimal-mistakes/skins/{{ site.minimal_mistakes_skin_dark | default: 'dark' }}"; // skin
9+
@import "minimal-mistakes"; // main partials

assets/js/theme.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
layout: null
3+
---
4+
var darkTheme = document.createElement("link");
5+
darkTheme.rel = "stylesheet alternate";
6+
darkTheme.href = "{{ '/assets/css/main.dark.css' | relative_url }}";
7+
document.head.appendChild(darkTheme);
8+
window.addEventListener("DOMContentLoaded", function () {
9+
var list = document.querySelector(".masthead .visible-links");
10+
if (!list) return;
11+
var mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
12+
function handler() {
13+
darkTheme.rel = mediaQuery.matches ? "stylesheet" : "stylesheet alternate";
14+
}
15+
var current = 0;
16+
var modes = ["light", "dark", "auto"];
17+
var modeNames = ["亮色", "暗色", "自动"];
18+
var switcher = document.createElement("a");
19+
switcher.className = "masthead__menu-item";
20+
switcher.innerText = modeNames[current];
21+
switcher.href = "javascript:;";
22+
switcher.onclick = function () {
23+
themeApply(current + 1);
24+
}
25+
list.appendChild(switcher);
26+
function themeApply(index) {
27+
index = (Number(index) || 0) % modes.length;
28+
if (index === current) return;
29+
if (modes[current] === "auto") mediaQuery.removeEventListener("change", handler);
30+
current = index;
31+
var mode = modes[current];
32+
switcher.innerText = modeNames[current];
33+
localStorage.setItem("theme", current);
34+
if (mode === "light") darkTheme.rel = "stylesheet alternate";
35+
else if (mode === "dark") darkTheme.rel = "stylesheet";
36+
else {
37+
mediaQuery.addEventListener("change", handler);
38+
handler();
39+
}
40+
}
41+
themeApply(localStorage.getItem("theme"));
42+
window.addEventListener("storage", function (event) {
43+
event.key === "theme" && themeApply(event.newValue);
44+
});
45+
});

0 commit comments

Comments
 (0)