Using the sx Prop
The simplest way to create themed components is using thesx prop on standard HTML elements.
Basic Example
Accepting sx as a Prop
Allow consumers to customize your component:The
sx prop merges styles, with later styles overriding earlier ones.Using the Box Component
TheBox component is a versatile primitive for building themed components.
Box as a Building Block
Box Props
The Box component accepts several useful props:Available System Props
Box supports these shorthand props:Component Composition
Build complex components by composing simpler ones.Example: Feature Card
Polymorphic Components
Create components that can render as different elements:Using Variants
Create components that support theme variants.Component with Variants
Custom Variant Keys
Define your own variant location in the theme:Accessing the Theme
Use theuseThemeUI hook to access theme values in your components.
Basic Usage
Using Raw Colors
Color Mode
Access and control color mode:Forwarding Refs
Create components that support React refs:Using the css Helper
For more complex styling needs, use thecss helper directly:
Best Practices
Accept sx Prop
Always accept an
sx prop to allow consumers to customize your components.Use Box Component
Build on top of the Box component for consistent theming and system props.
Leverage Variants
Use theme variants for common style variations instead of multiple props.
TypeScript Types
Export proper TypeScript types for your components’ props.
