Skip to content

Commit

Permalink
fix: color mode change
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed May 23, 2024
1 parent 7673081 commit 2c7f00d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ const config: Config = {
theme: prismThemes.github,
darkTheme: prismThemes.dracula,
},
colorMode: {
respectPrefersColorScheme: true,
}
} satisfies Preset.ThemeConfig,
};

Expand Down
23 changes: 22 additions & 1 deletion src/components/GiscusComment.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import Giscus from "@giscus/react"
import { useEffect, useRef, useState } from "react"

const GiscusComment = () => {
const [theme, setTheme] = useState('preferred_color_scheme')
const observerLock = useRef(false)

const updateTheme = () => {
setTheme(document.documentElement.getAttribute('data-theme'))
}

useEffect(() => {
if (!observerLock.current) {
observerLock.current = true
const observer = new MutationObserver(() => {
updateTheme()
})
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['data-theme'],
})
}
}, [])

return (
<div style={{
paddingTop: '2rem',
Expand All @@ -15,7 +36,7 @@ const GiscusComment = () => {
reactionsEnabled='1'
emitMetadata='0'
inputPosition='bottom'
theme='preferred_color_scheme'
theme={theme}
lang='zh-CN'
loading='lazy'
/>
Expand Down

0 comments on commit 2c7f00d

Please sign in to comment.