Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Direction for Ark UI

Tell the components inside which way text reads, for a whole app or one region, so arrow keys and menus flip with it. A provider for React on Ark UI.

On this page8 sections

Direction tells the components inside it which way the text reads, left to right or right to left, for a whole app or for one region of a page. Wrap an Arabic, Hebrew or Persian interface in it so arrow keys, sliders and submenus work the right way round.

It renders no element, so the dir attribute that flips your CSS is still yours to set. For one field where people may type in either script, dir="auto" on an Input is enough.

Preview width
StartltrEnd

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Direction anatomy: ltr, rtl, start, end

Usage

import { DirectionProvider } from "@/components/ui/direction";

The provider tells components the direction, and the dir attribute on the element inside it is what mirrors the layout, so set both to the same value.

<DirectionProvider direction="rtl">
  <div dir="rtl">
    <p>Everything in here reads right to left.</p>
  </div>
</DirectionProvider>

Demos

RTL

A row with Start, the current direction and End, inside a DirectionProvider set to rtl and a div with dir set to rtl. The middle label prints what useDirection reads from the provider, and the attribute puts Start on the right while ms-auto pushes End to the left. Drop either one and the label and the layout disagree.

Preview width

FAQ

How do I set up a right-to-left (RTL) layout in React?

Do two things at the top of the app. Set dir to rtl on the html element, with a lang such as ar or he, which flips the CSS and turns on Tailwind's rtl: variant. Then wrap the app in DirectionProvider with direction set to rtl, which tells the components.

Write your own styles with logical classes, such as ms-4, ps-2, start-0 and text-start, and they mirror with no further work, as the ms-auto in the RTL demo does.

Why do I need DirectionProvider if the html tag already has dir?

The attribute is for CSS, and the components don't read it.

Ark UI takes the direction only from this provider and assumes left to right without it. Its components write that direction on their own elements, so without the provider a Scroll Area or a Resizable gets dir="ltr" and reads left to right even on a right-to-left page. A dir prop on one component only sets the attribute on its outer element: Ark UI doesn't read it, so that component's arrow keys stay left to right, as do the parts it writes dir="ltr" on, such as a Resizable's panels.

How do I read the current text direction in a component?

Call useDirection from a component inside the provider. It returns "ltr" or "rtl", which is enough to swap an icon, pick an animation's starting side, or set dir on an element you render yourself. The middle label of the RTL demo prints it.

Outside any provider it returns "ltr", whatever the page's dir says.

Does DirectionProvider change the locale too?

On Ark UI it does. The provider is a LocaleProvider underneath, and when you pass no locale, direction picks one: ar-BH for rtl and en-US otherwise. Components inside that format dates or numbers use it, so pass locale as well, such as he-IL or fa-IR, and the direction then comes from that locale.

The lang attribute is separate in every case, so set it next to dir for screen readers to read the text in the right voice.

Can only part of the page be right-to-left?

Yes. Providers nest, and the nearest one wins, so wrap that region in its own DirectionProvider and set dir on the region's outer element. Everything outside keeps the page's direction.

This suits a preview of a translated email, a quoted message in Hebrew inside an English thread, or two editors side by side, one per language.

How do I make my own CSS work in both directions?

Use logical utilities instead of left and right: ms-* and me-* for margins, ps-* and pe-* for padding, start-* and end-* for position, text-start, border-s and rounded-s-*. They follow the dir attribute with no extra code, which is how the demo's End label moves to the far side.

Keep the rtl: variant for what logical properties can't express, such as flipping an arrow icon. It also reads the attribute, not the provider, so it only works where dir is set.

Should icons flip in a right-to-left layout?

Flip the ones that point forward or back in reading order: arrows, the chevrons on a breadcrumb or a next button, a reply or send icon. Add rtl:-scale-x-100 to mirror them, or choose the icon with useDirection.

Checkmarks, clocks, search glasses and logos stay as they are, and so do media controls, since a play button points the way the tape runs, not the way the text reads.

API Reference

DirectionProvider

PropType
direction?"ltr" | "rtl"

The reading direction it sets for everything inside it.

Type
"ltr" | "rtl"
locale?string

The locale it sets. When unset, direction picks one (ar-BH for rtl, * en-US otherwise).

useDirection