# Adaptive UI

> Adaptive layout for narrow containers and touch, vertical card rows, full-screen sheets, and touch selection.

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

`adaptive` turns on the adaptive UI. `true` is `{ target: 'both' }`,
which applies at every width. `target: 'mobile'` applies only while the grid
is narrower than `breakpoint` (640 px by default), and `target: 'desktop'`
only while it is at least that wide. The width is the grid's own container
width, not the device or the window, so a grid in a narrow sidebar adapts on
a wide screen too. The grid fills its parent's width, and `height="100%"` fills a parent with
a fixed height and follows it when the parent resizes.

```tsx
<DataGrid data={orders} columns={columns} adaptive={{ target: 'mobile', rowLayout: 'vertical' }} sorting filtering={{ ui: 'excel' }} />
```

## Server rendering

The server cannot measure a container, so it renders the mode the prop alone
gives: adaptive for `target: 'both'`, and `defaultActive` (default `false`)
for `mobile` and `desktop`. After mount the grid measures its width and
switches once if needed. Hydration always matches the server markup. If you
know the client is a phone (for example from a user-agent hint), pass
`defaultActive: true` to avoid the switch.

## Vertical rows

`rowLayout` defaults to `'horizontal'` (the desktop rows).
`rowLayout: 'vertical'` renders each record as a card with one label and
value line per visible column. Cards are drawn from the same
virtualized row window as horizontal rows, with a fixed card height of
`visible columns × rowHeight + 24 px`, so 100,000 records scroll with the
same row virtualization, paging and selection as the desktop layout.
The accessibility tree does not change. Each card is a `role="row"`, each
field is a `role="gridcell"` with the column's `aria-colindex`, and the
column headers stay in the tree, visually hidden. Arrow keys move between
fields and records as they do between cells.

In vertical mode a bar above the grid offers **Sort** and **Filter**. Sort
opens a full-screen sheet where each sortable column picks None, Ascending or
Descending. Filter lists the filterable columns; choosing one opens
its filter menu as a full-screen sheet. `api.openSortDialog()` and
`api.openFilterDialog(field?)` open them from code, and
`onBeforeAdaptiveDialogOpen` can cancel either one.

## Sheets

While adaptive, every grid dialog renders as a full-screen sheet with a
labelled close button and a focus trap. This covers the filter menu, the
column chooser, the sort sheet and the edit dialog. Menus (the
column menu, context menu and toolbar overflow) become bottom sheets with
44 px items.

## Editing

With `editing={{ mode: 'dialog' }}` the edit form fills the screen while the
grid is adaptive. The title and a 44 px **Close** button stay at the
top, **Save** and **Cancel** stay at the bottom, and the fields scroll between
them. The form is a native modal `<dialog>`: the page behind it is inert,
focus moves to the first field, Escape or Close discards the draft, and
focus returns to the cell that opened it. `editing.dialogProps.width` does
not apply to the sheet. Outside adaptive mode the same dialog is a centred
modal again.

```tsx
<DataGrid data={orders} columns={columns} adaptive={{ target: 'mobile', rowLayout: 'vertical' }} editing={{ mode: 'dialog' }} toolbar={['add', 'edit', 'delete']} />
```

Dialog editing is the editing path for vertical rows. Double-click a card (a
double-tap on touch screens), press F2 on a focused field, use the
toolbar **Edit** item on the selected record, or call `api.startEdit(rowId)`.
**Add** and `api.addRow()` open the same sheet for a new record. The cards
behind the sheet stay virtualized and keep their scroll position, so a
record deep in a 100,000-row list edits the same way as the first one, and
the card shows the saved values when the sheet closes.

## Touch

Tap selects. Swiping scrolls the records with native touch
scrolling, vertically through the cards and also sideways through the
columns of horizontal rows. A swipe never selects a record or starts a
long-press: a swipe on the grid is touch scrolling, and rows have no swipe
actions. Long-press a header to
open its column menu, or a cell to open the context menu when `contextMenu`
is set. Otherwise, long-pressing
a record with multiple selection enabled starts multi-select mode and adds
that record. After the first touch selection a **Select multiple** toggle
appears in the adaptive bar, showing the selected count. While it is on, taps
add and remove records. Toolbar items, pager buttons, group chips,
expand toggles and checkboxes get 44 px touch targets. The pager
drops its numeric buttons at narrow widths and hides the page-size label,
and the toolbar moves items that do not fit into its overflow
menu, which opens as a bottom sheet.

### Live demo: Adaptive UI

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

Adaptive mode as a container query on the grid's own width, vertical card rows over the same virtualized row window, full-screen sort, filter, column chooser and edit sheets, bottom-sheet menus, touch long-press, native swipe scrolling and the multi-select toggle, and 44 px targets.

`Demo.tsx`:

```tsx
'use client';

import { useRef, useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';
import { DataGrid, type DataGridAdaptive, type DataGridApi, type DataGridColumn, type DataGridEditing, type DataGridToolbarItemId } from '@salmonui/data-grid';
import { EventLog, useEventLog } from '../../components/demo-kit';
import { createOrders, type Order } from '../../lib/fixtures';

const orders = createOrders(20_000, 11);

const columns: DataGridColumn<Order>[] = [
  { field: 'id', header: 'Order', primaryKey: true, width: 110 },
  { field: 'customer', header: 'Customer', width: 160 },
  { field: 'country', header: 'Country', width: 120 },
  { field: 'priority', header: 'Priority', width: 110 },
  { field: 'amount', header: 'Amount', type: 'number', format: 'C2', align: 'end', width: 120 },
  { field: 'placed', header: 'Placed', type: 'datetime', format: 'yMMMd', width: 130 },
];

// Constants: option objects are reactive props.
const sorting = { multiple: true };
const filtering = { ui: 'excel' as const };
const selection = { multiple: true };
const paging = { pageSize: 50, pageSizeOptions: [20, 50, 100] };
// Dialog editing: while adaptive, the edit form is a full-screen sheet.
const editing: DataGridEditing<Order> = { mode: 'dialog' };
const toolbar: readonly DataGridToolbarItemId[] = ['add', 'edit', 'search', 'columnChooser'];

const widths = { phone: 380, tablet: 700, desktop: 1000 } as const;

const TARGETS = [
  { value: 'mobile', label: 'mobile (container below 640 px)' },
  { value: 'desktop', label: 'desktop (container 640 px and wider)' },
  { value: 'both', label: 'both' },
];
const FRAMES = [
  { value: 'phone', label: '380 px' },
  { value: 'tablet', label: '700 px' },
  { value: 'desktop', label: '1000 px' },
];

export default function AdaptiveDemo() {
  const api = useRef<DataGridApi<Order> | null>(null);
  const [target, setTarget] = useState<NonNullable<DataGridAdaptive['target']>>('mobile');
  const [vertical, setVertical] = useState(true);
  const [frame, setFrame] = useState<keyof typeof widths>('phone');
  const [log, push] = useEventLog();
  // `defaultActive` matches the initial 380 px frame, so the server already renders cards.
  const adaptive: DataGridAdaptive = { target, rowLayout: vertical ? 'vertical' : 'horizontal', breakpoint: 640, defaultActive: true };
  return (
    <>
      <div className="demo-toolbar">
        <label>
          target
          <Select aria-label="target" data={TARGETS} value={target} onValueChange={(value) => value != null && setTarget(value as typeof target)} />
        </label>
        <Checkbox checked={vertical} onCheckedChange={(checked) => setVertical(checked)}>
          vertical rows
        </Checkbox>
        <label>
          frame
          <Select aria-label="frame" data={FRAMES} value={frame} onValueChange={(value) => value != null && setFrame(value as typeof frame)} />
        </label>
        <Button variant="outline" onClick={() => api.current?.openSortDialog()}>
          Open sort sheet
        </Button>
        <Button variant="outline" onClick={() => api.current?.openFilterDialog('country')}>
          Open Country filter sheet
        </Button>
        <Button variant="outline" onClick={() => api.current?.startEdit()}>
          Edit selected record
        </Button>
      </div>
      <div style={{ maxWidth: widths[frame] }}>
        <DataGrid<Order>
          aria-label="Adaptive orders"
          data={orders}
          columns={columns}
          adaptive={adaptive}
          sorting={sorting}
          filtering={filtering}
          selection={selection}
          paging={paging}
          editing={editing}
          toolbar={toolbar}
          columnMenu
          columnChooser
          height={520}
          locale="en-US"
          currency="USD"
          timeZone="UTC"
          ref={api}
          onBeforeAdaptiveDialogOpen={(event) => push(`beforeAdaptiveDialogOpen ${event.kind}${event.field ? ` ${event.field}` : ''} (${event.source})`)}
          onSave={(event) => push(`save ${event.status} ${event.changed.map((c) => `${c.rowId} [${c.fields.join(', ')}]`).join('; ')}`)}
          onSortChange={(event) => push(`sortChange ${event.sortBy.map((s) => `${s.field} ${s.direction}`).join(', ') || 'none'}`)}
        />
      </div>
      <p className="demo-status">On a touch screen, long-press a header for its column menu, or long-press a record to start multi-select and tap more records to add them. Double-click (or double-tap) a record, or select one and use Edit, to edit it in the full-screen sheet. Swiping scrolls the records natively.</p>
      <EventLog entries={log} />
    </>
  );
}
```

## API reference

[`DataGridAdaptive`](https://salmonui.dev/docs/reference/data-grid/DataGridAdaptive) lists
the `adaptive` options,
[`DataGridAdaptiveDialogOpenEvent`](https://salmonui.dev/docs/reference/data-grid/DataGridAdaptiveDialogOpenEvent)
the `onBeforeAdaptiveDialogOpen` payload, and
[`DataGridApi`](https://salmonui.dev/docs/reference/data-grid/DataGridApi) the
`openSortDialog` and `openFilterDialog` methods.
