Skip to main content

Basic Composition

Use recast.compose() to combine multiple style objects:

Why Use Composition?

Reusability

Share style modules across different components for consistency

Maintainability

Update one style module and all composed components inherit the changes

Separation of Concerns

Keep different styling concerns (colors, sizes, states) in separate modules

Testing

Test individual style modules in isolation before composing them

How Composition Works

Later style objects override earlier ones for conflicting properties:
Base classes from all style objects are combined:
Variant groups are merged, with later objects adding to or overriding:
Modifiers from all style objects are combined:

Composition Patterns

Layer-Based Composition

Organize styles in logical layers from general to specific:
Layer Pattern

Feature-Based Composition

Build components by composing specific features:

Shared Style Libraries

Create reusable style modules that work across components:
Shared Modules

TypeScript Support

Composed style objects maintain full TypeScript support:

Performance Tips

Create composed styles outside components, not on every render:
Limit composition to 3-5 style objects for optimal performance:

Best Practices

Order by Specificity

Compose from most general to most specific to ensure proper overrides

Single Responsibility

Each style module should have one clear purpose (colors, layout, states, etc.)

Consistent Naming

Use consistent variant and modifier names across all style modules

Test Modules

Test individual style modules before composing them together