Skip to Content
Grist Widgets

Widget options

Persisting per-section settings -- filters, view modes, preferences -- in a Grist widget's own JSON store.

Every Grist custom widget can keep a JSON object with its section: view modes, filters, preferences, prompts. It belongs to the section, so two sections with the same widget keep their own.

Read and write

const w = useGrist()

w.widgetOptions                              // the current object, or null before Grist sends it
await w.patchWidgetOptions({ compact: true }) // merge into it
await w.setWidgetOptions({ compact: true })   // replace it (setWidgetOptions({}) clears it)

widgetOptions follows Grist: after a write, Grist sends the options back and the widget re-renders, with no re-fetch. Writing needs "full" access. In Grist, a widget's write is a draft of the section: its menu offers Save and Revert, and the options are kept once someone saves.

function CompactToggle() {
  const w = useGrist()
  const compact = w.widgetOptions?.compact === true
  return (
    <button onClick={() => void w.patchWidgetOptions({ compact: !compact })}>
      {compact ? "Expand" : "Compact"}
    </button>
  )
}

Typed options

Read them through one function that checks each value, so a missing or older option falls back to a default:

type Settings = { compact: boolean; pageSize: number }

function readSettings(raw: Record<string, unknown> | null): Settings {
  return {
    compact: raw?.compact === true,
    pageSize: typeof raw?.pageSize === "number" ? raw.pageSize : 25,
  }
}

A Settings view

Settings that a person chooses (a default view, a limit) belong in a Settings view. Export a Settings component from the widget's entry module, src/app/App.tsx, next to the widget: only its fields, which write the options.

import { defineGristWidget } from "grist-widget-sdk"

export const widget = defineGristWidget({
  title: "Board",
  requiredAccess: "full",
})

export default function App() {
  const w = widget.useGrist()
  return (
    <header>
      Board
      {/* The widget's own way in; hidden on a read-only document. */}
      {w.readOnly ? null : <button onClick={w.openConfiguration}>Settings</button>}
    </header>
  )
}

export function Settings() {
  const w = widget.useGrist()
  return (
    <label>
      Compact cards
      <input
        type="checkbox"
        checked={w.widgetOptions?.compact === true}
        disabled={w.readOnly || w.optionsIssue !== null}
        onChange={(e) => void w.patchWidgetOptions({ compact: e.target.checked })}
      />
    </label>
  )
}

mountGristWidget() does the rest:

  • Two ways in. Grist's creator panel shows "Open configuration" for the widget (hasCustomOptions defaults to true with Settings), and the widget's own button calls w.openConfiguration(). Both set w.configurationOpen.
  • The view fills the widget's panel, which is often small: a header with "Back to widget" on the left and "Settings" in the middle, then the fields, scrolling. Escape goes back, the first field takes the focus, and the widget stays mounted under the view, so it keeps its state.
  • Grist's Save. Grist keeps what a widget writes to its options as a draft of the section, as it does for sort and filters: only its Save, at the top of the section, keeps it for everyone. While the new options are a draft (w.optionsDraft), the view says to click it, if not done yet. The widget hears its own write and Grist's Revert, but not its Save: the reminder goes with a Revert, or when the view closes, and each opening starts afresh.
  • No Save button of the widget's own: a choice applies at once, and the reminder points at Grist's.
  • Read-only documents: the view says the settings can't be kept; disable the fields with w.readOnly.
  • Options the widget may not write over (written by a newer version, or not migrated: above): the view shows w.optionsIssue; disable the fields while it is set.
  • Settings for now, not for everyone (a zoom, the current tab) stay in the widget's toolbar, in React state: writing them would leave a draft at each click.

In a test, pass the Settings as mountGristWidget() gets them, then click Grist's button with emulator.openConfiguration():

const { emulator } = renderWithGrist(<App />, { widget, Settings })
await emulator.openConfiguration()

smokeTestWidget(entry) opens them too, so Settings that throw fail it. An export named Settings in src/app/App.tsx is the SDK's Settings view's content: a widget that draws its own view gives it another name.

A widget that draws its own configuration instead can still read w.configurationOpen with hasCustomOptions: true: then declare it only when the widget draws something, or Grist shows a button that does nothing.

Options that change shape

A section keeps its options for years, while the widget changes. When a new version reads them differently, declare the version and how to bring older options to it:

import { defineGristWidget, type MigrateWidgetOptions } from "grist-widget-sdk"

// Version 1 kept the board's columns as one string, "To do,Doing,Done".
const migrate: MigrateWidgetOptions = (options, fromVersion) =>
  fromVersion < 2 && typeof options.columns === "string"
    ? { ...options, columns: options.columns.split(",") }
    : options

export const widget = defineGristWidget({
  title: "Board",
  requiredAccess: "full",
  optionsVersion: 2,
  migrate,
})
  • The SDK keeps the version with the options, under __version, a reserved key the widget never sees.
  • Options written under an older version reach widgetOptions migrated, each time they are read. Nothing is written on load.
  • The widget's writes (setWidgetOptions, patchWidgetOptions) carry the current version; a patch merges into the migrated options.
  • Options without a version count as version 1, and may already be in a later shape (an older build wrote them): check the shape in migrate, as above, rather than trust fromVersion alone. A new section starts at the current version, with nothing to migrate.
  • From version 2, migrate is required.

When the options were written by a newer version of the widget, or migrate can't read them, the widget shows them as they are and may not write over them: optionsIssue says why, and the writes reject. setWidgetOptions({}) resets them; so does "Remove" next to Custom options in the section's menu.

{w.optionsIssue ? <p role="alert">{w.optionsIssue}</p> : null}

Test the migration with the previous version's options:

renderWithGrist(<App />, { widget, emulator: { widgetOptions: { columns: "To do,Done" } } })

Pitfalls

  • Don't write on every keystroke. Debounce, or write on blur.
  • Don't store large data here. Grist sends the options with every onOptions event; keep them under a few KB.
  • Not a secrets store. Anyone who can open the document can read them.

On this page