mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-23 04:09:49 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
"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;
|
||||
|
||||
Reference in New Issue
Block a user