Skip to main content
Let’s build a Button component from scratch. You’ll learn the three core concepts:
  1. styled() - create components with variants
  2. createStyledContext() - share variants between components
  3. withSlots() - compose compound components

Step 1: A Simple Styled Component

Start with the basics. Create a button with size variants.
Use it:
That’s the foundation. base applies always, variants apply based on props.

Step 2: Add More Variants

Let’s add a variant prop for different styles.

Step 3: Compound Variants

Sometimes you need special styling when multiple variants combine. That’s what compoundVariants is for.
Now <Button variant="primary" size="lg"> gets an extra shadow that smaller or secondary buttons don’t.

Step 4: Context for Parent-Child Communication

Here’s where better-styled shines. Let’s add a label to our button that automatically matches the button’s size.
Usage:
Button.Label automatically receives size="lg" from the parent. No prop drilling.

What You’ve Learned

  1. styled(component, config) - Creates components with variant support
  2. createStyledContext(variants) - Defines which variants should be shared
  3. withSlots(component, slots) - Creates compound components with dot notation

Next Steps

Understanding styled()

Deep dive into the main API

Variant System

Learn about compound variants and defaults

Context Propagation

Master parent-child communication

Compound Components

Build complex component APIs