better-themes

API Reference

Complete API documentation for better-themes

ThemeProvider

The main provider component that wraps your application.

Props

Prop

Type

Default Values

  • themes: ["light", "dark"]
  • defaultTheme: "system" (if enableSystem is true), otherwise "light"
  • storage: "localStorage"
  • storageKey: "theme"
  • enableSystem: true
  • enableColorScheme: true
  • attribute: "class"
  • disableTransitionOnChange: false

Examples

Basic usage:

layout.tsx
<ThemeProvider>{children}</ThemeProvider>

With custom themes:

layout.tsx
<ThemeProvider themes={["light", "dark", "purple", "blue"]}>
    {children}
</ThemeProvider>

With data attribute:

layout.tsx
<ThemeProvider attribute="data-theme">{children}</ThemeProvider>

Disable transitions on change:

layout.tsx
<ThemeProvider disableTransitionOnChange>{children}</ThemeProvider>

Force a specific theme:

layout.tsx
<ThemeProvider forcedTheme="dark">{children}</ThemeProvider>

Use sessionStorage for per-tab themes:

layout.tsx
<ThemeProvider storage="sessionStorage">{children}</ThemeProvider>

useTheme Hook

Returns theme state and controls.

Return Value

interface UseThemeProps {
    /** List of all available theme names */
    themes: string[];
    /** Forced theme name for the current page */
    forcedTheme?: string;
    /** Update the theme */
    setTheme: Dispatch<SetStateAction<string>>;
    /** Active theme name */
    theme?: string;
    /** System theme preference ("dark" or "light") */
    systemTheme?: "dark" | "light";
}

Properties

  • theme - Current active theme name (undefined on server)
  • setTheme - Function to update theme (accepts string or callback)
  • forcedTheme - Forced theme if set, otherwise undefined
  • themes - Array of all available themes (includes "system" if enabled)
  • systemTheme - System preference ("dark" or "light", only if enableSystem is true)

Examples

Basic usage:

ThemeToggle.tsx
const { ,  } = ();

("dark");

With callback:

ThemeToggle.tsx
const { ,  } = ();

(() => ( === "dark" ? "light" : "dark"));

Access system theme:

ThemeToggle.tsx
const {  } = ();

.(); // "dark" or "light"

Types

Attribute

type Attribute = `data-${string}` | "class";

The HTML attribute type that can be modified. Can be "class" or any data-* attribute.

ValueObject

interface ValueObject {
    [themeName: string]: string;
}

Mapping of theme names to HTML attribute values. Useful when you want different attribute values than theme names.

Example

layout.tsx
<ThemeProvider
    themes={["light", "dark", "purple"]}
    value={{
        light: "light-mode",
        dark: "dark-mode",
        purple: "purple-mode",
    }}
>
    {children}
</ThemeProvider>

RSC Export

For Next.js App Router and other React Server Components frameworks, use the /rsc export:

layout.tsx
import { ThemeProvider, useTheme } from "better-themes/rsc";

This ensures proper server component compatibility. The API is identical to the main export.

On this page