suiss/ui · Component
Direction
A provider that sets the reading direction, left-to-right or right-to-left, for the components inside it.
Direction is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
The direction module provides DirectionProvider and useDirection from the chosen base. DirectionProvider takes a direction of ltr or rtl and makes it available to the primitives below it, so behaviors that depend on direction, such as arrow-key navigation and popup placement, follow the reading direction. useDirection returns the current direction for your own components.
The provider does not set the dir attribute on the page. Set dir on the html element or on a container yourself so that the browser and logical CSS properties flip the layout.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Right to left
Set direction to rtl on the provider. The card reads it with useDirection and passes it to its dir attribute, so its footer buttons appear in reversed order.
API Reference
DirectionProvider
Makes the direction available to the components below it.
| Prop | Type | Default |
|---|---|---|
direction | "ltr" | "rtl" | "ltr" |
| Prop | Type | Default |
|---|---|---|
direction | "ltr" | "rtl" | "ltr" |
dir | "ltr" | "rtl" | - |
dir is the Radix name for the same prop. direction wins when both are set.
| Prop | Type | Default |
|---|---|---|
direction | "ltr" | "rtl" | "ltr" |
locale | string | - |
locale is passed to the I18nProvider of React Aria. When it is unset, the current locale is used with an Arabic script for rtl and a Latin script for ltr, so React Aria components follow direction.
Wrap your app, or a subtree, in DirectionProvider with the same direction you set in the dir attribute. Many components in the library use logical properties such as ps-, pe-, ms- and me-, which flip automatically under dir="rtl".
useDirection
Returns the current direction, "ltr" or "rtl", from the nearest DirectionProvider.
Read the value with useDirection when a component needs to adapt, for example to pass it to a dir attribute or to rotate a directional icon. For icons you can also use the rtl: Tailwind variant, for example rtl:rotate-180, so arrows point the right way.