@angular/cdk/bidi package provides utilities for handling bidirectional text and layouts, supporting both LTR (left-to-right) and RTL (right-to-left) orientations.
Installation
Basic Usage
Directionality Service
TheDirectionality service provides the current text direction.
Dir Directive
Use the[dir] directive to set direction for a subtree.
Dynamic Direction
Detecting Direction
Using with Signals
TheDirectionality service provides a signal for reactive direction tracking.
RTL Styling Patterns
Logical Properties (Recommended)
Use CSS logical properties for automatic RTL support:RTL-Specific Styles
API Reference
Directionality Service
Injectable:{providedIn: 'root'}
| Property | Type | Description |
|---|---|---|
value | Direction | Current direction (‘ltr’ or ‘rtl’) |
valueSignal | Signal<Direction> | Direction as a signal |
change | EventEmitter<Direction> | Emits when direction changes |
Dir Directive
Selector:[dir]
| Input | Type | Description |
|---|---|---|
dir | 'ltr' | 'rtl' | 'auto' | The text direction |
| Event | Type | Description |
|---|---|---|
dirChange | Direction | Emits when direction changes |
Type: Direction
Auto-Detection
When usingdir="auto", the direction is detected from the browser’s locale:
Global Configuration
Set direction globally on the document:Testing
Best Practices
- Use logical properties - Prefer
margin-inline-startovermargin-left - Test both directions - Always test your app in both LTR and RTL
- Avoid hardcoded directions - Use the Directionality service instead
- Icon mirroring - Some icons (arrows) should flip in RTL
- Number formatting - Numbers may need different formatting in RTL locales