MCPcopy Create free account
hub / github.com/BingyanStudio/github-analyzer / ThemeProvider

Function ThemeProvider

web/src/components/theme-provider.tsx:23–64  ·  view source on GitHub ↗
({
  children,
  defaultTheme = "system",
  storageKey = "vite-ui-theme",
  ...props
}: ThemeProviderProps)

Source from the content-addressed store, hash-verified

21const ThemeProviderContext = createContext<ThemeProviderState>(initialState)
22
23export function ThemeProvider({
24 children,
25 defaultTheme = "system",
26 storageKey = "vite-ui-theme",
27 ...props
28}: ThemeProviderProps) {
29 const [theme, setTheme] = useState<Theme>(
30 () => (localStorage.getItem(storageKey) as Theme) || defaultTheme
31 )
32
33 useEffect(() => {
34 const root = window.document.documentElement
35
36 root.classList.remove("light", "dark")
37
38 if (theme === "system") {
39 const systemTheme = window.matchMedia("(prefers-color-scheme: dark)")
40 .matches
41 ? "dark"
42 : "light"
43
44 root.classList.add(systemTheme)
45 return
46 }
47
48 root.classList.add(theme)
49 }, [theme])
50
51 const value = {
52 theme,
53 setTheme: (theme: Theme) => {
54 localStorage.setItem(storageKey, theme)
55 setTheme(theme)
56 },
57 }
58
59 return (
60 <ThemeProviderContext.Provider {...props} value={value}>
61 {children}
62 </ThemeProviderContext.Provider>
63 )
64}
65
66export const useTheme = () => {
67 const context = useContext(ThemeProviderContext)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected