4IT580: Docs
4IT580 WebGitLab

Compound Components

What this pattern is

Compound components are components that work together as a group, usually like:

You’ve probably already worked with them in component libraries like Chakra UI.

The idea:

Why it’s powerful

  1. Friendly API

    • You write UI the way it looks in HTML.
    • Less prop noise.
  2. No prop drilling

    • Root provides shared state via context.
    • Children consume it directly.
  3. Modular by default

    • Want no close button? Don’t render it.
    • No style props.
  4. Scales better than “god props”

    • Alternative is a single component with 20 props and flags.
    • That becomes a massive component quickly.
  5. Encourages consistent behavior

    • All items share the same rules from Root.
    • Easier to keep UX aligned.

Mental model

Think of compound components like a mini-app / subsystem:

The user (caller) defines layout, but your component defines rules.

When to use it

Use compound components when:

Don’t use it if:

Accordion example

vs

Resources