shinroute / src /store /themeStore.ts
shinmentakezo07
Add OmniRoute codebase without binary assets
9e4583c
Raw
History Blame Contribute Delete
1.34 kB
"use client";
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { THEME_CONFIG } from "@/shared/constants/config";
interface ThemeState {
theme: string;
setTheme: (theme: string) => void;
toggleTheme: () => void;
initTheme: () => void;
}
const useThemeStore = create<ThemeState>()(
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: string) {
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;