Data grid / foldkit-data-grid

data-grid

Rendering 100,000 rows into the DOM freezes the browser, and tracking focus, cell ranges, pinned columns, and scroll offsets by measuring DOM nodes breaks whenever rows sort or virtualize out of view. Plain list tables work until users need spreadsheet keyboard movement, inline cell editing, column resizing, or copy-paste across thousands of rows.

foldkit-data-grid computes the grid's geometry, keyboard movement, selection, editing, and virtualized row/column windows as pure projections over rows you supply. Cells are addressed by stable row keys and column ids rather than array indexes, so sorting, filtering, and scrolling 100,000 rows all read one model without touching the DOM. For the accessible HTML rendering layer, pair it with foldkit-mixins-data-grid.

See 100,000 virtualized rows with keyboard movement, pinning, and editing in the live data grid demo.

Install

sh
pnpm add effect foldkit foldkit-data-grid

A first grid of rows

This defines two columns (sku pinned to the start, price in the center), projects two product rows into display order, and computes a cell move and a selection box:

ts
import { ColumnLayout, Columns, GridProjection, RowModel } from 'foldkit-data-grid'

interface Product {
  readonly id: string
  readonly sku: string
  readonly price: number
}
const columns = Columns.define<Product>()({
  sku: { header: 'SKU', value: product => product.sku, pinned: 'start' },
  price: { header: 'Price', value: product => product.price },
})
const products: ReadonlyArray<Product> = [
  { id: 'p1', sku: 'A-1', price: 9 },
  { id: 'p2', sku: 'B-2', price: 2 },
]
const productKey = (product: Product) => product.id

const grid = GridProjection.make({
  rows: RowModel.fromArray(products, productKey),
  columns,
  layout: ColumnLayout.initial(columns),
})

const moved = grid.moveBy({ row: 'p1', column: 'sku' }, { rows: 1, columns: 1 })
const span = grid.box({ row: 'p2', column: 'price' }, { row: 'p1', column: 'sku' })

grid.columns is ['sku', 'price'], moved is Option.some({ row: 'p2', column: 'price' }), and span is Option.some({ rows: { start: 0, end: 2 }, columns: ['sku', 'price'] }).

  • Columns.define<Product>()({...}) declares typed columns by stable id. The curried call supplies the row type while inferring literal column ids, and throws on numeric keys or __proto__.
  • RowModel.fromArray(rows, key) wraps an in-memory array and indexes row keys once per (rows, key) pair (throwing on duplicate keys). Paged or cursor sources implement the RowModel interface directly; use RowModel.map(rows, input, make) to layer local edits over server rows.
  • ColumnLayout.initial(columns) captures column order, pinning, and visibility as plain serializable data of column ids.
  • GridProjection.make(...) combines rows, columns, and layout into one pure display projection; rebuild it whenever any input changes.