Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

theme.tsx

39 lines965 Bytes

1import {
2 createContext,
3 createEffect,
4 createSignal,
5 useContext as getContext,
6} from 'solid-js'
7import type { Accessor, JSX } from 'solid-js'
8​
9export type TanStackDevtoolsTheme = 'light' | 'dark'
10​
11type ThemeContextValue = {
12 theme: Accessor<TanStackDevtoolsTheme>
13 setTheme: (theme: TanStackDevtoolsTheme) => void
15const ThemeContext = createContext<ThemeContextValue | undefined>(undefined)
16​
17export const ThemeContextProvider = (props: {
18 children: JSX.Element
19 theme: TanStackDevtoolsTheme
20}) => {
21 const [theme, setTheme] = createSignal<TanStackDevtoolsTheme>(props.theme)
22 createEffect(() => {
23 setTheme(props.theme)
24 })
25 return (
26 <ThemeContext.Provider value={{ theme, setTheme }}>
27 {props.children}
28 </ThemeContext.Provider>
29 )
31​
32export function createTheme() {
33 const context = getContext(ThemeContext)
34 if (!context) {
35 throw new Error('createTheme must be used within a ThemeContextProvider')
36 }
37​
38 return context