refactor(toggleThemes): move all theme toggle code to toggleThemes.js and all theme related CSS code to themes.css

This commit is contained in:
2026-03-10 19:34:18 +08:00
parent a93cfaa45f
commit 37af87f5af
5 changed files with 111 additions and 65 deletions

37
src/utils/toggleThemes.js Normal file
View File

@@ -0,0 +1,37 @@
import { ref, onMounted, watch } from 'vue';
export function useTheme() {
const isDarkMode = ref(false);
const updateTheme = () => {
if (isDarkMode.value) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
};
onMounted(() => {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
isDarkMode.value = savedTheme === 'dark';
} else {
isDarkMode.value = window.matchMedia('(prefers-color-scheme: dark)').matches;
}
updateTheme();
});
watch(isDarkMode, () => {
updateTheme();
localStorage.setItem('theme', isDarkMode.value ? 'dark' : 'light');
});
const toggleTheme = () => {
isDarkMode.value = !isDarkMode.value;
};
return {
isDarkMode,
toggleTheme
};
}