The useThemeContext
hook allows you to access or modify the theme and mode properties of the Cerberus Design System in multiple components.
To use the useThemeContext
hook, you need to wrap the component using the hook with the Cerberus ThemeProvider
component.
import { ThemeProvider , useThemeContext } from ' @cerberus/react '
const { theme , mode , udpateTheme , updateMode } = useThemeContext ()
function handleSetTheme ( theme : string ) {
updateTheme ( ' party-town ' )
function handleToggleMode () {
updateMode ( ( prev ) => (prev === ' light ' ? ' dark ' : ' light ' ))
< button onClick = { handleSetTheme } type = " button " >
update theme to party-town
< button onClick = { handleToggleMode } type = " button " >
import { ThemeProvider , useThemeContext } from ' @cerberus/react '
const { mode } = useThemeContext ()
{ mode === ' light ' ? ' Hey, friend! ' : ' I wear my sunglasses at night. ' }
import { ThemeProvider } from ' @cerberus/react '
import Header from ' ./header '
If you are using NextJS, you can utilize cookies to store the theme and mode properties. This way, the user's theme and mode preferences persist between page loads.
import { type PropsWithChildren } from ' react '
} from ' @cerberus-design/react '
import { getTheme , injectTheme , type ThemeName } from ' @/styled-system/themes '
import { Nav } from ' ./components/Nav '
import { getCookie , setCookie } from ' ./actions/cookies '
export default async function RootLayout ( props : PropsWithChildren < RootProps > ) {
const themeName = ( await getCookie ( ' theme ' )) as ThemeName
const theme = themeName && ( await getTheme ( themeName ))
const colorModeName = ( await getCookie ( ' colorMode ' )) as ColorModes | undefined
const handleUpdateTheme = async ( theme : DefaultThemes ) => {
const newTheme = await getTheme ( theme as ThemeName )
setCookie ( ' theme ' , newTheme )
injectTheme ( document . documentElement , newTheme )
const handleUpdateMode = async ( mode : ColorModes ) => {
setCookie ( ' colorMode ' , mode )
data-panda-theme = { themeName || ' cerberus ' }
data-color-mode = { colorModeName || ' system ' }
dangerouslySetInnerHTML = { { __html : theme . css } }
defaultTheme = { themeName || ' cerberus ' }
defaultColorMode = { colorModeName || ' system ' }
updateTheme = { handleUpdateTheme }
updateMode = { handleUpdateMode }
const pathname = usePathname ()
const { mode , updateMode } = useThemeContext ()
const ariaLabel = useMemo ( () => {
return mode === ' light ' ? ' Switch to dark mode ' : ' Switch to light mode '
const handleUpdateMode = useCallback ( ( e : MouseEvent < HTMLButtonElement > ) => {
const currentMode = e . currentTarget . value as ColorModes
const newMode = getColorMode ( currentMode )
onClick = { handleUpdateMode }
The useThemeContext
does not accept any arguments. Instead, everything is passed as props to the ThemeProvider
component.
export type DefaultThemes = ' cerberus ' | ' acheron '
export type CustomThemes < K extends string = DefaultThemes > = ' cerberus ' | K
export type ColorModes = ' light ' | ' dark ' | ' system '
export interface ThemeContextValue < T extends string = DefaultThemes > {
updateTheme : ( theme : T ) => void
updateMode : ( mode : ColorModes ) => void
export interface ThemeProviderProps extends UseThemeOptions {
defaultTheme ? : DefaultThemes
defaultColorMode ? : ColorModes
define function useThemeContext (
defaultTheme : DefaultThemes = ' cerberus ' ,
defaultColorMode : ColorModes = ' light ' ,
updateTheme : ( theme : DefaultThemes ) => void
updateMode : ( mode : ColorModes ) => void