styled() API
Thestyled() utility lets you create custom styled components with access to the theme.
Basic Usage
API Signature
Parameters
- tag - The component or HTML element to style
- options - Optional configuration object
name- Component name for theme overridesslot- Slot name for component partsskipVariantsResolver- Skip theme variants (default: false)skipSx- Skip sx prop processing (default: false)shouldForwardProp- Determine which props to forward to DOMoverridesResolver- Resolve theme style overrides
Source Implementation
FromcreateStyled.js:132-307:
Using with Theme
Access theme values in your styles:Dynamic Styles with Props
Use props to create dynamic styles:shouldForwardProp
Control which props are forwarded to the underlying element:Default shouldForwardProp
FromcreateStyled.js:19-21:
ownerState, theme, sx, as.
Component Composition
Style existing styled components:Variants
Define style variants in your styled component:The sx Prop
By default, styled components support thesx prop:
sx prop:
Theme Style Overrides
When you provide aname option, the component can be styled via theme:
Tagged Template Syntax
You can also use tagged template literals:TypeScript
Full TypeScript support with prop types:Best Practices
- Use theme values - Always reference theme for consistency
- Avoid inline styles - Use styled components instead
- Compose components - Build complex components from simpler ones
- Type your props - Use TypeScript for better DX
- Use shouldForwardProp - Prevent unwanted props in DOM
Related APIs
- Box Component - Pre-built styled component
- sx Prop - Inline styling with theme access
- createStyled - Factory for creating custom styled utilities