Skip to Content
Grist Widgets

`<GristWidgetProvider>` and `<GristBoundary>`

The two components at a widget's root: the connection to Grist, and what shows until the widget can run.

mountGristWidget() renders both around your App:

<GristWidgetProvider options={widget.options}>
  <GristBoundary>
    <App />
  </GristBoundary>
</GristWidgetProvider>

Render them yourself only for a page that manages its own React root. One provider per widget; nesting providers is unsupported.

import { createRoot } from "react-dom/client"
import { GristBoundary, GristWidgetProvider } from "grist-widget-sdk"

function Widget() {
  return <p>Connected, with the access it asked for.</p>
}

createRoot(document.getElementById("root")!).render(
  <GristWidgetProvider options={{ requiredAccess: "read table" }}>
    <GristBoundary>
      <Widget />
    </GristBoundary>
  </GristWidgetProvider>
)

<GristWidgetProvider>

type GristWidgetProviderProps = {
  options?: UseGristOptions // read once, at mount
  children: React.ReactNode
}

Connects to Grist for the components below it; useGrist() under it reads this connection. The connection is synchronous: the provider checks that the page is in Grist's iframe and that window.grist (the plugin API) is loaded, calls grist.ready(options) once, and listens to Grist's records, options, mappings and theme. A page that loads the plugin API asynchronously gets one more look once the page has loaded. There is no polling and no heartbeat: the widget's iframe lives and dies with the Grist page, and a call that fails fails where it was made.

UseGristOptions

type UseGristOptions = {
  requiredAccess?: "none" | "read table" | "full" // default "read table"
  columns?: Array<string | GristColumnToMap>      // the columns the user maps in Grist
  allowSelectBy?: boolean                         // other sections can link to this widget
  requiredLinking?: "target" | "source"           // the link it can't work without, for its alerts
  hasCustomOptions?: boolean                      // Grist shows "Open configuration", which opens configurationOpen
  onEditOptions?: () => void | Promise<void>      // ...which calls this
  suppressAlerts?: GristSdkAlertKind[]            // alerts mountGristWidget() never shows
  optionsVersion?: number                         // the version of the widget options
  migrate?: MigrateWidgetOptions                  // brings older options to it, on read
}

type GristColumnToMap = {
  name: string           // the name widget code uses
  title?: string | null  // label in Grist's mapping panel
  description?: string | null
  type?: string          // allowed Grist types, comma-separated: "Text", "Numeric,Int", "Ref:Tasks"...
  optional?: boolean
  allowMultiple?: boolean
  strictType?: boolean
}

<GristBoundary>

type GristBoundaryProps = { children: React.ReactNode }

Renders its children once Grist is connected, has granted the access the widget declares, and every required declared column is mapped. Until then it shows a screen that says what is missing:

StateScreenRetry
Connecting, or mappings not reported yet"Connecting to Grist"—
Not in Grist's iframe, or no plugin API"Open inside Grist"yes
grist.ready() failed or timed out (30 s)"Something went wrong", with the erroryes
Grist granted less access than declared"More access needed", with the level to setyes
A required column isn't mapped"Map your columns"—

Retry calls useGrist().reload(). The boundary needs a provider above it.

Under mountGristWidget(), the screens speak the widget's language: see In the widget's language. A boundary under a provider of your own speaks English.

On this page