gridmason home / guides / react

Guide

Integrate Gridmason into a React app

Start from an empty Vite project and finish with a live, drag-to-arrange widget dashboard — the engine embedded, a widget rendering, and edit mode wired to React state.

verified end-to-end @gridmason/core 0.5.x @gridmason/sdk 0.4.x gridstack 12.x React 19 Node ≥ 22

Gridmason's engine (@gridmason/core) is framework-agnostic: the canvas is a custom element, and widgets are custom elements too. "Using it in React" therefore means one thing — your React app is the host that mounts the canvas and hands it a resolved layout and an SDK handle. This guide walks the whole path, and every file below is copied verbatim from a project that was built and run.

What you'll build

A single page rendering two instances of a small acme-greeting widget on the grid canvas, with an Edit layout button that turns on drag & resize and streams geometry changes back into your app:

PrerequisitesBefore you start

Step 1Create a Vite React project

Scaffold a fresh React + TypeScript app and install its base dependencies:

terminal
$ npm create vite@latest gm-dashboard -- --template react-ts
$ cd gm-dashboard
$ npm install

Step 2Install Gridmason

Add the engine, the SDK, and gridstack (the canvas's drag/resize dependency, whose stylesheet you'll import):

terminal
$ npm install @gridmason/core @gridmason/sdk gridstack

Note. You don't install @gridmason/protocol directly — core and sdk pull the contract types in transitively, and this app imports every type it needs from @gridmason/core/engine and @gridmason/sdk. gridstack is a dependency of core, but installing it explicitly makes its CSS import (below) robust regardless of your package manager's hoisting.

Step 3Add the files

You'll create three new files and edit three of the generated ones:

src/
├── widgets/
│ └── greeting-widget.ts # the widget: a custom element
├── gridmason/
│ ├── dashboard.ts # engine wiring: catalog, page type, layout
│ └── CanvasHost.tsx # the React ↔ canvas seam
├── App.tsx # edited — composes it all
├── App.css # edited — app + widget styles
├── index.css # edited — trimmed to a reset
└── main.tsx # edited — one import added

The widget

A Gridmason widget is a framework-agnostic custom element. Before connectedCallback runs, the canvas sets four attributes (settings, context, instance-id, edit-mode) and one property, sdk — the opaque host handle. This one reads its settings for a name, shows the host-issued instance id, and ticks a clock, cleaning up its timer on teardown.

Wire the engine

The headless engine is pure data — no DOM. Register the widget type in a WidgetCatalog, declare a page type, and resolve a starting LayoutDoc into the EffectiveLayout the canvas renders. Here it places two instances of the one widget.

The React ↔ canvas seam

Core ships no React wrapper, and it doesn't need one. <gm-page-canvas> is a custom element whose structured inputs (layout, context, sdk, editMode) are set as properties, so you mount it imperatively through a ref: create it once, assign its inputs, and push editMode when React state changes. It also forwards the canvas's gm:geometry-change event — fired after a user drag/resize settles — back to your app.

Compose the app

App.tsx registers the widget's custom element, builds the engine pieces once, mints a dev/no-op SDK handle with createNoopSDK(), and toggles edit mode. In production you'd swap the no-op for a real HostSDK implementation.

Styles and entry point

Give the canvas host a width, style the widget's light-DOM markup, trim the template CSS to a reset, and import gridstack's stylesheet in main.tsx (without it the grid items won't position).

Step 4Run it

terminal
$ npm run dev  # http://localhost:5173

Open the dev server. You should see:

To make edits stick, feed that geometry back into a new LayoutDoc and re-resolve it — the canvas re-renders when the layout object identity changes. Persisting it (to localStorage or your backend) and reloading it through loadLayout is the natural next increment.

How it fits together

Three packages, three responsibilities:

Next steps

Reference docs: the core quickstart & engine API, the sdk README, and the dashboard's reference widgets.