Skip to content

Commit d6979e1

Browse files
committed
remember is_dark
1 parent d14a1f0 commit d6979e1

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

_layout.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import Sidebar from './_sidebar.tsx';
77
const Layout: PagicLayout = ({ config, title, content, script, sidebar, outputPath }) => {
88
const [isDark, setIsDark] = React.useState(
99
// @ts-ignore
10-
!!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)
10+
typeof Deno === 'undefined' ? document.documentElement.classList.contains('is_dark') : false
1111
);
1212
return (
13-
<html className={isDark ? 'dark' : ''}>
13+
<html className={isDark ? 'is_dark' : ''}>
1414
<head>
1515
<script async src="https://www.google-analytics.com/analytics.js" />
1616
<script
@@ -22,8 +22,20 @@ const Layout: PagicLayout = ({ config, title, content, script, sidebar, outputPa
2222
<meta charSet="utf-8" />
2323

2424
<link rel="shortcut icon" type="image/png" href="//xcatliu.github.io/favicon.ico" />
25-
<link rel="stylesheet" href={isDark ? '/assets/prism_tomorrow.css' : '/assets/prism.css'} />
25+
<link id="prismTheme" rel="stylesheet" href={isDark ? '/assets/prism_tomorrow.css' : '/assets/prism.css'} />
2626
<link rel="stylesheet" href="/assets/index.css" />
27+
28+
<script
29+
dangerouslySetInnerHTML={{
30+
__html: `
31+
let shouldSetIsDark = document.cookie.includes('is_dark=1') ? true : document.cookie.includes('is_dark=0') ? false : window.matchMedia('(prefers-color-scheme: dark)').matches
32+
if (shouldSetIsDark) {
33+
document.documentElement.classList.add('is_dark');
34+
document.getElementById('prismTheme').href = "/assets/prism_tomorrow.css";
35+
}
36+
`
37+
}}
38+
/>
2739
</head>
2840
<body>
2941
<header>
@@ -47,6 +59,8 @@ const Layout: PagicLayout = ({ config, title, content, script, sidebar, outputPa
4759
onClick={(e) => {
4860
e.preventDefault();
4961
setIsDark(!isDark);
62+
// @ts-ignore
63+
document.cookie = `is_dark=${!isDark ? '1' : '0'}; expires=Tue, 19 Jun 2038 03:14:07 UTC; path=/`;
5064
}}
5165
>
5266
{isDark ? '关闭黑暗模式' : '开启黑暗模式'}

assets/index.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
--width-aside: 288px;
1414
}
1515

16-
html.dark {
16+
html.is_dark {
1717
--color-text: #aaa;
1818
--color-text-aside: #666;
1919
--color-muted: #777;
@@ -24,6 +24,15 @@ html.dark {
2424
--color-background-aside-hover: #111;
2525
--color-border: #333;
2626
--color-header-shadow: rgba(0, 0, 0, 0.8);
27+
28+
}
29+
30+
html.is_dark img {
31+
opacity: .8;
32+
transition: opacity .3s ease-in-out;
33+
}
34+
html.is_dark img:hover {
35+
opacity: 1;
2736
}
2837

2938
/* #region reset */

0 commit comments

Comments
 (0)