Data grid example

A portable column layout.

Order, visibility, pinning, widths and density share one presentation object. Editing remains a local draft.

Complete React client module · MIT · Next.js and Vite

Interactive example

A portable column layout

Order, visibility, pinning, widths and density share one presentation object. Editing remains a local draft.

Keyboard

  • Use Columns to show, hide, pin and reorder fields without dragging.
  • Tab to a column resize separator. Left and Right adjust by 10 pixels; Home uses the minimum and End uses the maximum.
  • Arrow keys move between visible cells. Shift extends a range; Enter edits and Escape cancels.
  • Pinned start and end follow the reading direction, including right-to-left layouts.

States covered

  • controlled view
  • column order
  • column visibility
  • logical pinning
  • pointer resize
  • keyboard resize
  • density
  • right-to-left layout
  • local draft editing

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-data-grid-column-layout.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useState } from 'react';
import { Button, DataGrid, createDataGridViewState, type DataColumn, type DataEdit, type DataGridViewState } from '@bld8/ui';

type RecordRow = { id: string; revision: number; title: string; status: string; owner: string; estimate: number };
const columns: DataColumn<RecordRow>[] = [
  { key: 'id', label: 'Record', type: 'text', hideable: false, pinned: 'start', width: 112, minWidth: 100, sortable: false },
  { key: 'title', label: 'Summary', type: 'text', editable: true, minWidth: 180 },
  { key: 'status', label: 'Stage', type: 'select', editable: true, minWidth: 140, options: [ { value: 'draft', label: 'Draft' }, { value: 'review', label: 'Review' }, { value: 'ready', label: 'Ready' } ] },
  { key: 'owner', label: 'Coordinator', type: 'text', editable: true, minWidth: 140 },
  { key: 'estimate', label: 'Points', type: 'number', editable: true, width: 100, minWidth: 88, pinned: 'end' }
];
const initialRows: RecordRow[] = [
  { id: 'REC-21', revision: 1, title: 'Review empty-state wording', status: 'review', owner: 'Remy Cole', estimate: 3 },
  { id: 'REC-22', revision: 1, title: 'Align the schedule toolbar', status: 'draft', owner: 'Imani Bell', estimate: 5 },
  { id: 'REC-23', revision: 1, title: 'Check keyboard return focus', status: 'ready', owner: 'Remy Cole', estimate: 2 }
];
export default function PortableGridLayoutExample() {
  const [rows, setRows] = useState(initialRows);
  const [view, setView] = useState<DataGridViewState>(() => createDataGridViewState(columns));
  const [rtl, setRtl] = useState(false);
  function edit(edits: DataEdit<RecordRow>[]) {
    setRows(current => current.map(row => edits.filter(change => change.id === row.id).reduce((next, change) => {
      if (change.key === 'estimate') { const value = Number(change.value); return Number.isFinite(value) ? { ...next, estimate: value } : next; }
      if (change.key === 'title' || change.key === 'status' || change.key === 'owner') return { ...next, [change.key]: String(change.value) };
      return next;
    }, row)));
  }
  return <div style={{ display: 'grid', gap: 16, minWidth: 0 }}>
    <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
      <Button variant="secondary" size="sm" onClick={() => setRtl(current => !current)}>{rtl ? 'Use left-to-right layout' : 'Use right-to-left layout'}</Button>
      <Button variant="secondary" size="sm" onClick={() => setView(current => ({ ...current, visibleColumnKeys: current.visibleColumnKeys.includes('status') ? current.visibleColumnKeys.filter(key => key !== 'status') : [...current.visibleColumnKeys, 'status'] }))}>Toggle stage column</Button>
      <Button variant="ghost" size="sm" onClick={() => setView(createDataGridViewState(columns))}>Restore starting layout</Button>
    </div>
    <div dir={rtl ? 'rtl' : 'ltr'} style={{ minWidth: 0 }}>
      <DataGrid label="Portable record layout" rows={rows} columns={columns} onEdit={edit} view={view} onViewChange={setView} layoutControls resizableColumns />
    </div>
    <output aria-label="Visible column keys" style={{ overflowWrap: 'anywhere' }}>{view.columnOrderKeys.filter(key => view.visibleColumnKeys.includes(key)).join(', ')}</output>
    <output aria-label="Current grid density">{view.density}</output>
    <p className="bui-hint">These edits stay in this example. A host application validates drafts and saves them separately from the view.</p>
  </div>;
}