useThemeVariables.uts
1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// composables/useThemeVariables.uts
// CSS 变量主题 Composable
import { computed, onMounted, ref } from "vue";
import { getThemeVariablesStyle } from "@/utils/theme-variables.uts";
import { useTheme } from "./useTheme.uts";
/**
* CSS 变量主题 Composable
* 提供响应式的 CSS 变量样式对象
*/
export function useThemeVariables() {
const { currentThemeName, autoSelectTheme } = useTheme();
const themeVars = ref<Record<string, string>>({});
/**
* 更新 CSS 变量
*/
function updateThemeVariables(): void {
themeVars.value = getThemeVariablesStyle();
}
// 初始化时设置 CSS 变量
onMounted(() => {
updateThemeVariables();
});
// 监听主题变化,自动更新 CSS 变量
const themeVarsComputed = computed(() => {
// 当主题名称变化时,重新计算 CSS 变量
return getThemeVariablesStyle();
});
return {
// CSS 变量样式对象(用于 :style 绑定)
themeVars: themeVarsComputed,
// 手动更新 CSS 变量
updateThemeVariables
};
}