CoastalCommitsPastes/client/app/components/theme/ThemeServerContextProvider.tsx

23 lines
785 B
TypeScript
Raw Normal View History

2022-11-18 01:36:53 -05:00
import type { FunctionComponent, PropsWithChildren } from "react"
// @ts-ignore -- createServerContext is not in @types/react atm
2022-11-18 01:36:53 -05:00
import { useContext, createServerContext } from "react"
import { cookies } from "next/headers"
import { Theme, THEME_COOKIE_NAME } from "./theme"
import { DEFAULT_THEME } from "./theme"
2022-11-18 01:36:53 -05:00
const ThemeContext = createServerContext<Theme | null>(null)
export function useServerTheme(): Theme {
2022-11-18 01:36:53 -05:00
return useContext(ThemeContext)
}
const ThemeServerContextProvider: FunctionComponent<PropsWithChildren<{}>> = ({
2022-11-18 01:36:53 -05:00
children
}) => {
2022-11-18 01:36:53 -05:00
const cookiesList = cookies()
const theme = cookiesList.get(THEME_COOKIE_NAME)?.value ?? DEFAULT_THEME
return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>
}
2022-11-18 01:36:53 -05:00
export default ThemeServerContextProvider