Skip to content

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

Components

Free

Direction for UI SDK

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 UI SDK.

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 and sliders work the right way round.

It renders a div with display: contents that sets dir, which UI SDK's components and your CSS both read. 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 writes its direction as dir on an element of its own, so the layout and the components inside both follow it. The dir on the inner div repeats it, which does no harm.

<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 provider's own dir puts Start on the right while ms-auto pushes End to the left. Without the provider, the div keeps the row right to left but the label says ltr.

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, for useDirection in your own code, since UI SDK's components read the attribute itself.

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?

On UI SDK you mostly don't. Its components read the direction from CSS, which that attribute sets, so the arrow keys in tabs and toolbars, the side a popup opens on and a slider all follow it.

The provider is for useDirection in your own code, and for a region whose direction differs from the page's. There it writes dir on an element around the region, but a popup opened from inside renders outside that element, so give the popup's content the same dir.

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?

No, it only sets the direction. Date and number formatting follow each component's own locale settings, so an Arabic interface still needs those set where it shows dates, prices or counts.

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.

A popup opened from the region renders at the end of the page, outside it, so give its content the same dir.

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"
Type
"ltr" | "rtl"
Default
"ltr"
className?string

Merged with the part's classes.

useDirection

PropType
returns"ltr" | "rtl"
Type
"ltr" | "rtl"