feat: local storage theme

This commit is contained in:
2026-01-31 00:16:57 +08:00
parent 84ba18b34c
commit 9043814c02
2 changed files with 65 additions and 39 deletions

View File

@@ -1,47 +1,14 @@
enum GlobalTheme { import { useThemeStore, GlobalTheme } from "@/stores/ThemeStore";
Light = "light",
Dark = "dark", const themeStore = useThemeStore();
Cupcake = "cupcake",
Bumblebee = "bumblebee",
Emerald = "emerald",
Corporate = "corporate",
Synthwave = "synthwave",
Retro = "retro",
Cyberpunk = "cyberpunk",
Valentine = "valentine",
Halloween = "halloween",
Garden = "garden",
Forest = "forest",
Aqua = "aqua",
Lofi = "lofi",
Pastel = "pastel",
Fantasy = "fantasy",
Wireframe = "wireframe",
Black = "black",
Luxury = "luxury",
Dracula = "dracula",
Cmyk = "cmyk",
Autumn = "autumn",
Business = "business",
Acid = "acid",
Lemonade = "lemonade",
Night = "night",
Coffee = "coffee",
Winter = "winter",
Dim = "dim",
Nord = "nord",
Sunset = "sunset",
Caramellatte = "caramellatte",
Abyss = "abyss",
Silk = "silk",
}
const optionalThemes = Object.values(GlobalTheme); const optionalThemes = Object.values(GlobalTheme);
const curGlobalTheme = ref(GlobalTheme.Light); // 一定不要通过改变 curGlobalTheme 来改变主题,而要通过 changeGlobalTheme 来改变主题
const curGlobalTheme = computed(() => themeStore.theme);
function changeGlobalTheme(theme: GlobalTheme) { function changeGlobalTheme(theme: GlobalTheme) {
curGlobalTheme.value = theme; themeStore.setTheme(theme);
} }
export const useGlobalThemeHook = () => ({ export const useGlobalThemeHook = () => ({

59
src/stores/ThemeStore.ts Normal file
View File

@@ -0,0 +1,59 @@
import { ref } from "vue";
import { defineStore } from "pinia";
export enum GlobalTheme {
Light = "light",
Dark = "dark",
Cupcake = "cupcake",
Bumblebee = "bumblebee",
Emerald = "emerald",
Corporate = "corporate",
Synthwave = "synthwave",
Retro = "retro",
Cyberpunk = "cyberpunk",
Valentine = "valentine",
Halloween = "halloween",
Garden = "garden",
Forest = "forest",
Aqua = "aqua",
Lofi = "lofi",
Pastel = "pastel",
Fantasy = "fantasy",
Wireframe = "wireframe",
Black = "black",
Luxury = "luxury",
Dracula = "dracula",
Cmyk = "cmyk",
Autumn = "autumn",
Business = "business",
Acid = "acid",
Lemonade = "lemonade",
Night = "night",
Coffee = "coffee",
Winter = "winter",
Dim = "dim",
Nord = "nord",
Sunset = "sunset",
Caramellatte = "caramellatte",
Abyss = "abyss",
Silk = "silk",
}
export const useThemeStore = defineStore(
"theme",
() => {
const theme = ref<GlobalTheme>(GlobalTheme.Light);
const setTheme = (newTheme: GlobalTheme) => {
theme.value = newTheme;
};
return {
theme,
setTheme,
};
},
{
persist: true,
},
);