Skip to content

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

Components

Free

Direction for Svelte 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 Svelte 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.

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

<script lang="ts">
import { DirectionProvider } from "@/components/ui/direction";
</script>

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

FAQ

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

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 and lang on an element around the region, and a popup opened from inside takes both with it into the portal it renders in.

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. It reads the direction once, when the component is set up, and keeps that value if the provider's direction changes afterward.

Does DirectionProvider change the locale too?

On UI SDK 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"
Type
"ltr" | "rtl"
Default
"ltr"
class?string

Merged with the part's classes.

useDirection

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