Skip to main content
Learn how to build custom components that integrate seamlessly with Theme UI’s theming system.

Using the sx Prop

The simplest way to create themed components is using the sx 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

The Box 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 the useThemeUI hook to access theme values in your components.

Basic Usage

Theme color values are CSS custom properties. Use theme.rawColors if you need raw color values (e.g., for Canvas or WebGL).

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 the css 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.

Complete Example

Here’s a complete example combining all concepts: