react-code-dataset / nivo /website /src /theming /SwitchableThemeProvider.tsx
Devendra174's picture
Upload folder using huggingface_hub
1e92f2d verified
raw
history blame
991 Bytes
import React, { createContext, ReactNode, useContext, useMemo, useState } from 'react'
import { ThemeProvider } from 'styled-components'
import themes from './theme'
type ThemeId = keyof typeof themes
interface ThemeIdContextValue {
themeId: ThemeId
setThemeId: (themeId: ThemeId) => void
}
const ThemeIdContext = createContext<ThemeIdContextValue>({} as any)
export const useSetTheme = () => {
const { setThemeId } = useContext(ThemeIdContext)
return setThemeId
}
export const SwitchableThemeProvider = ({ children }: { children: ReactNode }) => {
const [themeId, setThemeId] = useState<ThemeId>('light')
const contextValue = useMemo(
() => ({
themeId,
setThemeId,
}),
[themeId, setThemeId]
)
const theme = themes[themeId]
return (
<ThemeIdContext.Provider value={contextValue}>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
</ThemeIdContext.Provider>
)
}