Skip to main content

Basic Syntax

Style Configuration

The style configuration object supports five main properties:

Base - Foundation styles

Classes applied to all instances of the component. Each key accepts a string of classnames or an array of classname strings for better readability:
Or as an array for better readability with long classname lists:
Can also be an object for complex components:

Variants - Mutually exclusive options

Different visual styles (only one can be active):

Modifiers - Boolean flags

Styles that can be combined:

Defaults - Reduce repetition

Set default values for variants:

Conditionals - Advanced logic

Apply styles only when specific conditions are met:

What You Get Back

The recast.styles() function returns a style object with two key capabilities:

1. Component Application

Apply styles to any React component:

2. Style Extraction

Extract computed class names for advanced use cases:
Use extraction for: Server-side rendering, integration with animation libraries, or when you need raw class names.

Simple vs Complex Components

Simple Components (String Base)

For single-element components, use a string base:

Complex Components (Object Base)

For multi-element components, use an object base:

Performance

Recast automatically optimizes style computation:
  • Memoization: Results are cached to avoid recomputation
  • LRU Cache: Prevents memory leaks in long-running applications
  • Fast Extraction: Style extraction is optimized for performance
Performance Example
Performance Tip: Create style objects outside of components, not inside render functions. This ensures styles are created once and reused.

Best Practices

Create Once

Define style objects outside components to avoid recreation on every render

Use Defaults

Set sensible defaults to reduce prop repetition in your components

Semantic Names

Use meaningful variant names like variant and size.

Composition Ready

Design styles to work well with recast.compose() for maximum reusability