/** * Theme Provider — Theme Management System * * This file provides a complete theme management system with: * - Light/dark/system theme support * - Theme persistence (localStorage) * - System preference detection * - Type-safe theme context * * Location: {project_path}/skills/frontend-design/examples/typescript/theme-provider.tsx * * Usage: * ```tsx * import { ThemeProvider, useTheme } from './theme-provider'; * * function App() { * return ( * * * * ); * } * * function ThemeToggle() { * const { theme, setTheme } = useTheme(); * return ; * } * ``` */ import React, { createContext, useContext, useEffect, useState, ReactNode } from 'react'; import { Theme } from './design-tokens'; // ============================================ // Types // ============================================ interface ThemeProviderProps { children: ReactNode; defaultTheme?: Theme; storageKey?: string; } interface ThemeContextType { theme: Theme; effectiveTheme: 'light' | 'dark'; // Resolved theme (system → light/dark) setTheme: (theme: Theme) => void; toggleTheme: () => void; } // ============================================ // Context // ============================================ const ThemeContext = createContext(undefined); // ============================================ // Provider Component // ============================================ export function ThemeProvider({ children, defaultTheme = 'system', storageKey = 'ui-theme', }: ThemeProviderProps) { const [theme, setThemeState] = useState(() => { // Load theme from localStorage if available if (typeof window !== 'undefined') { const stored = localStorage.getItem(storageKey); if (stored && ['light', 'dark', 'system'].includes(stored)) { return stored as Theme; } } return defaultTheme; }); const [effectiveTheme, setEffectiveTheme] = useState<'light' | 'dark'>(() => { if (theme === 'system') { return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; } return theme; }); // Update effective theme when theme or system preference changes useEffect(() => { const root = window.document.documentElement; // Remove previous theme classes root.classList.remove('light', 'dark'); if (theme === 'system') { const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; root.classList.add(systemTheme); root.setAttribute('data-theme', systemTheme); setEffectiveTheme(systemTheme); } else { root.classList.add(theme); root.setAttribute('data-theme', theme); setEffectiveTheme(theme); } }, [theme]); // Listen for system theme changes when in system mode useEffect(() => { if (theme !== 'system') return; const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); const handleChange = (e: MediaQueryListEvent) => { const systemTheme = e.matches ? 'dark' : 'light'; const root = window.document.documentElement; root.classList.remove('light', 'dark'); root.classList.add(systemTheme); root.setAttribute('data-theme', systemTheme); setEffectiveTheme(systemTheme); }; // Modern browsers if (mediaQuery.addEventListener) { mediaQuery.addEventListener('change', handleChange); return () => mediaQuery.removeEventListener('change', handleChange); } // Legacy browsers else if (mediaQuery.addListener) { mediaQuery.addListener(handleChange); return () => mediaQuery.removeListener(handleChange); } }, [theme]); const setTheme = (newTheme: Theme) => { localStorage.setItem(storageKey, newTheme); setThemeState(newTheme); }; const toggleTheme = () => { setTheme(effectiveTheme === 'light' ? 'dark' : 'light'); }; const value: ThemeContextType = { theme, effectiveTheme, setTheme, toggleTheme, }; return {children}; } // ============================================ // Hook // ============================================ export function useTheme(): ThemeContextType { const context = useContext(ThemeContext); if (context === undefined) { throw new Error('useTheme must be used within a ThemeProvider'); } return context; } // ============================================ // Theme Toggle Component // ============================================ interface ThemeToggleProps { className?: string; iconSize?: number; } export function ThemeToggle({ className = '', iconSize = 20 }: ThemeToggleProps) { const { theme, effectiveTheme, setTheme } = useTheme(); return ( ); } // ============================================ // Theme Selector Component (Dropdown) // ============================================ interface ThemeSelectorProps { className?: string; } export function ThemeSelector({ className = '' }: ThemeSelectorProps) { const { theme, setTheme } = useTheme(); const [isOpen, setIsOpen] = useState(false); const themes: { value: Theme; label: string; icon: JSX.Element }[] = [ { value: 'light', label: 'Light', icon: ( ), }, { value: 'dark', label: 'Dark', icon: ( ), }, { value: 'system', label: 'System', icon: ( ), }, ]; return (
{isOpen && ( <>
setIsOpen(false)} aria-hidden="true" />
{themes.map((t) => ( ))}
)}
); } // ============================================ // Utility: Apply theme class to body // ============================================ export function useThemeClass() { const { effectiveTheme } = useTheme(); useEffect(() => { const root = window.document.documentElement; root.classList.remove('light', 'dark'); root.classList.add(effectiveTheme); root.setAttribute('data-theme', effectiveTheme); }, [effectiveTheme]); } // ============================================ // Higher-Order Component for Theme // ============================================ export function withTheme

( Component: React.ComponentType

) { return function ThemedComponent(props: P) { const theme = useTheme(); return ; }; }