# useDirection

Read the nearest left-to-right or right-to-left direction context.

`useDirection` returns the reading direction supplied by the nearest `DirectionProvider`. Use it when custom interaction behavior must distinguish logical start from logical end.

## Import

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

## Usage

```tsx
"use client";

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

export function DirectionStatus() {
  const direction = useDirection();

  return <span>Current direction: {direction}</span>;
}
```

The return value is `"ltr" | "rtl"`. Without a provider, the hook returns `"ltr"`.

## With DirectionProvider

Keep the provider direction and the native DOM `dir` attribute synchronized. The provider supplies component behavior; `dir` controls browser layout and text flow.

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

function Toolbar() {
  const direction = useDirection();
  const previousKey = direction === "rtl" ? "ArrowRight" : "ArrowLeft";

  return <p>Previous item key: {previousKey}</p>;
}

export function ArabicWorkspace() {
  return (
    <section lang="ar" dir="rtl">
      <DirectionProvider direction="rtl">
        <Toolbar />
      </DirectionProvider>
    </section>
  );
}
```

## When to use it

Use `useDirection` for behavior that cannot be expressed through CSS, including direction-aware arrow-key handling, carousel traversal, or application-owned positioning logic.

For layout, prefer logical CSS properties and utilities such as `start`, `end`, `ms`, `me`, `ps`, and `pe`. CSS automatically follows the nearest DOM direction and does not require a React render.

## Server rendering

The hook reads React context and does not access browser APIs. It returns the provider value during server rendering, or `ltr` when no provider exists, so the server and client remain deterministic when the provider tree is the same.

See [Direction](/components/direction/) for provider setup, scoped direction, and portal guidance. See the [RTL foundation guide](/foundations/rtl/) for layout, icon, content, and testing recommendations.

Source: https://zenos-ui.pages.dev/hooks/use-direction/