# Direction Provider

> A context that switches components to right-to-left behavior.

```tsx title="hero.tsx"
import type { TextDirection } from '@langgenius/dify-ui/direction-provider'
import { DirectionProvider } from '@langgenius/dify-ui/direction-provider'
import { SegmentedControl, SegmentedControlItem } from '@langgenius/dify-ui/segmented-control'
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderLabel,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from '@langgenius/dify-ui/slider'
import { Tabs, TabsIndicator, TabsList, TabsPanel, TabsTab } from '@langgenius/dify-ui/tabs'
import * as React from 'react'

const copy = {
  ltr: {
    lang: 'en',
    tabsLabel: 'Model settings',
    tabs: [
      {
        value: 'prompt',
        label: 'Prompt',
        panel: 'Instructions the model follows in every conversation.',
      },
      {
        value: 'parameters',
        label: 'Parameters',
        panel: 'Temperature, top P and the maximum number of tokens.',
      },
      { value: 'debug', label: 'Debug', panel: 'Send test messages and inspect each run.' },
    ],
    temperature: 'Temperature',
  },
  rtl: {
    lang: 'ar',
    tabsLabel: 'إعدادات النموذج',
    tabs: [
      { value: 'prompt', label: 'الموجّه', panel: 'التعليمات التي يتبعها النموذج في كل محادثة.' },
      {
        value: 'parameters',
        label: 'المعلمات',
        panel: 'درجة الحرارة وقيمة Top P والحد الأقصى للرموز.',
      },
      { value: 'debug', label: 'تصحيح الأخطاء', panel: 'أرسل رسائل تجريبية وافحص كل تشغيل.' },
    ],
    temperature: 'درجة الحرارة',
  },
} as const

export function DirectionProviderHeroDemo() {
  const [direction, setDirection] = React.useState<TextDirection>('rtl')
  const text = copy[direction]

  return (
    <div className="flex w-full max-w-96 flex-col gap-4">
      <SegmentedControl
        aria-label="Text direction"
        value={direction}
        onValueChange={setDirection}
        className="self-center"
      >
        <SegmentedControlItem value="ltr">Left to right</SegmentedControlItem>
        <SegmentedControlItem value="rtl">Right to left</SegmentedControlItem>
      </SegmentedControl>
      <DirectionProvider direction={direction}>
        <div
          dir={direction}
          lang={text.lang}
          className="flex flex-col gap-4 rounded-xl border border-divider-subtle bg-background-body p-4"
        >
          <Tabs defaultValue="prompt">
            <TabsList aria-label={text.tabsLabel} className="border-b border-divider-subtle">
              {text.tabs.map((tab) => (
                <TabsTab key={tab.value} value={tab.value}>
                  {tab.label}
                </TabsTab>
              ))}
              <TabsIndicator />
            </TabsList>
            {text.tabs.map((tab) => (
              <TabsPanel
                key={tab.value}
                value={tab.value}
                className="mt-1 rounded-lg px-2 py-2.5 system-sm-regular text-text-secondary"
              >
                {tab.panel}
              </TabsPanel>
            ))}
          </Tabs>
          <Slider defaultValue={0.7} min={0} max={2} step={0.1} className="flex-col gap-1">
            <div className="flex items-center justify-between">
              <SliderLabel>{text.temperature}</SliderLabel>
              <SliderValue className="system-sm-medium text-text-secondary tabular-nums" />
            </div>
            <SliderControl>
              <SliderTrack>
                <SliderIndicator />
                <SliderThumb />
              </SliderTrack>
            </SliderControl>
          </Slider>
        </div>
      </DirectionProvider>
    </div>
  )
}
```

## Usage guidelines

- **Set `dir` as well**: `DirectionProvider` tells components which way the inline axis runs, so arrow keys, slider values and popup placement follow it. In the example above, right to left makes `ArrowLeft` move to the next tab and raise the slider value. The provider renders no element and does not change CSS. Set the HTML `dir` attribute on the same subtree, usually on `<html>`, so layout, logical utilities and `:dir()` selectors follow.
- **Wrap the app once**: Put one provider near the root with the locale's direction. Nest another only for a region written in a different direction from the page.
- **Pass `dir` to popups in a nested region**: Popups render in a portal under `<body>`, outside the region's `dir`. Read the direction with `useDirection` from a component inside the provider and pass it as `dir` to the content part of every popup in the region, including submenus and popups opened from another popup. A page that sets `dir` on `<html>` needs no extra step.
- **Write direction-neutral styles**: Use logical utilities such as `ps-*`, `me-*`, `start-*` and `text-start` for spacing, offsets, borders and alignment, so one class list serves both directions. Caller overrides use the same family, because `cn` does not merge `pl-*` with `ps-*`.
- **Read the direction only when a value must leave CSS**: `useDirection` returns `'ltr'` or `'rtl'` for code that computes geometry or passes `dir` to portaled content. Prefer a `:dir(rtl)` or `rtl:` variant for anything CSS can express.

## Anatomy

```tsx
import { DirectionProvider, useDirection } from '@langgenius/dify-ui/direction-provider'
```

```tsx
<DirectionProvider direction="rtl">
  <div dir="rtl" />
</DirectionProvider>
```

## Examples

### Popups in a right-to-left region

Placement and keyboard read the provider, so the submenu opens toward the left and `ArrowLeft` opens it. Text, logical utilities and `:dir()` read the DOM instead, and a portaled popup inherits the direction and language of `<body>`. Each menu here receives `dir` from `useDirection` and `lang="ar"`, so its items, submenu chevron and screen-reader language all match the region.

```tsx title="portaled-popup.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { DirectionProvider, useDirection } from '@langgenius/dify-ui/direction-provider'
import {
  Menu,
  MenuContent,
  MenuItem,
  MenuSeparator,
  MenuSubmenu,
  MenuSubmenuTrigger,
  MenuTrigger,
} from '@langgenius/dify-ui/menu'

const folders = ['وثائق المنتج', 'تذاكر الدعم', 'ملاحظات الإصدار']

function DocumentMenu() {
  const direction = useDirection()

  return (
    <Menu>
      <MenuTrigger
        render={
          <Button className="data-popup-open:bg-components-button-secondary-bg-hover data-popup-open:inset-ring-components-button-secondary-border-hover" />
        }
      >
        إجراءات المستند
        <span aria-hidden="true" className="i-ri-arrow-down-s-line size-4 text-text-tertiary" />
      </MenuTrigger>
      <MenuContent dir={direction} lang="ar" className="w-48">
        <MenuItem>إعادة التسمية</MenuItem>
        <MenuItem>تنزيل</MenuItem>
        <MenuSubmenu>
          <MenuSubmenuTrigger>نقل إلى</MenuSubmenuTrigger>
          <MenuContent dir={direction} lang="ar" className="w-44">
            {folders.map((folder) => (
              <MenuItem key={folder}>{folder}</MenuItem>
            ))}
          </MenuContent>
        </MenuSubmenu>
        <MenuSeparator />
        <MenuItem variant="destructive">حذف</MenuItem>
      </MenuContent>
    </Menu>
  )
}

export function DirectionProviderPortaledPopupDemo() {
  return (
    <DirectionProvider direction="rtl">
      <div dir="rtl" lang="ar">
        <DocumentMenu />
      </div>
    </DirectionProvider>
  )
}
```

## API reference

### DirectionProvider

Enables RTL behavior for Dify UI and Base UI components. Doesn't render its own HTML element.

Inherits `children`, `direction` from [Base UI DirectionProvider](https://base-ui.com/react/utils/direction-provider).

### useDirection

```ts
useDirection(): 'ltr' | 'rtl'
```

**Returns:** `'ltr' | 'rtl'`
