Skip to main content
196

Search Lumen

Find components, APIs, guides, and recipes.

GitHub
Web docs
Selection and preferences

useThemeToggle

Synchronizes a light or dark theme with the document, local storage, and the shared theme-change event, with an optional pointer-origin transition that respects reduced motion.

Live component preview · Astro

Explore ThemeToggle

Try the component this hook connects to. The preview uses the Astro adapter; the React example below uses useThemeToggle.

Change the site theme

React usage

Import from @santi020k/lumen-react and spread the controller props onto the matching components.

tsxuseThemeToggle example
import { ThemeToggle, useThemeToggle } from '@santi020k/lumen-react'

export function ThemeControl() {
  const { theme, toggleTheme } = useThemeToggle()

  return (
    <ThemeToggle
      aria-label={`Switch to ${theme === 'dark' ? 'light' : 'dark'} theme`}
      onClick={toggleTheme}
    >
      {theme === 'dark' ? 'Light' : 'Dark'}
    </ThemeToggle>
  )
}
import { ThemeToggle, useThemeToggle } from '@santi020k/lumen-react'

export function ThemeControl() {
  const { theme, toggleTheme } = useThemeToggle()

  return (
    <ThemeToggle
      aria-label={`Switch to ${theme === 'dark' ? 'light' : 'dark'} theme`}
      onClick={toggleTheme}
    >
      {theme === 'dark' ? 'Light' : 'Dark'}
    </ThemeToggle>
  )
}

Load @santi020k/lumen-react/styles.css once in your application. With React Server Components, put 'use client' at the top of the module that calls the hook. Follow the React setup guide for the framework boundary.

Options

PropertyTypeDefaultDescription
defaultThemestring"light"Theme used when browser state is unavailable or has no saved value.

Controller return values

PropertyTypeDescription
themestringCurrent theme value.
setThemeDispatch<SetStateAction<string>>Sets the current theme directly.
toggleTheme(event?: MouseEvent<HTMLButtonElement>) => voidSwitches between light and dark and optionally starts the transition from the invoking button.
Web docsFull catalog