How to Enable Keyboard Navigation

This guide shows how to navigate the grid with keyboard shortcuts.

Enable keyboard navigation

Keyboard navigation is enabled automatically when selectable is set:

<DataGrid
  {data}
  {columns}
  selectable
/>

Click inside the grid body to give it focus, then use arrow keys to navigate.

Navigation shortcuts

Row navigation

KeyAction
Arrow DownMove to next row
Arrow UpMove to previous row
Page DownMove down one page
Page UpMove up one page
Home or Ctrl+HomeMove to first row
End or Ctrl+EndMove to last row

Selection shortcuts

KeyAction
SpaceToggle selection on focused row
EnterSelect focused row
Ctrl/Cmd+ASelect all rows (multi-select mode)
EscapeClear all selection

Range selection (multi-select mode)

KeyAction
Shift+Arrow DownExtend selection down
Shift+Arrow UpExtend selection up
Shift+Page DownExtend selection one page down
Shift+Page UpExtend selection one page up
Shift+HomeExtend selection to first row
Shift+EndExtend selection to last row

Programmatic navigation

Access grid state to navigate programmatically:

<script>
  import { DataGrid } from 'svelte-datagrid';

  let gridState;

  function goToFirst() {
    gridState.navigateToFirst();
  }

  function goToLast() {
    gridState.navigateToLast();
  }

  function goToRow(index) {
    gridState.navigateRow(index);
  }
</script>

<button onclick={goToFirst}>First</button>
<button onclick={goToLast}>Last</button>
<button onclick={() => goToRow(50)}>Go to row 50</button>

<DataGrid
  {data}
  {columns}
  selectable
  bind:gridState
/>

Navigation API

MethodDescription
navigateRow(index)Navigate to specific row
navigateToFirst()Navigate to first row
navigateToLast()Navigate to last row
navigateByPage(direction)Navigate one page up/down

Focus management

The grid body receives focus when clicked. You can also focus it programmatically:

<script>
  let gridElement;

  function focusGrid() {
    gridElement.querySelector('[data-testid="datagrid-body"]')?.focus();
  }
</script>

<button onclick={focusGrid}>Focus Grid</button>

<div bind:this={gridElement}>
  <DataGrid {data} {columns} selectable />
</div>

Accessibility considerations

The grid implements the ARIA grid pattern:

  • role="grid" on container
  • role="rowgroup" on body
  • role="row" on each row
  • role="gridcell" on each cell
  • aria-rowindex for each row
  • tabindex="0" on body for focus

Screen readers announce:

  • Current row position
  • Selected state
  • Cell contents

Combine with selection events

Track navigation alongside selection:

<script>
  let currentRow = $state(null);
  let selectedRows = $state(new Set());

  function handleSelection(event) {
    selectedRows = event.selected;
    // Current row is the last selected
    currentRow = event.added.length > 0 ? event.added[event.added.length - 1] : null;
  }
</script>

<DataGrid
  {data}
  {columns}
  selectable="multiple"
  getRowId={(row) => row.id}
  onselectionchange={handleSelection}
/>

<p>Current: {currentRow}, Selected: {selectedRows.size}</p>

Complete example

<script lang="ts">
  import { DataGrid } from 'svelte-datagrid';

  const data = Array.from({ length: 100 }, (_, i) => ({
    id: i + 1,
    name: `Row ${i + 1}`,
    value: Math.floor(Math.random() * 1000)
  }));

  const columns = [
    { key: 'id', header: 'ID', width: 60 },
    { key: 'name', header: 'Name', width: 150 },
    { key: 'value', header: 'Value', width: 100 }
  ];

  let gridState;
  let selectedIds = $state(new Set());

  function handleSelection(event) {
    selectedIds = event.selected;
  }
</script>

<div style="margin-bottom: 8px;">
  <button onclick={() => gridState?.navigateToFirst()}>First</button>
  <button onclick={() => gridState?.navigateByPage(-1)}>Page Up</button>
  <button onclick={() => gridState?.navigateByPage(1)}>Page Down</button>
  <button onclick={() => gridState?.navigateToLast()}>Last</button>
  <span style="margin-left: 16px;">Selected: {selectedIds.size}</span>
</div>

<div style="height: 400px;">
  <DataGrid
    {data}
    {columns}
    selectable="multiple"
    getRowId={(row) => row.id}
    onselectionchange={handleSelection}
    bind:gridState
  />
</div>

<p style="color: #666; font-size: 0.875rem;">
  Click grid, then use Arrow keys, Page Up/Down, Home/End to navigate.
  Space to toggle selection. Shift+arrows to extend selection.
</p>

See also