diff --git a/components/DarkModeButton.js b/components/DarkModeButton.js new file mode 100644 index 00000000..a4d36989 --- /dev/null +++ b/components/DarkModeButton.js @@ -0,0 +1,21 @@ +import { useGlobal } from '@/lib/global' +import { saveDarkModeToCookies } from '@/lib/theme' + +const DarkModeButton = (props) => { + const { isDarkMode, updateDarkMode } = useGlobal() + // 用户手动设置主题 + const handleChangeDarkMode = () => { + const newStatus = !isDarkMode + saveDarkModeToCookies(newStatus) + updateDarkMode(newStatus) + const htmlElement = document.getElementsByTagName('html')[0] + htmlElement.classList?.remove(newStatus ? 'light' : 'dark') + htmlElement.classList?.add(newStatus ? 'dark' : 'light') + } + + return
+ +
+} +export default DarkModeButton diff --git a/components/ThemeSwitch.js b/components/ThemeSwitch.js index 12d81812..f766599c 100644 --- a/components/ThemeSwitch.js +++ b/components/ThemeSwitch.js @@ -9,8 +9,7 @@ export function ThemeSwitch () { return (
- - 切换主题:{theme} + {theme}
) diff --git a/themes/example/LayoutBase.js b/themes/example/LayoutBase.js index 1f357ad8..c99582f4 100644 --- a/themes/example/LayoutBase.js +++ b/themes/example/LayoutBase.js @@ -15,7 +15,7 @@ import JumpToTopButton from './components/JumpToTopButton' const LayoutBase = props => { const { children, meta } = props return ( -
+
{/* 顶栏LOGO */}
@@ -24,7 +24,7 @@ const LayoutBase = props => {