This commit is contained in:
Jack Mechem 2026-05-21 21:13:06 -07:00
parent 3f178f8795
commit c6e6c5ca48
Signed by: jackmechem
SSH key fingerprint: SHA256:GjIjMAC33pzYOe+hWcX5uvgnPrVFAXSrquvt84AOJbU
20 changed files with 664 additions and 277 deletions

43
stores/useThemeStore.ts Normal file
View 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();
};
}