Use themes
Define a contract
Section titled “Define a contract”Use createThemeContract to define stable token names:
const vars = createThemeContract({ color: { text: null, },})Create a theme
Section titled “Create a theme”Use createTheme to provide concrete values:
const darkTheme = createTheme(vars, { color: { text: "#f5f5f5", },})Provide the active theme
Section titled “Provide the active theme”Wrap the relevant subtree with ThemeProvider:
<ThemeProvider theme={darkTheme}>{children}</ThemeProvider>Resolve themed styles
Section titled “Resolve themed styles”Use useThemedStyles, useThemedStyle, resolveThemeStyles, or resolveThemeTokens before render. Raw themed exports intentionally contain token references until runtime resolution.
For a focused read, use useThemeSelector and return a primitive or a
memoized value:
const foreground = useThemeSelector((theme) => theme.color.text)Selectors compare their result by reference, so avoid allocating a new object for an unchanged theme snapshot. The themed style hooks already retain stable resolved references for unchanged style and theme inputs.
Common mistakes
Section titled “Common mistakes”- using browser CSS variables like
var(--brand)instead of a theme contract - reading themed style exports directly without resolving them
- mixing “contract shape” and “concrete values” in the same module without clear naming