diff --git a/blog.config.js b/blog.config.js index 3dbd6a1d..258ef3fd 100644 --- a/blog.config.js +++ b/blog.config.js @@ -11,6 +11,7 @@ const BLOG = { DEBUG: process.env.NEXT_PUBLIC_DEBUG || false, // 是否显示调试按钮 THEME: process.env.NEXT_PUBLIC_THEME || 'next', // 主题, 支持 ['next','hexo',"fukasawa','medium'] + THEME_SWITCH: process.env.NEXT_PUBLIC_THEME_SWITCH || false, // 是否显示切换主题按钮 LANG: 'zh-CN', // e.g 'zh-CN','en-US' see /lib/lang.js for more. SINCE: 2021, // e.g if leave this empty, current year will be used. BEI_AN: process.env.NEXT_PUBLIC_BEI_AN || '', // 备案号 闽ICP备XXXXXXX diff --git a/components/ThemeSwitch.js b/components/ThemeSwitch.js new file mode 100644 index 00000000..d4a10e7d --- /dev/null +++ b/components/ThemeSwitch.js @@ -0,0 +1,39 @@ +import { useGlobal } from '@/lib/global' +import { useRouter } from 'next/router' +import { ALL_THEME } from '@/lib/theme' + +/** + * + * @returns 主题切换 + */ +export function ThemeSwitch () { + const GlobalConfig = useGlobal() + const router = useRouter() + const { theme, setTheme } = GlobalConfig + const themeOptions = [] + ALL_THEME.forEach(t => { + themeOptions.push({ value: t, text: t }) + }) + + function switchTheme () { + const currentIndex = ALL_THEME.indexOf(theme) + const newIndex = currentIndex < ALL_THEME.length - 1 ? currentIndex + 1 : 0 + changeTheme(ALL_THEME[newIndex]) + } + /** + * 切换主题 + */ + function changeTheme (theme) { + router.query.theme = '' + setTheme(theme) + } + + return ( +