Skip to main content

Syntax

RecastStylesObject[]
required
Array of style objects created with recast.styles(). Must contain at least one style object.
Returns: RecastStylesObject - A new composed style object with merged styles from all input objects

How Composition Works

When composing style objects, Recast merges them using the following rules:

Merge Order

Later style objects override earlier ones for conflicting properties:

Property Merging

Base classes from all style objects are concatenated together:
Variant groups are merged, with later objects adding to or overriding earlier ones:
Modifiers from all style objects are combined:
Default values are combined, with later objects taking precedence:
Conditional rules from all style objects are combined in order:

Basic Example

Advanced Patterns

Layer-based Composition

Organize styles in logical layers from general to specific:

Feature-based Composition

Compose styles based on specific features or capabilities:

TypeScript Integration

Composed style objects maintain full TypeScript support:

Performance Considerations

Composition is optimized for performance:
  • Memoization: Style merging is cached to avoid recomputation
  • Single composition: Compose styles once, not on every render
  • Efficient merging: Deep merging uses optimized algorithms

Best Practices

Compose from most general to most specific to ensure proper overrides:
Each style object should have a single, clear responsibility:
Share common style modules across different components:

Error Handling

Empty array error: recast.compose([]) will throw an error. Always provide at least one style object.

Debugging Composition

Use the extract method to debug composed styles: