Files
NotionNext/components/ThemeSwitch.js
2022-03-16 13:00:18 +08:00

40 lines
992 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 (
<div draggable="true" className="fixed left-4 bottom-12 text-white bg-black rounded z-50">
<div className="p-2 cursor-pointer" onClick={switchTheme}>
<i className="fas fa-sync mr-1" />
切换主题{theme}
</div>
</div>
)
}