Spaces:
Running
Running
Download src/lib/theme.ts from evalstate/mcp-webcam: direct link, hf CLI and curl.
- Browser
- Download file 927 Bytes
-
https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/theme.ts
- Command line
-
hf download hf://spaces/evalstate/mcp-webcam/src/lib/theme.ts
-
curl -L -o theme.ts https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/src/lib/theme.ts
927 Bytes
| import { useCallback, useEffect, useState } from 'react'; | |
| export type Theme = 'dark' | 'light'; | |
| const THEME_KEY = 'mcp-webcam-theme'; | |
| function readInitialTheme(): Theme { | |
| if (typeof window === 'undefined') return 'dark'; | |
| try { | |
| const saved = window.localStorage.getItem(THEME_KEY); | |
| if (saved === 'dark' || saved === 'light') return saved; | |
| } catch { | |
| // ignore | |
| } | |
| return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | |
| } | |
| export function useTheme() { | |
| const [theme, setTheme] = useState<Theme>(readInitialTheme); | |
| useEffect(() => { | |
| document.documentElement.classList.toggle('dark', theme === 'dark'); | |
| try { | |
| window.localStorage.setItem(THEME_KEY, theme); | |
| } catch { | |
| // ignore | |
| } | |
| }, [theme]); | |
| const toggleTheme = useCallback(() => { | |
| setTheme((t) => (t === 'dark' ? 'light' : 'dark')); | |
| }, []); | |
| return { theme, setTheme, toggleTheme }; | |
| } | |