Redesign
This commit is contained in:
parent
3f178f8795
commit
c6e6c5ca48
20 changed files with 664 additions and 277 deletions
43
stores/useThemeStore.ts
Normal file
43
stores/useThemeStore.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
"use client";
|
||||
|
||||
import { useSyncExternalStore } from "react";
|
||||
|
||||
const STORAGE_KEY = "theme";
|
||||
|
||||
function getTheme(): "light" | "dark" {
|
||||
if (typeof document === "undefined") return "light";
|
||||
return document.documentElement.classList.contains("dark-theme") ? "dark" : "light";
|
||||
}
|
||||
|
||||
function getThemeSnapshot(): "light" | "dark" {
|
||||
return getTheme();
|
||||
}
|
||||
|
||||
function getServerSnapshot(): "light" | "dark" {
|
||||
return "light";
|
||||
}
|
||||
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function subscribe(cb: () => void) {
|
||||
listeners.add(cb);
|
||||
return () => listeners.delete(cb);
|
||||
}
|
||||
|
||||
function notifyListeners() {
|
||||
listeners.forEach((cb) => cb());
|
||||
}
|
||||
|
||||
export function useTheme(): "light" | "dark" {
|
||||
return useSyncExternalStore(subscribe, getThemeSnapshot, getServerSnapshot);
|
||||
}
|
||||
|
||||
export function useSetTheme() {
|
||||
return function setTheme() {
|
||||
const isDark = document.documentElement.classList.toggle("dark-theme");
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, isDark ? "dark" : "light");
|
||||
} catch {}
|
||||
notifyListeners();
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue