Positron data grid pattern

From posit-dev/positron · View interactive version

Positron data grid pattern is an AI agent skill from the GitHub repository posit-dev/positron. Use when building a list, table, grid, or virtualized scrolling UI in Positron, or touching `DataGridInstance` / `PositronDataGrid`. Avoids the common mistake of mediating props into the instance via useEffect.

How to use this skill

Skill file

Positron Data Grid Pattern

Positron has a virtualized grid system built around an abstract DataGridInstance and a renderer component <PositronDataGrid />. The pattern is non-obvious from outside, and the wrong instinct is to build a React wrapper component that takes props and pushes them into the instance via useEffect. Don't. This skill exists so you don't make that mistake.

The core insight

A DataGridInstance subclass IS the data grid.

The instance owns:

<PositronDataGrid instance={...} /> is a thin React renderer over any DataGridInstance subclass. It does not own state. It just observes the instance and paints.

The pattern

The shared shape, regardless of data strategy:

// 1. Pick or build a DataGridInstance subclass.
//    - Embed strategy: subclass PositronListInstance, or DataGridInstance directly.
//    - fetchData strategy: subclass DataGridInstance and override fetchData().

// 2. The caller creates exactly one instance for the component's lifetime.
const [instance] = useState(() => new MySubclass({ /* options */ }));

// 3. The caller subscribes to instance events as needed.
useEffect(() => {
    const d = instance.onDidSomething(payload => /* ... */);
    return () => d.dispose();
}, [instance]);

// 4. Dispose on unmount.
useEffect(() => () => instance.dispose(), [instance]);

// 5. Render the grid.
return <PositronDataGrid instance={instance} />;

How data gets into the instance depends on the strategy:

```tsx

useEffect(() => {

instance.setItems(items);

}, [instance, items]);

```

This setter is specific to PositronListInstance; other embed subclasses can expose whatever shape they want.

Either way: no React wrapper component sits between the caller and the instance. The instance is the API surface.

The anti-pattern (do NOT do this)

Don't build <MyList items={...} renderItem={...} onActivate={...} /> as a React component that internally:

Symptoms that you're heading the wrong way:

If you see those, delete the wrapper. Let the caller drive the instance directly.

Two data strategies

A DataGridInstance subclass can manage data in one of two ways. Pick based on dataset size.

Strategy A: embed the data in memory

For small-to-medium lists where the entire dataset fits comfortably in memory.

Example: PositronListInstance.

Strategy B: implement `fetchData` for lazy/virtualized data

For huge datasets where holding everything in memory is infeasible. The base class calls fetchData(rowStartIndex, rowCount, columnStartIndex, columnCount) (or similar) when it needs the cells for the visible viewport.

Examples: TableDataDataGridInstance, TableSummaryDataGridInstance, InlineTableDataGridInstance, ColumnSelectorDataGridInstance.

You generally know which you need before writing a line: is the dataset bounded and small? Embed. Is it backed by a query/file/comm and possibly enormous? Implement fetchData.

Customizing grid behavior

The subclass passes options to super(...) in its constructor - column/row headers, scrollbars (and overscroll), pinning, selection, resizing, automatic layout, etc. The same <PositronDataGrid /> renderer adapts to all of these via the instance's configuration.

If you need a behavior the base class doesn't expose, the path is usually:

  1. Add the option to DataGridInstance's options type and constructor.
  2. Plumb it down to whatever subsystem needs it (LayoutManager, scroll, etc.).
  3. Update existing subclasses if their defaults change.

This is invasive - many subclasses share the base - so weigh the cost before going there.

Existing subclasses to read for reference

Skim two before writing a new one.

Quick checklist when adding a new grid-style UI

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.