ThemeProvider
stablefunction ThemeProvider({ themes, active, children }: ThemeProviderProps): JSX.ElementAll themes that might be active at this scope. Their CSS-variable definitions are emitted once via a <style> element so theme switches become attribute swaps rather than React re-renders. Include any pre-generated combination themes (e.g. dark_red) used by nested <Theme> boundaries.
Name of the active theme. Must match the name of one of the entries in themes; otherwise CSS variables fall back to browser defaults.
Subtree that consumes the active theme.
Description
Top-level theme provider. Renders a <style> element containing every theme's tokens, scoped to
[data-theme="<name>"], and wraps its children in a <div data-theme={active}>.
A theme switch is a single attribute write — set active to a different name and the cascade
flips. No React re-renders, no unmount/remount.
Theme boundary
Theme
stablefunction Theme({ name, children }: ThemeProps): JSX.ElementTheme name to apply to this subtree. Combined with the parent chain, motif looks up a registered combination theme (e.g. dark_red), falling back to the inner name (red) or the parent active name when no combination is registered.
Subtree that switches to the new active theme.
Sub-tree theme boundary. Switches the active theme for descendants by setting data-theme on a
wrapping element. The CSS-variable cascade handles the visual change; React state is not involved.
When nested inside a parent <Theme> (or <ThemeProvider>), motif composes the names with _
and looks for a registered combination theme. Pre-generate the combinations you care about and
pass them all to <ThemeProvider themes={...}> — they activate automatically when the chain
matches.
Example
Related
- Theming — the theming model.
createTheme— build a theme to pass tothemes.useTheme— read the active theme object from a component.useThemeName— read just the active theme name.useThemeChain— read the full chain of nested theme names.useThemeSetting— auto dark/light hook for web.