rapid-grid
v0.1.0
Published
Framework-agnostic, two-axis virtualized data grid
Readme
rapid-grid
A zero runtime dependency, framework agnostic, two-axis virtualized data grid for JavaScript and TypeScript. It renders visible rows and columns with a recycled DOM cell pool and works with plain JavaScript, React, Vue, Angular, Svelte, Web Components, and other frameworks through a plain DOM host.
Install
npm install rapid-gridimport { RapidGrid, CellRange, DataType, SelectionMode } from 'rapid-grid';
import 'rapid-grid/style.css';
const grid = new RapidGrid('#grid', {
items: Array.from({ length: 100_000 }, (_, id) => ({ id, name: `Item ${id}`, price: id / 10 })),
columns: [
{ binding: 'id', width: 80, dataType: DataType.Number },
{ binding: 'name', width: 180 },
{ binding: 'price', width: 120, dataType: DataType.Number, format: 'c2' }
],
selectionMode: SelectionMode.CellRange
});
grid.onSelectionChanged((_grid, event) => console.log(event.ranges));
grid.select(new CellRange(1, 1, 4, 2)); // UI rows 2–5, columns 2–3 (zero-based indices)
// Call grid.destroy() when the host component is removed.<div id="grid" style="height: 500px; width: 100%"></div>The host must have a height. The grid adds its own positioned child and leaves other host children intact. Its layout works without the optional CSS import; the stylesheet adds visual styling.
Plain JavaScript
Import the package from an ESM build tool, or load dist/esm/index.js from an installed package path. Use require('rapid-grid') in CommonJS. The grid uses only browser DOM APIs and does not depend on a component framework.
Framework integration
Create the grid after the framework mounts the host element, update it with grid.collectionView.setItems(items), and call grid.destroy() during cleanup. onFormatItem receives the recycled cell element. If a framework mounts components in cells, use onCellRecycling to unmount them when the grid releases that cell.
API notes
rows.get(index).heightandcolumns.get(index).widthprovide variable sizes. Coordinate lookup uses binary search over prefix offsets. Size changes rebuild offsets lazily from the changed index.SelectionMode.CellRangeis the default. Click a cell, then Shift-click, drag, or use Shift+Arrow keys to select a rectangle. Moving Shift+Arrow back toward the anchor shrinks the range.Cellkeeps single-cell selection;MultiRangealso allows Ctrl/Cmd-click to add disjoint ranges.- Ctrl/Cmd+C copies the latest selected rectangle as tab-separated text. Ctrl/Cmd+V pastes at its top-left cell, parsing values according to column types. A single pasted value fills the whole selected rectangle. Read-only columns are skipped.
copySelection()andpaste(text)expose the same behavior to application code;pastereturns the count of updated cells and throws on invalid data before changing any cells. Up to one million cells can be copied or pasted at a time. scrollIntoView,getCell,getCellElement,hitTest,refresh,invalidate,autoSizeRow, andautoSizeColumnare available on the grid.onFormatItem,onCellRecycling,onBeginningEdit,onCellEditEnding,onCellEditEnded,onSelectionChanging,onSelectionChanged,onDraggedRow, andonDraggedColumnreturn unsubscribe functions. Returningfalseor settingevent.cancel = truecancels changing events.allowMergingcontrols body or header merging. Setcolumn.allowMergingfor vertical body merges androw.allowMergingfor horizontal body merges. Header merges coalesce equal labels.allowDraggingenables row and column moves from headers. Row moves mutate the supplied items array throughcollectionView.DataTypesupports string, number, date, time, date-time, date range, and Boolean. String formats includeupperandmask:###-##(#consumes one input character). Number formats includen2,c0, andp1. Date formats includeyyyy,MM,dd,HH,hh,mm, anda. Date range values use{ start: Date, end: Date }. A column can supply a customeditorDOM factory.autoSizeRowsis off by default. When enabled, it measures currently rendered cells on animation frames outside fast scrolling. It can cost extra layout work for complex cell templates.collectionView.sort(binding, direction, dataType)sorts the mutable source array. Callgrid.refresh(true)after mutating item values directly;setValueandsetItemsnotify the grid automatically.
Build and test
npm install
npm test
npm run benchThe benchmark exercises viewport lookup and cell window calculation against 100,000 rows and 1,000 columns and reports its 95th percentile against a 16.7 ms frame budget. It is a computation benchmark; actual browser FPS also depends on viewport size, custom renderers, CSS, and device hardware.
