useThemeVariables.uts 1023 Bytes
// 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
	};
}