@@ -7,10 +7,10 @@ import Sidebar from './_sidebar.tsx';
7
7
const Layout : PagicLayout = ( { config, title, content, script, sidebar, outputPath } ) => {
8
8
const [ isDark , setIsDark ] = React . useState (
9
9
// @ts -ignore
10
- ! ! ( window . matchMedia && window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches )
10
+ typeof Deno === 'undefined' ? document . documentElement . classList . contains ( 'is_dark' ) : false
11
11
) ;
12
12
return (
13
- < html className = { isDark ? 'dark ' : '' } >
13
+ < html className = { isDark ? 'is_dark ' : '' } >
14
14
< head >
15
15
< script async src = "https://www.google-analytics.com/analytics.js" />
16
16
< script
@@ -22,8 +22,20 @@ const Layout: PagicLayout = ({ config, title, content, script, sidebar, outputPa
22
22
< meta charSet = "utf-8" />
23
23
24
24
< 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' } />
26
26
< 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
+ />
27
39
</ head >
28
40
< body >
29
41
< header >
@@ -47,6 +59,8 @@ const Layout: PagicLayout = ({ config, title, content, script, sidebar, outputPa
47
59
onClick = { ( e ) => {
48
60
e . preventDefault ( ) ;
49
61
setIsDark ( ! isDark ) ;
62
+ // @ts -ignore
63
+ document . cookie = `is_dark=${ ! isDark ? '1' : '0' } ; expires=Tue, 19 Jun 2038 03:14:07 UTC; path=/` ;
50
64
} }
51
65
>
52
66
{ isDark ? '关闭黑暗模式' : '开启黑暗模式' }
0 commit comments