import { createContext, useContext } from 'react' export type ColorScheme = 'light' | 'dark' export const themeStorageKey = 'react-spectrum-color-scheme' export const ThemeContext = createContext<{ colorScheme: ColorScheme setColorScheme: (colorScheme: ColorScheme) => void } | null>(null) export function getInitialColorScheme(): ColorScheme { try { const saved = localStorage.getItem(themeStorageKey) if (saved === 'light' || saved === 'dark') return saved } catch { // Storage may be unavailable; theme switching still works for this session. } return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' } export function useTheme() { const theme = useContext(ThemeContext) if (!theme) throw new Error('useTheme must be used within ThemeContext') return theme }