Theming & Dark Mode
Provide a theme, write light/dark values with mode(), flip sections with inversed, follow the system color scheme, and theme plain styled components.
The UI system has two theme channels, and both are fed by one provider:
- The vitus-labs context (
contextfrom@vitus-labs/core) carriesthemeplus extra provider props. Rocketstyle readsthemeandmodefrom it. - The CSS engine's theme context (styler's
ThemeProvider) carriesthemeonly. Everystyledcomponent and everycssinterpolation receives it asprops.theme.
Core's Provider writes the theme into both. Rocketstyle's Provider adds mode on top. Unistyle's Provider adds the precomputed breakpoint helpers on top. This guide uses all three together.
1. Define the theme
A theme is a plain object. rootSize and breakpoints are used by unistyle. Everything else is yours:
export const theme = {
rootSize: 16,
breakpoints: { xs: 0, sm: 576, md: 768, lg: 992, xl: 1200 },
fontFamily: {
base: 'Inter, system-ui, sans-serif',
mono: 'JetBrains Mono, monospace',
},
palette: {
light: { bg: '#ffffff', fg: '#111827', muted: '#6b7280', primary: '#2563eb' },
dark: { bg: '#0b0f17', fg: '#f3f4f6', muted: '#9ca3af', primary: '#60a5fa' },
},
}2. Provide theme and mode
Use rocketstyle's Provider and pass unistyle's Provider as its provider. Unistyle's provider enables responsive values and hands the theme to core's provider (see Provider & Consumer):
'use client'
import './init'
import { Provider } from '@vitus-labs/rocketstyle'
import { Provider as UnistyleProvider } from '@vitus-labs/unistyle'
import type { ReactNode } from 'react'
import { theme } from './theme'
export function ThemeRoot({ children }: { children: ReactNode }) {
return (
<Provider provider={UnistyleProvider} theme={theme} mode="light">
{children}
</Provider>
)
}| Prop | Type | Default | Description |
|---|---|---|---|
theme | object | Parent's theme | Theme passed to rocketstyle callbacks and styled components |
mode | 'light' | 'dark' | 'light' | Active color mode |
inversed | boolean | false | Flip the mode inherited from the parent provider |
provider | ComponentType | core Provider | Provider that receives theme, mode, isDark, isLight |
Nested providers inherit theme and mode from the parent, so only the top-level provider needs theme and provider.
Always pass a theme at the top
Core's Provider renders its children directly, without a context, when theme is empty or missing. mode then never reaches rocketstyle. Give the top-level provider a non-empty theme object.
3. Write mode-aware components
.theme() and every dimension method receive (theme, mode, css). mode(lightValue, darkValue) stores both values. Rocketstyle resolves them for the active mode at render time:
'use client'
import './init'
import { Element } from '@vitus-labs/elements'
import { rocketstyle } from '@vitus-labs/rocketstyle'
export const Card = rocketstyle()({ name: 'Card', component: Element })
.attrs({ block: true })
.theme((t, mode) => ({
backgroundColor: mode(t.palette.light.bg, t.palette.dark.bg),
color: mode(t.palette.light.fg, t.palette.dark.fg),
borderColor: mode('#e5e7eb', '#1f2937'),
}))
.states((t, mode) => ({
highlighted: {
borderColor: mode(t.palette.light.primary, t.palette.dark.primary),
},
}))
.styles((css) => css`
padding: 24px;
border-radius: 12px;
border: 1px solid ${({ $rocketstyle }) => $rocketstyle.borderColor};
background-color: ${({ $rocketstyle }) => $rocketstyle.backgroundColor};
color: ${({ $rocketstyle }) => $rocketstyle.color};
`)mode is a function, not a string
Inside theme callbacks, mode is the selector (light, dark) => …. A check like mode === 'dark' is always false. Always call mode(light, dark).
Base and dimension themes are cached per theme object, and the mode-resolved copies are cached per mode. Switching the mode only recomputes the mode-specific caches. See Theme Caching.
4. Invert a section
Nested providers can flip the inherited mode. This is useful for a dark footer or hero inside a light page:
<ThemeRoot>
<Page />
<Provider inversed>
{/* dark when the page is light, and the other way round */}
<Footer />
</Provider>
</ThemeRoot>A single component can always render in the opposite mode with .config({ inversed: true }):
export const InversedCard = Card.config({ inversed: true })5. Toggle dark mode and follow the system
useColorScheme() returns 'light' | 'dark' from prefers-color-scheme and updates live. Combine it with useLocalStorage to remember a manual choice:
'use client'
import './init'
import { useColorScheme, useLocalStorage } from '@vitus-labs/hooks'
import { Provider } from '@vitus-labs/rocketstyle'
import { Provider as UnistyleProvider } from '@vitus-labs/unistyle'
import { createContext, type ReactNode, useContext, useMemo } from 'react'
import { theme } from './theme'
type Preference = 'system' | 'light' | 'dark'
const ColorModeContext = createContext<{
preference: Preference
setPreference: (value: Preference) => void
}>({ preference: 'system', setPreference: () => {} })
export const useColorMode = () => useContext(ColorModeContext)
export function ThemeRoot({ children }: { children: ReactNode }) {
const system = useColorScheme()
const [preference, setPreference] = useLocalStorage<Preference>(
'color-mode',
'system',
)
const mode = preference === 'system' ? system : preference
const value = useMemo(
() => ({ preference, setPreference }),
[preference, setPreference],
)
return (
<ColorModeContext value={value}>
<Provider provider={UnistyleProvider} theme={theme} mode={mode}>
{children}
</Provider>
</ColorModeContext>
)
}'use client'
import { useColorMode } from './ThemeRoot'
export function ModeSwitch() {
const { preference, setPreference } = useColorMode()
return (
<select
value={preference}
onChange={(e) => setPreference(e.target.value as typeof preference)}
>
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
)
}On React Native, useColorScheme() uses the Appearance API instead of a media query. useLocalStorage is web-only there, so persist the preference with your storage of choice.
Avoiding a flash on static pages
useColorScheme() and useLocalStorage() are hydration-safe. During the server render and hydration they return 'light' and the initial value, then update. With SSR or output: 'export', a visitor who prefers dark therefore sees the light theme for a moment before the switch.
If that matters, let CSS decide the colors instead of JavaScript:
- Keep every color in a CSS custom property (
--bg,--fg, …), with a dark set under a class such as.darkon<html>. - Set that class before first paint with a small blocking script, for example with
next-themes. - Use the variables directly in themes:
backgroundColor: 'var(--bg)'. The emitted CSS is then the same in both modes, so the static HTML is already correct.
This site works that way: its theme has no colors, and the .dark class is the only light/dark switch. Use mode() where the mode is known before the first paint (client-only apps, React Native), or where a short flash is acceptable.
6. Theme plain styled components
Components built with styled / css read props.theme from styler's theme context. They see theme, but not mode. Rocketstyle's provider forwards only theme to the CSS engine.
To give them mode-dependent values, add a nested styler ThemeProvider with a function theme. It receives the outer theme and returns the new one, so the breakpoint data added by unistyle is kept:
import { ThemeProvider } from '@vitus-labs/styler'
// defined at module level → stable identity, no re-render churn
const withLight = (outer) => ({ ...outer, colors: outer.palette.light })
const withDark = (outer) => ({ ...outer, colors: outer.palette.dark })
<Provider provider={UnistyleProvider} theme={theme} mode={mode}>
<ThemeProvider theme={mode === 'dark' ? withDark : withLight}>
{children}
</ThemeProvider>
</Provider>import { styled } from '@vitus-labs/styler'
const Muted = styled.p`
color: ${({ theme }) => theme.colors.muted};
`Without the function form, an object theme replaces the outer one completely. Rocketstyle callbacks are not affected by this nested provider: they read the theme from the vitus-labs context and use mode().
A nested rocketstyle Provider (for example an inversed section) passes the plain theme to styler again. Inside it, wrap the children in the ThemeProvider for the flipped mode as well.
Read the theme in components with styler's useTheme(). Global styles get it as well, and a dynamic createGlobalStyle swaps its rules when the theme changes:
import { createGlobalStyle } from '@vitus-labs/styler'
const GlobalStyles = createGlobalStyle`
body {
background: ${({ theme }) => theme.colors.bg};
color: ${({ theme }) => theme.colors.fg};
}
`For typed props.theme, augment styler's DefaultTheme. See Styler Theming: TypeScript.
Related
- Themes & Styles:
.theme(), dimensions,mode()internals - Provider & Consumer: provider props, nesting, context shape
- Styler Theming:
ThemeProvider, function themes,useTheme - Building a Design System
Last updated on
SSR & Static Export
Run the UI system under the Next.js App Router, server rendering and `output: 'export'`. Covers where init() goes, how styles reach the HTML, and how to stay hydration-safe.
Building a Design System
Step by step from design tokens to a Button and a Card built with rocketstyle, attrs and elements, with generated Storybook stories.