Skip to main content

Overview

The DataGrid component provides a complete solution for displaying and managing tabular data in Svelte applications. It supports advanced features like column sorting, row selection, responsive layouts, and full customization through the Bond system.

Installation

npm install @svelte-atoms/core

Basic Usage

<script>
  import { DataGrid } from '@svelte-atoms/core';
</script>

<DataGrid.Root>
  <DataGrid.Header>
    <DataGrid.Tr header>
      <DataGrid.Th>Name</DataGrid.Th>
      <DataGrid.Th>Email</DataGrid.Th>
      <DataGrid.Th>Role</DataGrid.Th>
    </DataGrid.Tr>
  </DataGrid.Header>

  <DataGrid.Body>
    {#each users as user}
      <DataGrid.Tr>
        <DataGrid.Td>{user.name}</DataGrid.Td>
        <DataGrid.Td>{user.email}</DataGrid.Td>
        <DataGrid.Td>{user.role}</DataGrid.Td>
      </DataGrid.Tr>
    {/each}
  </DataGrid.Body>
</DataGrid.Root>

Components

DataGrid.Root

The root container for the data grid. Manages the overall state and provides context to child components.
class
string
Additional CSS classes to apply to the root element
template
string
CSS grid template columns definition (e.g., “1fr 2fr 1fr”)
fallbackTemplate
string
default:"auto"
Fallback template when no template is provided
values
string[]
default:"[]"
Array of values (bindable)
factory
Function
Custom factory function for creating the DataGrid bond
children
Snippet
Child content with access to datagrid bond context

DataGrid.Header

Container for the header row(s) of the data grid.
class
string
Additional CSS classes for the header
children
Snippet
Header content, typically DataGrid.Tr with header prop

DataGrid.Body

Container for the body rows of the data grid.
class
string
Additional CSS classes for the body
children
Snippet
Body content, typically multiple DataGrid.Tr components
Container for footer content.
class
string
Additional CSS classes for the footer
children
Snippet
Footer content

DataGrid.Tr

Represents a table row in the data grid.
class
string
Additional CSS classes for the row
value
string
Unique identifier for the row (auto-generated if not provided)
rows
string
default:"auto"
CSS grid rows configuration
data
T
Data associated with this row
header
boolean
default:"false"
Whether this is a header row
onclick
Function
Click handler for the row

DataGrid.Th

Header cell component with sorting support.
class
string
Additional CSS classes for the header cell
id
string
Unique identifier for the column (auto-generated if not provided)
width
string
default:"1fr"
Column width in CSS units
direction
'asc' | 'desc'
default:"asc"
Sort direction for this column
sortable
boolean | SortableType
Enable sorting for this column. Can be true/false or a function to extract sort value
hidden
boolean
default:"false"
Hide this column
screen
string
Screen size breakpoint for responsive visibility
onsort
Function
Callback when column is sorted

DataGrid.Td

Data cell component.
class
string
Additional CSS classes for the cell
onclick
Function
Click handler for the cell
children
Snippet
Cell content

DataGrid.Checkbox

Checkbox component for row selection.
class
string
Additional CSS classes for the checkbox
value
any
Checkbox value
checked
boolean
Checked state (bindable)
onchange
Function
Callback when checkbox state changes

Advanced Examples

With Row Selection

<script>
  import { DataGrid } from '@svelte-atoms/core';

  let users = [
    { id: '1', name: 'John Doe', email: '[email protected]' },
    { id: '2', name: 'Jane Smith', email: '[email protected]' }
  ];
</script>

<DataGrid.Root>
  <DataGrid.Header>
    <DataGrid.Tr header>
      <DataGrid.Th width="auto">
        <DataGrid.Checkbox />
      </DataGrid.Th>
      <DataGrid.Th>Name</DataGrid.Th>
      <DataGrid.Th>Email</DataGrid.Th>
    </DataGrid.Tr>
  </DataGrid.Header>

  <DataGrid.Body>
    {#each users as user (user.id)}
      <DataGrid.Tr value={user.id} data={user}>
        <DataGrid.Td>
          <DataGrid.Checkbox />
        </DataGrid.Td>
        <DataGrid.Td>{user.name}</DataGrid.Td>
        <DataGrid.Td>{user.email}</DataGrid.Td>
      </DataGrid.Tr>
    {/each}
  </DataGrid.Body>
</DataGrid.Root>

With Sortable Columns

<script>
  import { DataGrid } from '@svelte-atoms/core';

  let users = [
    { name: 'John', age: 30 },
    { name: 'Jane', age: 25 },
    { name: 'Bob', age: 35 }
  ];

  function handleSort(event, { field, direction }) {
    // Custom sorting logic
    users = users.sort((a, b) => {
      const aVal = typeof field === 'function' ? field(a) : a[field];
      const bVal = typeof field === 'function' ? field(b) : b[field];
      return direction === 'asc' ? aVal - bVal : bVal - aVal;
    });
  }
</script>

<DataGrid.Root>
  <DataGrid.Header>
    <DataGrid.Tr header>
      <DataGrid.Th sortable="name" onsort={handleSort}>Name</DataGrid.Th>
      <DataGrid.Th sortable="age" onsort={handleSort}>Age</DataGrid.Th>
    </DataGrid.Tr>
  </DataGrid.Header>

  <DataGrid.Body>
    {#each users as user}
      <DataGrid.Tr>
        <DataGrid.Td>{user.name}</DataGrid.Td>
        <DataGrid.Td>{user.age}</DataGrid.Td>
      </DataGrid.Tr>
    {/each}
  </DataGrid.Body>
</DataGrid.Root>

Responsive Columns

<script>
  import { DataGrid } from '@svelte-atoms/core';
  import { container } from '@svelte-atoms/core/runes';

  const gridContainer = container();
</script>

<DataGrid.Root {@attach gridContainer.attach}>
  <DataGrid.Header>
    <DataGrid.Tr header>
      <DataGrid.Th>Name</DataGrid.Th>
      <DataGrid.Th 
        hidden={gridContainer.current?.width < 768}>
        Email
      </DataGrid.Th>
      <DataGrid.Th 
        hidden={gridContainer.current?.width < 1024}>
        Phone
      </DataGrid.Th>
    </DataGrid.Tr>
  </DataGrid.Header>
  <!-- Body content -->
</DataGrid.Root>

Types

Direction

type Direction = 'asc' | 'desc';

SortableType

type SortableType = string | (<T>(d: T) => string | number | boolean | Date);

DatagridContext

interface DatagridContext<T = unknown> extends DataGridBond<T> {
  [key: string]: unknown;
}

Styling

The DataGrid uses CSS Grid for layout and can be fully customized using Tailwind CSS classes or custom CSS.

Default Classes

  • datagrid-root: Root container with CSS Grid display
  • datagrid-tr: Table row with grid layout
  • border-border: Border styling
  • Hover and active states for interactive rows

Custom Styling

<DataGrid.Root class="rounded-lg shadow-lg">
  <DataGrid.Th class="bg-blue-100 font-bold">
    Custom Header
  </DataGrid.Th>
  <DataGrid.Td class="text-gray-600 italic">
    Custom Cell
  </DataGrid.Td>
</DataGrid.Root>

Bond System

The DataGrid component uses the Bond system for state management. You can access the bond instance through the component’s context:
<script>
  let datagridRef;

  $effect(() => {
    if (datagridRef) {
      const bond = datagridRef.getBond();
      // Access bond state and methods
      console.log(bond.state.selectedRows);
    }
  });
</script>

<DataGrid.Root bind:this={datagridRef}>
  <!-- Content -->
</DataGrid.Root>

Accessibility

  • Keyboard navigation support
  • ARIA labels for sortable columns
  • Screen reader friendly row selection
  • Focus management for interactive elements

Build docs developers (and LLMs) love