Skip to content

Commit 1dadf35

Browse files
committed
refactor: darkmode
1 parent 828afd8 commit 1dadf35

File tree

12 files changed

+90
-140
lines changed

12 files changed

+90
-140
lines changed

public/index.html

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,35 @@
33

44
<head>
55
<script>
6+
</script>
7+
<script>
8+
// load for dev
69
if ('%NODE_ENV%' === 'development') {
710
function loadCss(filename) {
811
var cssNode = document.createElement("script");
912
cssNode.setAttribute("src", filename);
1013
document.getElementsByTagName("head")[0].appendChild(cssNode);
1114
}
1215
loadCss("https://cdn.tailwindcss.com/3.3.0")
13-
}
16+
}
17+
18+
// dynamic load a default theme
19+
var head = document.head;
20+
var link = document.createElement("link");
21+
22+
link.type = "text/css";
23+
link.rel = "stylesheet";
24+
25+
if (localStorage.getItem("darkmode").toLowerCase() === "true") {
26+
link.href = '/theme-dark.css';
27+
}
28+
else {
29+
link.href = '/theme.css';
30+
}
31+
32+
head.appendChild(link);
1433
</script>
15-
34+
1635
<!-- Google tag (gtag.js) -->
1736
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NTJ9NFK1RC"></script>
1837
<script>
@@ -25,7 +44,8 @@
2544
<!-- <script src="https://cdn.tailwindcss.com/3.2.6"></script> -->
2645

2746
<!-- katex -->
28-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-wcIxkf4k558AjM3Yz3BBFQUbk/zgIYC2R0QpeeYb+TwlBVMrlgLqwRjRtGZiK7ww" crossorigin="anonymous">
47+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
48+
integrity="sha384-wcIxkf4k558AjM3Yz3BBFQUbk/zgIYC2R0QpeeYb+TwlBVMrlgLqwRjRtGZiK7ww" crossorigin="anonymous">
2949

3050
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
3151
<link rel="preconnect" href="https://fonts.gstatic.com">
@@ -55,6 +75,8 @@
5575
</head>
5676

5777
<body>
78+
79+
5880
<noscript>
5981
You need to enable JavaScript to run this app.<br />
6082
您需要啟用JavaScript來瀏覽這個部落格。
@@ -71,13 +93,4 @@
7193
To create a production bundle, use `npm run build` or `yarn build`.
7294
-->
7395
</body>
74-
75-
<!-- <script src="https://utteranc.es/client.js"
76-
repo="p208p2002/blog"
77-
issue-term="pathname"
78-
theme="github-light"
79-
crossorigin="anonymous"
80-
async>
81-
</script> -->
82-
8396
</html>

public/theme-dark.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
:root {
2+
--main-1:#6b84b5;
3+
--main-2:#6b84b5;
4+
--main-3:#6b84b5;
5+
--support-1:#40464c;
6+
--support-2:#40464c;
7+
--support-3:#40464c;
8+
--text-1:#c0c0c0;
9+
--text-2:#c0c0c0;
10+
--text-3:#c0c0c0;
11+
--main-bg-1:#343a40;
12+
}
13+
14+
.theme-dark img {
15+
filter: invert(85%);
16+
}
17+
18+
/* do not apply img filter to comments block */
19+
#comments img {
20+
filter: invert(0%);
21+
}

public/theme.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:root {
2+
--main-1:#2A4F96;
3+
--main-2:#3563BD;
4+
--main-3:#698ed5;
5+
--support-1:#a6a6a6;
6+
--support-2:#e3e3e3;
7+
--support-3:#f0f0f0;
8+
--text-1:#464646;
9+
--text-2:#878787;
10+
--text-3:#f4f4f4;
11+
--main-bg-1:#ffffff;
12+
}

src/dark-mode.css

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/index.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
@import './dark-mode.css';
2-
31
html, body, #root {
42
margin: 0;
53
-webkit-font-smoothing: antialiased;

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { BLOG_NAME } from './configs/general'
1111
import MdRender from './modules/MdRender';
1212
import { Helmet } from 'react-helmet'
1313
import { AppState } from './AppState'
14-
import './theme.css'
1514

1615
export const AppStateContext = React.createContext();
1716

src/modules/CornerMenu/index.jsx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,39 @@ import { MdOutlineLightMode, MdOutlineDarkMode } from 'react-icons/md'
66
// eslint-disable-next-line
77
import { HOME_PAGE, GITHUB } from '../../configs/general'
88
import Darkmode from 'drkmd-js'
9-
import { useEffect,useState } from 'react'
9+
import { useEffect, useState } from 'react'
1010

1111

1212

1313
function Index() {
1414
let dm = new Darkmode()
15-
15+
16+
// eslint-disable-next-line
17+
const [theme, setTheme] = useState(dm.currentTheme())
18+
1619
useEffect(() => {
1720
dm.attach()
18-
// eslint-disable-next-line
21+
// eslint-disable-next-line
1922
}, [])
2023

21-
// eslint-disable-next-line
22-
const [theme,setTheme] = useState(dm.currentTheme())
24+
useEffect(() => {
25+
var head = document.head;
26+
var link = document.createElement("link");
27+
28+
link.type = "text/css";
29+
link.rel = "stylesheet";
30+
31+
if(theme === "dark"){
32+
link.href = '/theme-dark.css';
33+
}
34+
else{
35+
link.href = '/theme.css';
36+
}
37+
38+
head.appendChild(link);
39+
return () => { head.removeChild(link); }
40+
41+
}, [theme])
2342

2443
return (
2544
<div id="Corner-Menu">

src/modules/MdRender/index.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.post-title{
2+
color: var(--text-1);
3+
}
14
.document-info{
25
margin-top: 10px;
36
margin-bottom: 25px;

src/modules/MdRender/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default function MdRender({ doc_id }) {
201201

202202
</Helmet>
203203
<br />
204-
<h1 className="text-3xl font-bold mb-3">{postTitle}</h1>
204+
<h1 className="post-title text-3xl font-bold mb-3">{postTitle}</h1>
205205
<div className="document-info">
206206
<div className="post-date text-zinc-500">
207207
日期:&nbsp;{date}

src/modules/Search/index.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#Search-Bar {
22
margin-top: 16px;
33
margin-bottom: 32px;
4+
background-color: var(--main-bg-1);
45
}
56

67
#Search-Bar input[type=text] {
@@ -10,6 +11,7 @@
1011
height: 24px;
1112
padding-left: 5px;
1213
margin-right: 5px;
14+
background-color: var(--main-bg-1);
1315
}
1416

1517
#Search-Bar input[type=text]:focus{
@@ -20,4 +22,5 @@
2022
height: 28px;
2123
border-style: solid;
2224
border-width: 0px 0px 0px 0px;
23-
}
25+
color: var(--text-1);
26+
}

0 commit comments

Comments
 (0)