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 ( +
+
+ + 切换主题:{theme} +
+
+ ) +} diff --git a/pages/_app.js b/pages/_app.js index 143b0d2e..ecf1c2ad 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -15,6 +15,7 @@ import 'katex/dist/katex.min.css' import dynamic from 'next/dynamic' import { GlobalContextProvider } from '@/lib/global' import { DebugPanel } from '@/components/DebugPanel' +import { ThemeSwitch } from '@/components/ThemeSwitch' const Ackee = dynamic(() => import('@/components/Ackee'), { ssr: false }) const Gtag = dynamic(() => import('@/components/Gtag'), { ssr: false }) @@ -24,6 +25,7 @@ const GoogleAdsense = dynamic(() => import('@/components/GoogleAdsense'), { ssr: const MyApp = ({ Component, pageProps }) => { return ( + {BLOG.THEME_SWITCH && } {BLOG.DEBUG && } {BLOG.ANALYTICS_ACKEE_TRACKER && } {BLOG.ANALYTICS_GOOGLE_ID && }