Skip to Content
Grist Widgets

Core concepts

The handful of Grist ideas -- sections, records, mapping, values -- every widget author needs.

A short mental model for the parts of Grist your widget interacts with, and how the SDK reflects them.

The widget lives in an iframe

A Custom Widget is an iframe embedded by the Grist host. Communication happens through grist-plugin-api.js, which exposes a global grist object and a request/event channel back to the document.

┌─────────────────────────────┐
│ Grist host document         │
│  ┌───────────────────────┐  │
│  │ widget iframe         │  │
│  │  grist-plugin-api.js  │  │
│  │  grist-widget-sdk     │  │
│  │  your React widget    │  │
│  └───────────────────────┘  │
└─────────────────────────────┘

The SDK shields you from the iframe lifecycle, the grist.ready handshake, and the event streams (How the SDK works).

Sections, selection, and modes

A Custom Widget is attached to a section of a Grist page. The section has:

  • A current table (w.currentTableId).
  • A cursor row — what grist.onRecord reports as w.record.
  • A visible row set — what grist.onRecords reports as w.records.
  • A new-row placeholder — when the user clicks the blank trailing row, grist.onNewRecord fires.

The SDK derives a single w.mode for you:

type GristWidgetMode = "empty" | "row" | "new-row"
modemeaning
emptyNo row selected (or section is empty).
rowA real row is selected. w.record is non-null.
new-rowThe user is on the blank "create" row.

Render branches against mode instead of combining record == null with other flags yourself.

Column mapping

Widgets declare logical column names (the names your code uses). The user maps them to real columns in the section configuration UI. This means a widget that wants Title and Done works on any table that has any two columns the user is willing to map.

The SDK exposes:

  • w.mappings — { logicalName → realColId }, as Grist reports it.
  • w.mappedRecord — w.record rewritten under logical names, or null when required mappings are missing.
  • w.columnMappingStatus — { ok, pending?, missing, emptyMultiples } to gate UI.
  • w.mapBack(patch) — reverse-map a logical patch to a real-column patch ready for writes.

Once a widget declares columns and the user maps them, Grist sends it only the mapped columns: w.record and w.records hold those and the row id, nothing else. A widget that also reads other columns (a template naming any of them, say) declares no columns, or reads the table itself with w.fetchRows(tableId), which needs full access. The emulator does the same.

See Declare and map columns.

Decoded vs encoded

Grist sometimes sends cell values as marshalled tuples: dates as ["D", epochSeconds, tz], errors as ["E", ...], references as ["R", tableId, rowId], lists as ["L", ...].

The SDK pins decoding on:

  • w.record / w.records (event streams) — decoded.
  • w.fetchRows() — the section's decoded rows.
  • w.fetchRows(tableId, { columns }) — another table's rows, decoded with its column metadata.

A reference arrives in w.record and w.records as the text its column shows (a person's name, not their row id), and a RefList as the list of those. A widget that declares a column as a reference (Ref:People, with no Text alternative) gets the row ids instead, in every reference: see Typed reads. w.fetchRows(tableId) always keeps the ids, and needs full access.

w.fetchRows(tableId) without columns gives another table's cells as Grist sends them. Use decodeGristValue(value, column) to decode one, or pass columns. The mapped records go further: each cell is checked against its declared type (Typed reads).

Reads vs writes

PathUse for
w.record / w.records (subscriptions)Reactive selection — re-renders when Grist updates.
w.fetchRows()One-off read of the current section.
w.fetchRows(tableId)One-off read of any table by id (full access).
w.table.* / w.getTable(id).*All record CRUD.
w.applyActions([...])Schema mutations (add/rename/remove tables, columns) or operations needing custom undo descriptions.

How the SDK fits in

The declaration, the one grist.ready() call, the provider and boundary that mountGristWidget() renders, and why tests run the same code: see How the SDK works.

On this page