šŸ“ Sign Up | šŸ” Log In

← Root | ↑ Up

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ šŸ“„ shadcn/directory/sadmann7/diceui/components/data-grid │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

╔══════════════════════════════════════════════════════════════════════════════════════════════╗
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘

title: Data Grid description: A high-performance editable data grid component with virtualization, keyboard navigation, and cell editing capabilities. preview: true links: doc: https://tanstack.com/table/latest/docs/introduction api: /docs/components/data-grid#api-reference

<ComponentTabs name="data-grid-demo" />

Installation

<Steps> <Step> Install the main component and dependencies:
```package-install
npx shadcn@2.4.0-canary.12 add "https://diceui.com/r/data-grid"
```
</Step> <Step> Install the [`DataGridSortMenu`](#datagridsortmenu) (optional):
```package-install
npx shadcn@2.4.0-canary.12 add "https://diceui.com/r/data-grid-sort-menu"
```
</Step> <Step> Install the [`DataGridRowHeightMenu`](#datagridrowheightmenu) (optional):
```package-install
npx shadcn@2.4.0-canary.12 add "https://diceui.com/r/data-grid-row-height-menu"
```
</Step> <Step> Install the [`DataGridViewMenu`](#datagridviewmenu) (optional):
```package-install
npx shadcn@2.4.0-canary.12 add "https://diceui.com/r/data-grid-view-menu"
```
</Step> <Step> Install the [`DataGridKeyboardShortcuts`](#datagridkeyboardshortcuts) (optional):
```package-install
npx shadcn@2.4.0-canary.12 add "https://diceui.com/r/data-grid-keyboard-shortcuts"
```
</Step> </Steps>

Features

The Data Grid component provides a comprehensive spreadsheet-like experience with:

  • High Performance: Virtualized rows and columns for handling large datasets
  • Cell Editing: In-place editing with various cell types (text, number, select, date, etc.)
  • Cell Selection: Single and multi-cell selection
  • Cell Copying: Copy selected cells to clipboard
  • Keyboard Navigation: Full keyboard support with Excel-like shortcuts
  • Context Menu: Right-click actions for rows and cells
  • Sorting: Multi-column sorting with drag-and-drop reordering and ascending/descending controls
  • Search: Find and navigate to matching cells with keyboard shortcuts
  • Row Management: Add, delete, and reorder rows
  • Column Resizing: Adjustable column widths

Layout

Import the components and compose them together:

import { DataGrid } from "@/components/data-grid/data-grid";
import { DataGridSortMenu } from "@/components/data-grid/data-grid-sort-menu";
import { DataGridRowHeightMenu } from "@/components/data-grid/data-grid-row-height-menu";
import { DataGridViewMenu } from "@/components/data-grid/data-grid-view-menu";
import { DataGridKeyboardShortcuts } from "@/components/data-grid/data-grid-keyboard-shortcuts";
import { useDataGrid } from "@/hooks/use-data-grid";

const { table, ...dataGridProps } = useDataGrid({
  data,
  columns,
  onDataChange: setData,
});

<div className="flex flex-col gap-4">

  {/* Toolbar with menu components */}
  <div className="flex items-center gap-2 self-end">
    <DataGridSortMenu table={table} />
    <DataGridRowHeightMenu table={table} />
    <DataGridViewMenu table={table} />
  </div>

  {/* Keyboard shortcuts dialog (opens with Ctrl+/) */}
  <DataGridKeyboardShortcuts enableSearch={!!dataGridProps.searchState} />

  {/* Data grid */}
  <DataGrid table={table} {...dataGridProps} />
</div>

Cell Architecture

The Data Grid uses a three-layer cell composition pattern:

  1. DataGridCell: Routes to the appropriate cell variant based on the column's meta.cell.variant property
  2. Cell Variants: Implement specific editing UIs for different data types (text, number, select, etc.)
  3. DataGridCellWrapper: Provides common functionality for all cells (focus, selection, keyboard interactions)
// Cell composition flow
<DataGridCell cell={cell} table={table} />
  ↓
<ShortTextCell {...props} />  // Based on variant
  ↓
<DataGridCellWrapper {...props}>
  {/* Cell-specific content */}
</DataGridCellWrapper>

Each cell variant receives the same props and wraps its content in DataGridCellWrapper, which provides:

  • Focus management and visual focus ring
  • Selection state and highlighting
  • Search match highlighting
  • Click, double-click, and keyboard event management
  • Edit mode triggering (Enter, F2, Space, or typing)

Cell Types

The Data Grid supports various cell types for different data formats:

Short Text Cell

{
  id: "name",
  accessorKey: "name",
  header: "Name",
  meta: {
    label: "Name",
    cell: {
      variant: "short-text",
    },
  },
}

Long Text Cell

{
  id: "notes",
  accessorKey: "notes",
  header: "Notes",
  meta: {
    label: "Notes",
    cell: {
      variant: "long-text",
    },
  },
}

Number Cell

{
  id: "price",
  accessorKey: "price",
  header: "Price",
  meta: {
    label: "Price",
    cell: {
      variant: "number",
      min: 0,
      step: 0.01,
    },
  },
}

Select Cell

{
  id: "category",
  accessorKey: "category",
  header: "Category",
  meta: {
    label: "Category",
    cell: {
      variant: "select",
      options: [
        { label: "Electronics", value: "electronics" },
        { label: "Clothing", value: "clothing" },
        { label: "Books", value: "books" },
      ],
    },
  },
}

Multi-Select Cell

{
  id: "skills",
  accessorKey: "skills",
  header: "Skills",
  meta: {
    label: "Skills",
    cell: {
      variant: "multi-select",
      options: [
        { label: "JavaScript", value: "javascript" },
        { label: "TypeScript", value: "typescript" },
        { label: "React", value: "react" },
      ],
    },
  },
}

Date Cell

{
  id: "startDate",
  accessorKey: "startDate",
  header: "Start Date",
  meta: {
    label: "Start Date",
    cell: {
      variant: "date",
    },
  },
}

Checkbox Cell

{
  id: "isActive",
  accessorKey: "isActive",
  header: "Active",
  meta: {
    label: "Active",
    cell: {
      variant: "checkbox",
    },
  },
}

Usage

With Toolbar and Keyboard Shortcuts

import { DataGrid } from "@/components/data-grid/data-grid";
import { DataGridKeyboardShortcuts } from "@/components/data-grid/data-grid-keyboard-shortcuts";
import { DataGridSortMenu } from "@/components/data-grid/data-grid-sort-menu";
import { DataGridRowHeightMenu } from "@/components/data-grid/data-grid-row-height-menu";
import { DataGridViewMenu } from "@/components/data-grid/data-grid-view-menu";
import { useDataGrid } from "@/hooks/use-data-grid";

export default function MyDataGrid() {
  const { table, ...dataGridProps } = useDataGrid({
    data,
    columns,
    enableSearch: true,
  });

  return (
    <div className="flex flex-col gap-4">
      {/* Toolbar with menu components */}
      <div className="flex items-center gap-2 self-end">
        <DataGridSortMenu table={table} />
        <DataGridRowHeightMenu table={table} />
        <DataGridViewMenu table={table} />
      </div>
      
      {/* Keyboard shortcuts dialog (opens with Ctrl+/) */}
      <DataGridKeyboardShortcuts enableSearch={!!dataGridProps.searchState} />
      
      {/* Data grid */}
      <DataGrid table={table} {...dataGridProps} />
    </div>
  );
}

Row Selection

Add a selection column to enable row selection:

import { Checkbox } from "@/components/ui/checkbox";

const columns = [
  {
    id: "select",
    header: ({ table }) => (
      <Checkbox
        checked={
          table.getIsAllPageRowsSelected() ||
          (table.getIsSomePageRowsSelected() && "indeterminate")
        }
        onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
      />
    ),
    cell: ({ row }) => (
      <Checkbox
        checked={row.getIsSelected()}
        onCheckedChange={(value) => row.toggleSelected(!!value)}
      />
    ),
    size: 40,
    enableSorting: false,
    enableHiding: false,
  },
  // ... other columns
];

Row Management

Add and delete rows with callbacks:

const onRowAdd = React.useCallback(() => {
  const newRow = {
    id: `${Date.now()}`,
    // ... other default values
  };
  setData(prev => [...prev, newRow]);
  
  return {
    rowIndex: data.length,
    columnId: "name", // Focus this column after adding
  };
}, [data.length]);

const onRowsDelete = React.useCallback((rows) => {
  setData(prev => prev.filter(row => !rows.includes(row)));
}, []);

const { table, ...dataGridProps } = useDataGrid({
  data,
  columns,
  onRowAdd,
  onRowsDelete,
});

Context Menu Actions

Right-click on cells to access context menu options:

  • Copy: Copy selected cells to clipboard
  • Clear: Clear content from selected cells
  • Delete rows: Remove selected rows (only available when onRowsDelete is provided)

API Reference

useDataGrid

Hook for initializing the data grid with state management and editing capabilities.

<AutoTypeTable path="./types/docs/data-grid.ts" name="UseDataGridProps" />

DataGrid

Main data grid with virtualization and editing capabilities.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridProps" />

DataGridColumnHeader

Column header with sorting controls and visual indicators for sort direction.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridColumnHeaderProps" />

DataGridCell

Routes to the appropriate cell variant based on the column's meta.cell.variant property.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridCellProps" />

DataGridCellWrapper

Base wrapper providing common functionality for all cell variants including focus management, selection state, search highlighting, and keyboard interactions.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridCellWrapperProps" />

DataGridCellVariants

Individual cell variants for different data types. Each variant implements the DataGridCellVariantProps interface and wraps its content in DataGridCellWrapper.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridCellVariantProps" />

Available cell variants:

  • ShortTextCell: Single-line text input with inline contentEditable
  • LongTextCell: Multi-line textarea displayed in a popover dialog
  • NumberCell: Numeric input with optional min, max, and step constraints
  • SelectCell: Single-select dropdown with predefined options
  • MultiSelectCell: Multi-select input with badge display and command palette
  • CheckboxCell: Boolean checkbox for true/false values
  • DateCell: Date picker with calendar popover

Creating Custom Cell Variants

You can create custom cell variants by implementing the DataGridCellVariantProps interface and wrapping your content in DataGridCellWrapper:

import { DataGridCellWrapper } from "@/components/data-grid/data-grid-cell-wrapper";
import type { DataGridCellVariantProps } from "@/types/docs/data-grid";

export function CustomCell<TData>({
  cell,
  table,
  rowIndex,
  columnId,
  isFocused,
  isEditing,
  isSelected,
}: DataGridCellVariantProps<TData>) {
  const value = cell.getValue() as YourType;
  
  return (
    <DataGridCellWrapper
      cell={cell}
      table={table}
      rowIndex={rowIndex}
      columnId={columnId}
      isEditing={isEditing}
      isFocused={isFocused}
      isSelected={isSelected}
    >
      {/* Your custom cell content */}
    </DataGridCellWrapper>
  );
}

DataGridRow

Individual row with virtualization for large datasets.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridRowProps" />

DataGridSearch

Search dialog with keyboard shortcuts for finding and navigating between matching cells in the grid.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridSearchProps" />

DataGridContextMenu

Right-click context menu for quick access to common cell and row actions like copy, clear, and delete.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridContextMenuProps" />

DataGridSortMenu

Menu for managing multi-column sorting with drag-and-drop reordering and ascending/descending controls.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridSortMenuProps" />

DataGridRowHeightMenu

Menu for adjusting row heights between short, medium, tall, and extra-tall options with persistent preferences.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridRowHeightMenuProps" />

DataGridViewMenu

Menu for controlling column visibility with search and toggle all functionality.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridViewMenuProps" />

DataGridKeyboardShortcuts

Searchable reference dialog for all available keyboard shortcuts for navigating and interacting with the data grid.

<AutoTypeTable path="./types/docs/data-grid.ts" name="DataGridKeyboardShortcutsProps" />

Accessibility

The Data Grid follows WAI-ARIA guidelines for grid widgets:

  • Full keyboard navigation support
  • Screen reader announcements for cell changes
  • Focus management during editing
  • Proper ARIA labels and roles
  • High contrast mode support

Keyboard Interactions

<KeyboardShortcutsTable shortcuts={[ { keys: ["↑", "↓", "←", "→"], description: "Navigate between cells", }, { keys: ["Tab"], description: "Move to next cell", }, { keys: ["Shift", "Tab"], description: "Move to previous cell", }, { keys: ["Home"], description: "Move to first column", }, { keys: ["End"], description: "Move to last column", }, { keys: ["Ctrl + Home", "Cmd + Home"], description: "Move to first cell", }, { keys: ["Ctrl + End", "Cmd + End"], description: "Move to last cell", }, { keys: ["PgUp"], description: "Move up one page", }, { keys: ["PgDn"], description: "Move down one page", }, { keys: ["Shift + ↑", "Shift + ↓", "Shift + ←", "Shift + →"], description: "Extend selection", }, { keys: ["Ctrl + A", "Cmd + A"], description: "Select all cells", }, { keys: ["Ctrl + Click", "Cmd + Click"], description: "Toggle cell selection", }, { keys: ["Shift + Click"], description: "Select range", }, { keys: ["Escape"], description: "Clear selection or exit edit mode", }, { keys: ["Enter"], description: "Start editing cell", }, { keys: ["Double Click"], description: "Start editing cell", }, { keys: ["Delete"], description: "Clear selected cells", }, { keys: ["Backspace"], description: "Clear selected cells", }, { keys: ["Ctrl + F", "Cmd + F"], description: "Open search", }, { keys: ["Ctrl + Shift + S", "Cmd + Shift + S"], description: "Toggle the sort menu", }, { keys: ["Ctrl + /", "Cmd + /"], description: "Show keyboard shortcuts", }, ]} />

Credits

ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

← Root | ↑ Up