perf: 代码优化 hooks/useTheme
This commit is contained in:
parent
ee2ee7e1da
commit
63844a8f2e
@ -31,19 +31,22 @@ const themeList: IThemeList[] = [
|
|||||||
/** 正在应用的主题名称 */
|
/** 正在应用的主题名称 */
|
||||||
const activeThemeName = ref<ThemeName>(getActiveThemeName() || DEFAULT_THEME_NAME)
|
const activeThemeName = ref<ThemeName>(getActiveThemeName() || DEFAULT_THEME_NAME)
|
||||||
|
|
||||||
|
/** 设置主题 */
|
||||||
const setTheme = (value: ThemeName) => {
|
const setTheme = (value: ThemeName) => {
|
||||||
activeThemeName.value = value
|
activeThemeName.value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 在 html 根元素上挂载 class */
|
/** 在 html 根元素上挂载 class */
|
||||||
const setHtmlClassName = (value: ThemeName) => {
|
const setHtmlRootClassName = (value: ThemeName) => {
|
||||||
document.documentElement.className = value
|
document.documentElement.className = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
const initTheme = () => {
|
const initTheme = () => {
|
||||||
|
// watchEffect 来收集副作用
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
const value = activeThemeName.value
|
const value = activeThemeName.value
|
||||||
setHtmlClassName(value)
|
setHtmlRootClassName(value)
|
||||||
setActiveThemeName(value)
|
setActiveThemeName(value)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user