Embedding

Embedding

Mount the studio's components, the workflow canvas first, in your own React app; what is exported, what the host provides, and a working example from this site.

The studio app is built from components that @sendwend/studio also exports, and the CLI composes exactly these: nothing is held back for it. Routes fetch and components render, so the components carry no router and no query client. A host app passes data in as props and keeps ownership of its URLs and data fetching.

The most useful one is WorkflowCanvas: a read-only diagram of a workflow drawn from its compiled IR. This site uses it for every example in the workflow gallery, so a page always shows what the code compiles to rather than a hand-drawn picture of it.

What is exported

tsx
import {
  WorkflowCanvas,
  TemplatesPage,
  WorkflowList,
  MetricCard,
} from '@sendwend/studio';
ExportWhat it renders
WorkflowCanvasA workflow's IR as a pan-and-zoom canvas, with optional per-node badges and selection
WorkflowListThe workflows table: shape, funnel columns and sparklines
TemplatesPageA template preview with its envelope, toolbar and optional props editor
PushPreviewA push notification on iOS and Android lock screens
MetricCardA headline number, a period-over-period delta and a chart of one or more series
SparklineA small line of recent values
DateRangePickerThe range picker the metrics view uses, with lastDays, isoDay, parseIsoDay, formatDay
Avatar, ButtonSmall building blocks
CHANNEL_ICON, ALL_CHANNELS_ICON, channelIconThe icons per metric channel
collectTemplateRefs, collectTemplatesEvery template a set of IRs references, and where
layout, CARD_SIZE, ICON_SIZEThe pure IR-to-nodes-and-edges layout the canvas draws
superellipseThe corner-shape style the studio's cards use

The root export also re-exports everything in @sendwend/studio/config: defineStats and the stats types (WorkflowReport, NodeStats, MetricPoint, …), so data you fetch yourself can be typed to what the components expect.

WorkflowCanvas

ts
interface WorkflowCanvasProps {
  ir: WorkflowIR;
  /** Node id → users currently waiting there; no badge without it. */
  stats?: NodeStats;
  /** react-flow's own chrome (background dots, zoom controls). */
  colorMode?: 'light' | 'dark';
  /** Node id to show as selected; pairs with onSelectNode for an inspector panel. */
  selectedId?: string | undefined;
  /** Node clicked, or undefined when the click landed on empty canvas. */
  onSelectNode?: (id: string | undefined) => void;
}

ir is a compiled workflow: workflow(...).toIR(), or one entry of compileConfig(config).workflows. It is plain data, so you can compile on the server and send it to the browser; the canvas never needs your workflow code.

The canvas fills its parent, so give the parent a height. Nodes cannot be dragged. colorMode only affects what react-flow paints itself; the cards and connectors read their colors from CSS variables (below), and follow your theme through them. It is passed in rather than read from the DOM so the host stays in control.

What the host provides

react-flow's stylesheet. The canvas is built on @xyflow/react (a dependency of the studio) and needs its base CSS. Import it once in your global stylesheet:

css
@import '@xyflow/react/dist/style.css';

The color variables. Cards, connectors, the grid and icons are styled inline from these custom properties. The studio defines them for its own app; an embedding host defines them itself:

VariableUsed for
--color-canvasThe canvas background
--color-cardNode card background
--color-edgeCard borders and connectors
--color-gridThe background dots
--color-primary, --color-secondary, --color-mutedCard titles, labels, secondary text
--color-accent, --color-accent-ringThe selected node
--color-icon-trigger, --color-icon-message, --color-icon-delay, --color-icon-control, --color-icon-data, --color-icon-exitIcon colors per node category

The studio's own theme also defines --color-selected and --color-card-shadow, which other components use; defining them too costs nothing.

A font, optionally. Cards ask for 'Inter Variable', Inter, then fall back to the system UI font.

A working example: this site

The canvas on this site's workflow pages is a small wrapper, apps/web/src/components/workflow-canvas. It makes three choices worth copying.

Load it lazily, in the browser only. react-flow measures the DOM, so there is nothing useful to render on the server. Keeping the import behind lazy() also keeps the studio out of the server bundle (here, a Cloudflare Worker) and its startup time. useSyncExternalStore with a server snapshot of false renders nothing on the server and on the hydration pass, then the canvas once mounted:

tsx
import { useTheme } from '@repo/ui'; // the site's own theme hook
import { cn } from 'cn';
import { Suspense, lazy, useSyncExternalStore } from 'react';
import type { WorkflowIR } from 'sendwend';

const Canvas = lazy(async () => {
  const { WorkflowCanvas: component } = await import('@sendwend/studio');
  return { default: component };
});

const subscribe = () => () => {};

export default function WorkflowCanvas({
  ir,
  className,
}: {
  ir: WorkflowIR;
  className?: string;
}) {
  const { theme } = useTheme();
  const mounted = useSyncExternalStore(
    subscribe,
    () => true,
    () => false
  );

  return (
    <div className={cn('workflow-canvas overflow-hidden', className)}>
      {mounted && (
        <Suspense fallback={null}>
          <Canvas ir={ir} colorMode={theme === 'dark' ? 'dark' : 'light'} />
        </Suspense>
      )}
    </div>
  );
}

A page then renders <WorkflowCanvas ir={ir} className="h-96" />, with the IR compiled on the server by compileConfig.

Scope the variables to a wrapper. Two of the names, --color-card and --color-edge, already mean something else in this site's theme. Defining the canvas's values on a .workflow-canvas class instead of :root keeps both meanings intact. The values map onto the site's palette, with a dark set under the site's .dark class:

css
@import '@xyflow/react/dist/style.css';

.workflow-canvas {
  --color-muted: var(--color-gray-500);
  --color-canvas: var(--color-gray-50);
  --color-card: white;
  --color-edge: var(--color-gray-300);
  --color-grid: var(--color-gray-400);
  --color-selected: var(--color-gray-100);
  --color-accent-ring: oklch(from var(--color-accent) l c h / 0.25);
  --color-icon-trigger: var(--color-amber-500);
  --color-icon-message: var(--color-sky-500);
  --color-icon-delay: var(--color-violet-500);
  --color-icon-control: var(--color-pink-500);
  --color-icon-data: var(--color-emerald-500);
  --color-icon-exit: var(--color-muted);
  --color-card-shadow: oklch(from var(--color-gray-900) l c h / 0.08);
}

.dark .workflow-canvas {
  --color-canvas: black;
  --color-card: var(--color-gray-900);
  --color-edge: var(--color-gray-700);
  --color-grid: var(--color-gray-700);
  --color-selected: var(--color-gray-800);
  --color-icon-trigger: var(--color-amber-400);
  --color-icon-message: var(--color-sky-400);
  --color-icon-delay: var(--color-violet-400);
  --color-icon-control: var(--color-pink-400);
  --color-icon-data: var(--color-emerald-400);
  --color-card-shadow: oklch(from var(--color-gray-950) l c h / 0.6);
}

--color-primary, --color-secondary and --color-accent are not redefined: the site's own values are the ones wanted.

Restyle react-flow's controls through its variables. The zoom controls are react-flow's own and read --xy-controls-* variables. react-flow's stylesheet sits in @layer xyflow, so an unlayered rule wins without !important:

css
.workflow-canvas .react-flow {
  --xy-controls-button-background-color: var(--color-card);
  --xy-controls-button-background-color-hover: var(--color-selected);
  --xy-controls-button-color: var(--color-secondary);
  --xy-controls-button-color-hover: var(--color-primary);
  --xy-controls-button-border-color: var(--color-border);
}

Live badges and selection

The canvas takes the same data the studio feeds it. To show how many users wait on each node, pass a NodeStats object (node id → count), for example from an endpoint of your own that runs the query @sendwend/studio/postgres uses: running instances of the workflow grouped by node_id. To build an inspector, hold the selected id in state and pass selectedId and onSelectNode; the IR has every node's parameters, and each node's meta.loc records where it was built.

tsx
const [selected, setSelected] = useState<string | undefined>();

<WorkflowCanvas
  ir={ir}
  stats={waiting}
  selectedId={selected}
  onSelectNode={setSelected}
/>;

The other components

The canvas is styled entirely inline, which is why the variables above are all it needs. The other components are not:

  • Styling. MetricCard, Sparkline, PushPreview, WorkflowList, TemplatesPage and the rest use Tailwind utility classes against the studio's theme tokens (bg-card, text-muted, border-border, …). A host on Tailwind 4 has to define those tokens and add the package to its sources (@source) for the classes to be generated.
  • Labels. WorkflowList, TemplatesPage and DateRangePicker call react-i18next's useTranslation() for their labels, and the studio's translation strings are not exported. Without an i18next instance that provides those keys, they render the keys themselves. WorkflowCanvas, MetricCard, Sparkline and PushPreview take every string they show as props.

Treat the canvas as the component built for embedding, and the rest as available but tied more closely to the studio's own app.