useTheme
The useTheme
hook allows you to access or modify the theme and mode properties of the Cerberus Design System.
Usage
import { useTheme } from '@cerberus/react'
type CustomizedThemes = 'party-town'
function Nav() { const { theme, mode, updateTheme, updateMode } = useTheme<CustomizedThemes>()
function handleSetTheme(theme: string) { updateTheme('party-town') }
function handleToggleMode() { updateMode((prev) => (prev === 'light' ? 'dark' : 'light')) }
return ( <nav> <button onClick={handleSetTheme} type="button"> update theme to party-town </button> <button onClick={handleToggleMode} type="button"> toggle mode </button> </nav> )}
API
export type DefaultThemes = 'cerberus' | 'acheron'export type CustomThemes<K extends string = DefaultThemes> = 'cerberus' | Kexport type ColorModes = 'light' | 'dark' | 'system'
export interface UseThemOptions { defaultTheme?: CustomThemes defaultMode?: ColorModes options?: { cache?: boolean updateMode?: (mode: ColorModes) => void updateTheme?: (theme: CustomThemes) => void }}
define function useTheme<C extends string = DefaultThemes>( defaultTheme: CustomThemes<C> = 'cerberus', defaultColorMode: ColorModes = 'light', options?: UseThemOptions = {}): { theme: CustomThemes<C> mode: ColorModes updateTheme: (theme: CustomThemes<C>) => void updateMode: (mode: ColorModes) => void}
Arguments
The useTheme
accepts the following optional arguments:
Name | Default | Description |
---|---|---|
defaultTheme | 'cerberus' | The default theme of the Cerberus Design System. |
defaultMode | 'light' | The default mode of the Cerberus Design System. |
Options
The useTheme
hook accepts an optional options
object with the following properties:
Name | Default | Description |
---|---|---|
cache | true | Whether to cache the theme and mode in the local storage. |
updateTheme | A custom function to call when the theme is updated. | |
updateMode | A custom function to call when the mode is updated. |
Return
The useTheme
hook returns an object with the following properties:
Name | Description |
---|---|
theme | The current theme of the Cerberus Design System. |
mode | The current mode of the Cerberus Design System. |
updateTheme | A function that allows you to update the theme of the Cerberus Design System. |
updateMode | A function that allows you to update the mode of the Cerberus Design System. |