Skip to main content

Theme

Utility provider that gives everything below it a light, dark, or system color mode.

import { Theme } from "@/fragments/ui";Source

Preview

<ThemeProvider defaultMode="system">
  <div
    style={{
      padding: '16px',
      borderRadius: '8px',
      background: 'var(--fui-bg-elevated)',

Installation

npx @usefragments/cli add theme

Writes src/fragments/ui/components/Theme plus what it imports, the globals stylesheet, and .fragments/registry-lock.json, then prints the packages to add. No account, no key.

Your own registry: publish from Fragments Cloud and add --registry org/name. Connect a workspace.

Anatomy

Theme is a compound: import the root and reach its parts through dot notation.

  • Theme.Provider
  • Theme.Toggle
  • Theme.Button
  • Theme.useTheme

Examples

With Toggle

The toggle cycles light, dark, then back to system.

<ThemeProvider defaultMode="light">
  <div style={{ display: 'flex', alignItems: 'center', gap: '16px' }}>
    <ThemeToggle />
    <span>Click to cycle themes</span>
  </div>
</ThemeProvider>

Toggle Sizes

Three sizes so the control fits whatever bar holds it.

<ThemeProvider defaultMode="light">
  <div style={{ display: 'flex', alignItems: 'center', gap: '16px' }}>
    <ThemeToggle size="sm" />
    <ThemeToggle size="md" />
    <ThemeToggle size="lg" />
  </div>

Theme Button

A single icon button for toolbars with no room to spare.

<ThemeProvider defaultMode="system">
  <ThemeButton aria-label="Switch color mode" />
</ThemeProvider>

API

Component props
Prop
Type
Default
Description
childrenRequirednodeNot setApplication content
defaultModeenumlightdarksystemsystemDefault theme mode for uncontrolled usage
defaultThemeenumlightdarksystemNot set
modeenumlightdarksystemNot setControlled theme mode
onModeChangefunctionNot setCallback when mode changes
storageKeystringfui-themelocalStorage key for persistence
attributeenumdata-themeclassdata-themeHow to apply theme to DOM
componentDefaultsobjectNot setDefault primitive component behavior for the subtree.

Accessibility

  • The toggle's label names the mode it is in
  • System mode follows prefers-color-scheme

Guidance

Use when

  • An app needs light and dark modes
  • The user's choice should survive a reload
  • You want to honour the OS setting by default

Avoid when

  • Recoloring one component (use its props)
  • A single one-off color swap (use a CSS variable)
  • AppShellsiblingThemeProvider typically wraps AppShell

Next steps