Template
1
0
mirror of https://github.com/un-pany/v3-admin-vite.git synced 2025-04-21 11:29:20 +08:00

fix: useTheme use single ref (#21)

This commit is contained in:
a-leaf 2022-10-21 11:25:06 +08:00 committed by GitHub
parent f3fd669cad
commit 12cb7cfcc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,36 +9,37 @@ interface IThemeList {
/** 注册的主题名称, 其中 normal 是必填的 */
export type ThemeName = "normal" | "dark"
/** 主题列表 */
const themeList: IThemeList[] = [
{
title: "默认",
name: "normal"
},
{
title: "黑暗",
name: "dark"
}
]
/** 正在应用的主题名称 */
const activeThemeName = ref<ThemeName>(getActiveThemeName() || "normal")
const initTheme = () => {
setHtmlClassName(activeThemeName.value)
}
const setTheme = (value: ThemeName) => {
activeThemeName.value = value
setHtmlClassName(activeThemeName.value)
setActiveThemeName(activeThemeName.value)
}
/** 在 html 根元素上挂载 class */
const setHtmlClassName = (value: ThemeName) => {
document.documentElement.className = value
}
/** 主题 hook */
export function useTheme() {
/** 主题列表 */
const themeList: IThemeList[] = [
{
title: "默认",
name: "normal"
},
{
title: "黑暗",
name: "dark"
}
]
/** 正在应用的主题名称 */
const activeThemeName = ref<ThemeName>(getActiveThemeName() || "normal")
const initTheme = () => {
setHtmlClassName(activeThemeName.value)
}
const setTheme = (value: ThemeName) => {
activeThemeName.value = value
setHtmlClassName(activeThemeName.value)
setActiveThemeName(activeThemeName.value)
}
/** 在 html 根元素上挂载 class */
const setHtmlClassName = (value: ThemeName) => {
document.documentElement.className = value
}
return { themeList, activeThemeName, initTheme, setTheme }
}