File size: 1,191 Bytes
88c4c60 | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 | "use client";
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { THEME_CONFIG } from "@/shared/constants/config";
const useThemeStore = create(
persist(
(set, get) => ({
theme: THEME_CONFIG.defaultTheme,
setTheme: (theme) => {
set({ theme });
applyTheme(theme);
},
toggleTheme: () => {
const currentTheme = get().theme;
const newTheme = currentTheme === "dark" ? "light" : "dark";
set({ theme: newTheme });
applyTheme(newTheme);
},
initTheme: () => {
const theme = get().theme;
applyTheme(theme);
},
}),
{
name: THEME_CONFIG.storageKey,
}
)
);
// Apply theme to document
function applyTheme(theme) {
if (typeof window === "undefined") return;
const root = document.documentElement;
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
const effectiveTheme = theme === "system" ? systemTheme : theme;
if (effectiveTheme === "dark") {
root.classList.add("dark");
} else {
root.classList.remove("dark");
}
}
export default useThemeStore;
|