Files
davila7--claude-code-templates/dashboard/src/lib/theme.ts
T
wehub-resource-sync bb5c75ce05
Component Security Validation / Security Audit (push) Has been cancelled
Deploy to Cloudflare Pages / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:58 +08:00

48 lines
1.2 KiB
TypeScript

// Theme management utility
export type Theme = 'light' | 'dark';
export const THEME_KEY = 'claude-theme';
export function getTheme(): Theme {
if (typeof window === 'undefined') return 'dark';
const stored = localStorage.getItem(THEME_KEY);
if (stored === 'light' || stored === 'dark') {
return stored;
}
// Detect OS preference on first visit
if (window.matchMedia?.('(prefers-color-scheme: light)').matches) {
return 'light';
}
return 'dark';
}
export function setTheme(theme: Theme): void {
if (typeof window === 'undefined') return;
localStorage.setItem(THEME_KEY, theme);
applyTheme(theme);
}
export function toggleTheme(): Theme {
const current = getTheme();
const next = current === 'dark' ? 'light' : 'dark';
setTheme(next);
return next;
}
export function applyTheme(theme: Theme): void {
if (typeof document === 'undefined') return;
const html = document.documentElement;
// Remove both classes first
html.classList.remove('light', 'dark');
// Add the correct class
html.classList.add(theme);
}
// Initialize theme on page load
export function initTheme(): void {
if (typeof window === 'undefined') return;
const theme = getTheme();
applyTheme(theme);
}