Theming
Theme support with ThemeProvider and useTheme in @vitus-labs/styler.
@vitus-labs/styler provides a theming system via React Context. The ThemeProvider makes a theme object available to all styled components, and useTheme() provides direct access in hooks.
ThemeProvider
Wraps your application and provides a theme object to all descendant styled components.
import { ThemeProvider } from '@vitus-labs/styler'
const theme = {
colors: {
primary: '#0d6efd',
secondary: '#6c757d',
background: '#ffffff',
text: '#212529',
},
spacing: {
sm: 8,
md: 16,
lg: 24,
},
borderRadius: 8,
}
function App() {
return (
<ThemeProvider theme={theme}>
<Main />
</ThemeProvider>
)
}Props
| Prop | Type | Description |
|---|---|---|
theme | Theme | ((outer: Theme) => Theme) | Theme object available to all descendant styled components, or a function that derives it from the outer theme (see Function Themes). Theme = DefaultTheme & Record<string, unknown> |
children | ReactNode | Child elements |
How Theme Reaches Styled Components
When a styled component has dynamic interpolations (function values), the component:
- Calls
useTheme()to read the current theme from context - Merges theme into the props object:
{ ...componentProps, theme } - Passes the merged object to each function interpolation
Without a ThemeProvider, interpolations receive an empty theme object ({}), never undefined.
The injected context theme is used only for resolving styles — it is not forwarded to the rendered DOM element, nor (since 2.9) to a wrapped custom component. An explicitly passed theme prop is forwarded as-is. If a component wrapped with styled(MyComponent) needs the theme, read it with useTheme():
function MyCard({ className, children }) {
const theme = useTheme() // not props.theme
return <div className={className} data-mode={theme.mode}>{children}</div>
}
const StyledCard = styled(MyCard)`
padding: ${(p) => p.theme.spacing.md}px;
`const Card = styled.div`
background: ${(props) => props.theme.colors.background};
padding: ${(props) => props.theme.spacing.md}px;
border-radius: ${(props) => props.theme.borderRadius}px;
`Static styled components (no function interpolations) do not access theme context — they have zero per-render overhead.
useTheme Hook
Access the theme object directly in any component:
import { useTheme } from '@vitus-labs/styler'
function StatusBadge({ status }) {
const theme = useTheme()
const color = status === 'active'
? theme.colors.primary
: theme.colors.secondary
return (
<span style={{ color, fontWeight: 'bold' }}>
{status}
</span>
)
}Signature
useTheme<T extends Theme = Theme>(): TThe generic type parameter allows custom typing:
interface MyTheme {
colors: { primary: string }
spacing: (n: number) => string
}
const theme = useTheme<MyTheme>()
// theme.colors.primary is typed as string
// theme.spacing is typed as (n: number) => stringDefault Value
If useTheme() is called outside any ThemeProvider, it returns an empty object {} (the context's default value). It does not throw.
Nested Themes
ThemeProvider instances can be nested. Child providers completely override the parent theme for their subtree:
const lightTheme = { colors: { background: '#fff', text: '#000' } }
const darkTheme = { colors: { background: '#1a1a1a', text: '#fff' } }
function App() {
return (
<ThemeProvider theme={lightTheme}>
<Header /> {/* Uses lightTheme */}
<ThemeProvider theme={darkTheme}>
<Sidebar /> {/* Uses darkTheme */}
</ThemeProvider>
<Main /> {/* Uses lightTheme */}
</ThemeProvider>
)
}An object theme does not merge — the inner theme completely replaces the outer one. To extend the outer theme, pass a function.
Function Themes
Since 2.8, theme can be a function that receives the outer theme and returns the new one — the same pattern styled-components supports:
const withAccent = (outer) => ({
...outer,
colors: { ...outer.colors, primary: '#d63384' },
})
<ThemeProvider theme={lightTheme}>
<Header /> {/* lightTheme */}
<ThemeProvider theme={withAccent}>
<PromoBanner /> {/* lightTheme with a pink primary */}
</ThemeProvider>
</ThemeProvider>The result is memoized on [theme, outer], so define the function outside render (or wrap it in useCallback) to keep descendants from re-rendering. Without an outer provider, outer is {}.
TypeScript Support
Module Augmentation
Define a DefaultTheme interface for type-safe theme access across all styled components:
declare module '@vitus-labs/styler' {
export interface DefaultTheme {
colors: {
primary: string
secondary: string
background: string
text: string
}
spacing: {
sm: number
md: number
lg: number
}
borderRadius: number
}
}Once augmented, all interpolation functions receive typed props.theme:
const Box = styled.div`
padding: ${(props) => props.theme.spacing.md}px;
// TypeScript knows spacing.md is a number ✓
// TypeScript errors on props.theme.nonexistent ✓
`DefaultTheme
The base interface is intentionally empty to allow augmentation:
export interface DefaultTheme {}This is the recommended pattern — augment once in a declaration file, and all styled components, useTheme() calls, and createGlobalStyle interpolations get full type safety.
Theme in Global Styles
createGlobalStyle also receives theme. A dynamic global like this replaces its rules when the theme changes (e.g. a light/dark toggle) instead of accumulating them, and removes them when the last instance unmounts:
const GlobalStyles = createGlobalStyle`
body {
background: ${(props) => props.theme.colors.background};
color: ${(props) => props.theme.colors.text};
font-family: system-ui, sans-serif;
}
`
<ThemeProvider theme={theme}>
<GlobalStyles />
<App />
</ThemeProvider>Theme in useCSS
The useCSS hook automatically accesses theme from context:
const card = css`
background: ${(props) => props.theme.colors.background};
border-radius: ${(props) => props.theme.borderRadius}px;
`
function Card({ children }) {
const className = useCSS(card)
// theme automatically merged from context
return <div className={className}>{children}</div>
}API Reference
DefaultTheme
Prop
Type
Last updated on