Skip to content

Commit 5573080

Browse files
committed
port theme.*.styl to color-definations.scss
1 parent d1c4745 commit 5573080

10 files changed

+63
-98
lines changed

_config.example.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
keywords: Blog,博客,Hexo
44
author: Yourname
55
description: A simple and beautiful blog
6-
defaultTheme: light-mode # light-mode or dark-mode
6+
defaultTheme: light-mode # light-mode or dark-mode or default
77

88
# icon
99
# If the is size is incorrect, please edit /source/css/style.styl

layout/partial/head.pug

-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ meta(content="black", name="apple-mobile-web-app-status-bar-style")
1818
meta(content="telephone=no", name="format-detection")
1919

2020
meta#site_data_static(data-url=url_for("/"))
21-
meta#default-theme(data=theme.defaultTheme == "dark-mode" ? "dark" : "light")
2221

2322
meta(name="renderer", content="webkit")
2423
link(rel="shortcut icon", type="image/x-icon", href=url_for(theme.favicon))
2524

2625
link(rel="stylesheet", href=url_for("js_complied/bundle.css"))
27-
link(rel="stylesheet", href=url_for("css/theme/" + (theme.defaultTheme == "dark-mode" ? "dark" : "light") + ".css"))
2826
link(rel="stylesheet", href=url_for("css/style.css"))
2927
link(rel="stylesheet", href=url_for("css/font-awesome.min.css"))
3028
link(rel="alternate", type="application/atom+xml", title="ATOM 1.0", href="/atom.xml")

layout/partial/layout.pug

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
doctype
2-
html(lang="zh-CN")
2+
html(lang="zh-CN", theme=theme.defaultTheme)
33
head
44
include head
55

source/css/theme/base.styl

Whitespace-only changes.

source/css/theme/dark.styl

-26
This file was deleted.

source/css/theme/light.styl

-26
This file was deleted.

source/css/theme/mixin_functions.styl

-16
This file was deleted.

src/anatolo/dark-light-toggle.ts

+12-26
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
1-
import { SiteStatic } from './site-static';
2-
3-
function loadStyles(url: string) {
4-
var link = document.createElement('link');
5-
link.id = 'themecss';
6-
link.rel = 'stylesheet';
7-
link.type = 'text/css';
8-
link.href = url;
9-
var head = document.getElementsByTagName('head')[0];
10-
head.appendChild(link);
11-
}
12-
13-
function getDefaultTheme() {
14-
return document.getElementById('default-theme')?.getAttribute('data');
1+
function getTheme() {
2+
return document.querySelector('html')!.getAttribute('theme') ?? 'default';
153
}
164

175
function setTheme() {
18-
if (document.getElementById('themecss')) {
19-
document.getElementById('themecss')?.remove();
20-
}
21-
if (localStorage['themeChanged'] && getDefaultTheme() == 'light') {
22-
loadStyles(SiteStatic.url_for('css/theme/dark.css'));
23-
} else if (localStorage['themeChanged'] && getDefaultTheme() == 'dark') {
24-
loadStyles(SiteStatic.url_for('css/theme/light.css'));
25-
} else {
26-
loadStyles(SiteStatic.url_for(`css/theme/${getDefaultTheme()}.css`));
6+
const theme = localStorage.getItem('theme');
7+
if (theme) {
8+
document.querySelector('html')!.setAttribute('theme', theme);
279
}
2810
}
2911

3012
setTheme();
3113

3214
export function darkLightToggle() {
33-
if (!localStorage['themeChanged']) {
34-
localStorage.setItem('themeChanged', 'true');
15+
let themeNow = getTheme();
16+
if (themeNow === 'default') {
17+
themeNow = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'true';
18+
}
19+
if (themeNow === 'dark') {
20+
localStorage.setItem('theme', 'light');
3521
} else {
36-
localStorage.removeItem('themeChanged');
22+
localStorage.setItem('theme', 'dark');
3723
}
3824
setTheme();
3925
}

src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './scss/color-defination.scss';
12
import './scss/blog_basic.scss';
23
import './scss/highlight.scss';
34
import './scss/style.scss';

src/scss/color-defination.scss

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@mixin percentageColor($name, $c1, $c2) {
2+
@for $i from 1 to 20 {
3+
#{--#{$name}-#{$i*50}}: mix($c1, $c2, $i * 5%);
4+
}
5+
}
6+
7+
@mixin genColor($name, $c1, $c2) {
8+
#{--#{$name}-rgb}: join($c1, (), comma);
9+
#{--#{$name}}: rgb($c1);
10+
@include percentageColor($name, rgb($c1), rgb($c2));
11+
12+
#{--#{$name}-high}: mix(rgb($c1), rgb($c2), 90%);
13+
#{--#{$name}-midium}: mix(rgb($c1), rgb($c2), 70%);
14+
#{--#{$name}-low-mid}: mix(rgb($c1), rgb($c2), 50%);
15+
#{--#{$name}-low}: mix(rgb($c1), rgb($c2), 20%);
16+
#{--#{$name}-very-low}: mix(rgb($c1), rgb($c2), 0.05%);
17+
}
18+
19+
@mixin darkTheme() {
20+
$primary: 255 255 255;
21+
$secondary: 40 40 40;
22+
@include genColor('primary', $primary, $secondary);
23+
@include genColor('secondary', $secondary, $primary);
24+
}
25+
26+
@mixin lightTheme() {
27+
$primary: 22 22 22;
28+
$secondary: 255 255 255;
29+
@include genColor('primary', $primary, $secondary);
30+
@include genColor('secondary', $secondary, $primary);
31+
}
32+
33+
html[theme='dark'] {
34+
@include darkTheme();
35+
}
36+
37+
html[theme='light'] {
38+
@include lightTheme();
39+
}
40+
41+
html[theme='default'] {
42+
@include lightTheme();
43+
}
44+
@media (prefers-color-scheme: dark) {
45+
html[theme='default'] {
46+
@include darkTheme();
47+
}
48+
}

0 commit comments

Comments
 (0)