Skip to content
All components
Primitives

Direction

Tell the Radix primitives in a subtree whether text runs left-to-right or right-to-left, so their keyboard behavior and positioning match the content.

Provider that tells every Radix primitive below it which way text runs.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add direction

Then import it from @/components/ui/direction. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

Left to right

Provide left-to-right direction to the example's descendants. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { DirectionProvider } from "@/components/ui/direction"
import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbLink,
  BreadcrumbList,
  BreadcrumbPage,
  BreadcrumbSeparator,
} from "@/components/ui/breadcrumb"
import { Slider } from "@/components/ui/slider"

function Panel({ label }: { label: string }) {
  return (
    <div className="flex w-full flex-col gap-4 rounded-16 border border-soft-200 p-4">
      <p className="text-subheading-xs text-soft-400">{label}</p>
      <Breadcrumb aria-label={`${label} breadcrumb`}>
        <BreadcrumbList>
          <BreadcrumbItem>
            <BreadcrumbLink href="#">Home</BreadcrumbLink>
          </BreadcrumbItem>
          <BreadcrumbSeparator />
          <BreadcrumbItem>
            <BreadcrumbPage>Direction</BreadcrumbPage>
          </BreadcrumbItem>
        </BreadcrumbList>
      </Breadcrumb>
      <Slider defaultValue={[30]} aria-label={`${label} example value`} />
    </div>
  )
}

export function DirectionLeftToRight() {
  return (
    <div className="flex w-full max-w-lg flex-col gap-4">
      <DirectionProvider dir="ltr">
        <Panel label="ltr" />
      </DirectionProvider>
    </div>
  )
}

Right to left

Provide right-to-left direction to the example's descendants. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { DirectionProvider } from "@/components/ui/direction"
import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbLink,
  BreadcrumbList,
  BreadcrumbPage,
  BreadcrumbSeparator,
} from "@/components/ui/breadcrumb"
import { Slider } from "@/components/ui/slider"

function Panel({ label }: { label: string }) {
  return (
    <div className="flex w-full flex-col gap-4 rounded-16 border border-soft-200 p-4">
      <p className="text-subheading-xs text-soft-400">{label}</p>
      <Breadcrumb aria-label={`${label} breadcrumb`}>
        <BreadcrumbList>
          <BreadcrumbItem>
            <BreadcrumbLink href="#">Home</BreadcrumbLink>
          </BreadcrumbItem>
          <BreadcrumbSeparator />
          <BreadcrumbItem>
            <BreadcrumbPage>Direction</BreadcrumbPage>
          </BreadcrumbItem>
        </BreadcrumbList>
      </Breadcrumb>
      <Slider defaultValue={[30]} aria-label={`${label} example value`} />
    </div>
  )
}

export function DirectionRightToLeft() {
  return (
    <div className="flex w-full max-w-lg flex-col gap-4">
      <DirectionProvider dir="rtl">
        <div dir="rtl">
          <Panel label="rtl" />
        </div>
      </DirectionProvider>
    </div>
  )
}

Nested override

Nest a left-to-right provider inside a right-to-left region. useDirection reads the nearest provider, so the inner slider uses left-to-right keys.

"use client"

import { DirectionProvider, useDirection } from "@/components/ui/direction"

import { Slider } from "@/components/ui/slider"

function DirectionReadout() {
  const direction = useDirection()
  return (
    <p className="text-paragraph-sm text-sub-600">
      Primitives in this panel read direction: {direction}
    </p>
  )
}

export function DirectionNestedOverride() {
  return (
    <div className="flex w-full max-w-lg flex-col gap-4">
      <DirectionProvider dir="rtl">
        <div dir="rtl" className="flex flex-col gap-3 rounded-16 border border-soft-200 p-4">
          <DirectionReadout />
          <DirectionProvider dir="ltr">
            <div dir="ltr" className="flex flex-col gap-3 rounded-12 border border-soft-200 p-3">
              <DirectionReadout />
              <Slider defaultValue={[60]} aria-label="Embedded code editor font size" />
            </div>
          </DirectionProvider>
        </div>
      </DirectionProvider>
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

DirectionProvider

Not a visual component: it tells every Radix primitive below it which way text runs, so menus, sliders, carousels and tooltips flip together instead of one at a time. Setting dir on <html> covers the document; this covers a subtree, which is what a preview pane or an embedded editor needs.

DirectionProvider props
PropTypeDefaultDescription
childrenReact.ReactNode—Represents all of the things React can render.
direction"ltr" | "rtl" | undefined—Text and layout direction supplied to descendants: ltr or rtl. Match the content's reading direction.
Inherited props (1)
DirectionProvider inherited props
PropTypeDefaultDescription
dir(required)"ltr" | "rtl"—Reading direction used by the primitive. Keep it consistent with the surrounding content and Direction provider.

Accessibility & usage

From registry/ui/direction.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • An app or section localized into a right-to-left language such as Arabic or Hebrew.
  • A subtree whose direction differs from the document, such as a preview pane, an embedded editor or a left-to-right code area inside a right-to-left page.
  • Components that need to read the current direction through useDirection to mirror their own behavior.

When not to use

  • To translate text, format dates and numbers, or pick a locale; that is internationalization, not direction.
  • To mirror layout by itself: the provider does not set the DOM dir attribute or flip CSS. Set dir on the element too.
  • In a left-to-right-only product with dir set on <html>; Radix already defaults to left-to-right.

Anatomy

DirectionProvider
Wraps a subtree. Requires dir (ltr or rtl); the optional direction prop overrides it. Renders no DOM element.
useDirection
Hook that returns the nearest provider's direction, for custom components that need direction-aware behavior.

Keyboard & interaction

  • Radix primitives below the provider swap Left and Right arrow behavior in rtl: sliders, toggle groups, menus, tabs and carousels move in the reading direction.
  • Nested providers override the outer one for their subtree only.
  • The provider renders no element and adds no focus stops or semantics.

Responsive behavior

  • Direction does not change with viewport size; the same rules apply on every screen.
  • Use logical spacing and alignment utilities (ms-*, me-*, ps-*, pe-*, text-start) so layouts mirror correctly at every width.
  • Check right-to-left layouts at 320 CSS pixels as well: truncation, overflow and sticky elements must sit on the correct side.

Content guidelines

  • Set dir on the element that contains the content and lang for the language, alongside the provider.
  • Mixed-direction text (a left-to-right product name in a right-to-left sentence) may need <bdi> or dir="auto" on that inline element.
  • Direction-sensitive icons such as back and forward arrows must point in the reading direction.

Resources