# Right-to-left

> RTL layout, keyboard and scrolling.

Canonical: https://salmonui.dev/docs/data-grid/rtl

A grid inside a `dir="rtl"` ancestor, or with an explicit `dir` prop,
renders right to left. `dir` takes `'ltr'`, `'rtl'` or
`'auto'`; `'auto'` follows the script of the grid `locale`, so
`locale="ar-EG"` with `dir="auto"` is right to left (see
[Localization](https://salmonui.dev/docs/data-grid/localization)). Without `dir` the grid inherits the
ancestor direction. All grid CSS uses logical properties, so the layout,
alignment, indents and frozen zones mirror without RTL-specific styles.
Arrow keys move in visual order: in RTL, ArrowLeft moves to the
next column, and the pager arrow keys flip the same way. Horizontal
scroll offsets are normalized across browsers, so column virtualization
works in RTL.

In an RTL grid each body cell lays out its text with its own direction
(`unicode-bidi: plaintext`), so a left-to-right value such as a German
date ("1. März 2014") or an amount keeps its order instead of being
reordered around the neutral characters, while Arabic or Hebrew text stays
right to left; cells stay aligned to the grid's start side (and `align:
'end'` to its end side).

```tsx
<DataGrid data={orders} columns={columns} locale="ar-EG" dir="auto" />
```

The resolved `dir` is computed from props alone (the `'auto'` case from the
`locale` string), so the server renders the same `dir` attribute the client
hydrates with; an inherited direction needs no prop, since the logical CSS
mirrors the server markup without script.

## Pinned columns, resizing and reordering

Pinning is logical: `pinned: 'start'` and `pinnedColumns.start` pin to the
right edge in RTL and `'end'` to the left edge, so the same column
definitions serve both directions. The resize handle sits on the
column's end edge, the left edge in RTL; dragging it to the left widens the
column, and the keyboard resize keys on a focused header
(Ctrl+Alt+ArrowLeft, Ctrl+Alt+ArrowRight) follow the visual direction, as do
the reorder keys (Alt+ArrowLeft, Alt+ArrowRight). While a header is dragged,
the drop indicator and the drag preview are positioned from the logical
start, so they track the pointer in either direction. `onColumnResizeEnd`
and `onColumnReorder` report the same widths and visible indices as in LTR.

### Live demo: Right-to-left

Live demo: https://salmonui.dev/demos/data-grid/rtl

An RTL grid with 30 virtualized columns - mirrored layout and pinned zones, flipped arrow keys, mirrored resize handles and drag indicators, and normalized horizontal scrolling.

`Demo.tsx`:

```tsx
'use client';

import { DataGrid, type DataGridColumn } from '@salmonui/data-grid';
import { EventLog, useEventLog } from '../../components/demo-kit';
import { createWideRows, type WideRow } from '../../lib/fixtures';
import { createWideColumns } from '../../lib/wide';

const rows = createWideRows(10_000);
// `#` is pinned to the start (the right edge in RTL) and the last column to the
// end (the left edge); the rest scroll between them.
const columns: DataGridColumn<WideRow>[] = createWideColumns(30).map((column, index, all) =>
  index === 0 ? { ...column, pinned: 'start' } : index === all.length - 1 ? { ...column, pinned: 'end' } : column,
);

export default function RtlDemo() {
  const [log, push] = useEventLog();
  // The direction is inherited from an ancestor `dir` (or set with the `dir` prop).
  return (
    <>
      <div dir="rtl" lang="ar">
        <DataGrid<WideRow>
          aria-label="Right-to-left grid with column virtualization"
          data={rows}
          columns={columns}
          height={340}
          locale="en-US"
          resizable
          reorderable
          onColumnResizeEnd={(event) => push(`resize (${event.source}): ${event.field} ${Math.round(event.previousWidth)} → ${Math.round(event.width)}`)}
          onColumnReorder={(event) => push(`reorder (${event.source}): ${event.field} ${event.fromIndex} → ${event.toIndex}`)}
        />
      </div>
      {/* The log is English app text: it stays left to right outside the RTL subtree. */}
      <EventLog entries={log} />
    </>
  );
}
```

## API reference

`dir` is documented on
[`DataGridProps`](https://salmonui.dev/docs/reference/data-grid/DataGridProps).
