This commit is contained in:
zuowei1216
2025-12-30 14:46:22 +08:00
parent 6c73b31100
commit 12395d8eca
181 changed files with 1255 additions and 114 deletions

View File

@@ -0,0 +1,40 @@
import { ref, onMounted, onUnmounted } from 'vue';
import { initThemeListener } from '@/utils/theme';
export function useTheme() {
const isDark = ref(true);
// 简易的判断当前是否是深色主题的逻辑
// 实际逻辑由 utils/theme.ts 中的 updateTheme 处理,它会更新 body 的 arco-theme 属性
const checkTheme = () => {
isDark.value = document.body.getAttribute('arco-theme') === 'dark';
};
onMounted(() => {
// 初始化并开始监听
initThemeListener();
// 初始检查
checkTheme();
// 监听 body 属性变化 (以便在 theme.ts 更新 body 属性时我们能感知到)
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'arco-theme') {
checkTheme();
}
});
});
observer.observe(document.body, { attributes: true });
// Cleanup
onUnmounted(() => {
observer.disconnect();
});
});
return {
isDark
};
}