Direction
A provider that tells components whether users read from left to right or from right to left.
A direction provider tells the components inside it whether users read from left to right or from right to left. In a right-to-left interface, a slider counts its value from the right edge, and the left and right arrow keys swap their roles. The provider renders no element and sets no dir attribute, so the element around it sets dir to the same direction, which mirrors the layout.
Usage
Examples
With useDirection
Shared components that render an element of their own cannot count on the page around them to set the right dir attribute. Through useDirection such a component reads the direction of the nearest provider and sets dir on its element itself, so its layout mirrors together with the controls inside it and users meet one consistent reading direction.
API Reference
DirectionProvider
| Prop | direction |
|---|---|
| Type | "ltr" | "rtl" |
| Default | "ltr" |
| Description | Sets the reading direction that the components inside the provider follow. The provider sets no dir attribute, so the element around it needs dir with the same value. |
| Prop | children |
|---|---|
| Type | ReactNode |
| Default | – |
| Description | Holds the app or the group of components that follows the direction. |
useDirection
useDirection returns the direction that the nearest DirectionProvider sets, "ltr" or "rtl". Without a provider, it returns "ltr". It is a hook, so it takes no arguments, renders no element and has no props of its own.