# Direction

Provide left-to-right or right-to-left behavior to Zenos components and hooks.

Direction connects an application's reading direction to Zenos and the underlying Base UI behavior. Use it for keyboard navigation, popup alignment, sliders, menus, and other interactions that need to distinguish logical start from logical end.

```tsx
import { ArrowRightIcon } from "@phosphor-icons/react";
import { Badge, Button, Card, DirectionProvider, Field, Input, Stack, Switch, Text, useDirection } from "@zentara-co/zenos-ui";
import { useState } from "react";

type DirectionLanguage = "en" | "ar";

const directionContent = {
  en: {
    direction: "ltr",
    language: "en",
    title: "Workspace access",
    description: "Enter your details to continue to the workspace.",
    email: "Email address",
    remember: "Remember me",
    action: "Continue",
  },
  ar: {
    direction: "rtl",
    language: "ar",
    title: "الوصول إلى مساحة العمل",
    description: "أدخل بياناتك للمتابعة إلى مساحة العمل.",
    email: "البريد الإلكتروني",
    remember: "تذكرني",
    action: "متابعة",
  },
} as const;

function CurrentDirection() {
  const direction = useDirection();

  return (
    <Badge variant="solid" color="brand">
      {direction.toUpperCase()}
    </Badge>
  );
}

function DirectionExample() {
  const [language, setLanguage] = useState<DirectionLanguage>("ar");
  const content = directionContent[language];

  return (
    <Stack gap="4" align="center" className="w-full">
      <Stack direction="row" gap="2" role="group" aria-label="Preview language">
        <Button
          size="sm"
          variant={language === "en" ? "primary" : "ghost"}
          aria-pressed={language === "en"}
          onClick={() => setLanguage("en")}
        >
          English
        </Button>
        <Button
          size="sm"
          variant={language === "ar" ? "primary" : "ghost"}
          aria-pressed={language === "ar"}
          onClick={() => setLanguage("ar")}
        >
          العربية
        </Button>
      </Stack>

      <div dir={content.direction} lang={content.language} className="w-full max-w-xl">
        <DirectionProvider direction={content.direction}>
          <Card className="text-start">
            <Card.Header>
              <div className="flex items-start justify-between gap-3">
                <Stack gap="1">
                  <Card.Title>{content.title}</Card.Title>
                  <Card.Description>{content.description}</Card.Description>
                </Stack>
                <CurrentDirection />
              </div>
            </Card.Header>
            <Card.Content>
              <Stack gap="4">
                <Field.Root>
                  <Field.Label>{content.email}</Field.Label>
                  <Field.Control
                    render={<Input type="email" placeholder="name@example.com" dir="ltr" />}
                  />
                </Field.Root>
                <div className="flex items-center justify-between gap-4">
                  <Text size="s">{content.remember}</Text>
                  <Switch aria-label={content.remember} defaultChecked />
                </div>
              </Stack>
            </Card.Content>
            <Card.Footer className="justify-end">
              <Button variant="primary" endIcon={<ArrowRightIcon className="rtl:rotate-180" />}>
                {content.action}
              </Button>
            </Card.Footer>
          </Card>
        </DirectionProvider>
      </div>
    </Stack>
  );
}
```

## Import

```tsx
import { DirectionProvider, useDirection } from "@zentara-co/zenos-ui";
```

## Application direction

Set the document's native `dir` attribute and the provider direction from the same application state. The HTML attribute controls browser layout and text flow; the provider controls direction-aware component behavior.

```tsx
import { DirectionProvider } from "@zentara-co/zenos-ui";

export function App({ children }: { children: React.ReactNode }) {
  return (
    <html lang="ar" dir="rtl">
      <body>
        <DirectionProvider direction="rtl">{children}</DirectionProvider>
      </body>
    </html>
  );
}
```

Zenos does not update `document.documentElement` automatically. The application owns language selection, persistence, server-rendered attributes, and hydration, so it should keep both values synchronized.

## Read the current direction

[`useDirection`](/hooks/use-direction/) returns the nearest provider value. Without a provider it returns `ltr`.

```tsx
import { useDirection } from "@zentara-co/zenos-ui";

function DirectionStatus() {
  const direction = useDirection();
  return <span>Current direction: {direction}</span>;
}
```

Use this hook when custom interaction behavior changes with direction. Pure layout should usually rely on logical CSS properties and utilities such as `start`, `end`, `ms`, `me`, `ps`, and `pe`.

## Scoped direction

Direction can change inside a subsection without changing the entire application. Put the DOM attribute and provider on the same boundary.

```tsx
<section lang="he" dir="rtl">
  <DirectionProvider direction="rtl">
    <ConversationPreview />
  </DirectionProvider>
</section>
```

Use an explicit direction for interactive component trees. Reserve `dir="auto"` for isolated text whose writing direction is not known in advance.

## Portals

Place `DirectionProvider` above dialogs, menus, selects, popovers, tooltips, and other portalled components. Context keeps their positioning and keyboard behavior aligned even when the popup is mounted outside the local DOM subtree. The document-level `dir` value still controls the portal's text flow and logical CSS.

## Anatomy

```text
DirectionProvider
useDirection
```

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| direction | ltr \| rtl | ltr | Reading direction supplied to Zenos and Base UI behavior. |
| children | ReactNode | — | Component tree that inherits the configured direction. |
| useDirection() | () => ltr \| rtl | — | Returns the nearest provider direction, falling back to ltr. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

Set a matching `lang` attribute so assistive technology uses the correct language and pronunciation. Test focus order, arrow keys, text alignment, mirrored directional icons, and popup placement in both directions. Direction alone does not translate labels or make physical `left` and `right` styles logical.

For layout guidance, directional icons, mixed-direction content, and a testing checklist, see the broader [RTL foundation guide](/foundations/rtl/).

Source: https://zenos-ui.pages.dev/components/direction/