Synertech Web/ui v0.57.5 Site Storybook

Documentation / Advanced

WorkflowEditor

Client@synertech/uiOpen in Storybook

A visual flow editor: nodes you drag around, linked by Bézier curves drawn from an output port to an input port, on a canvas you pan and zoom with the wheel. The toolbar gathers zoom, recentring, fit to content and JSON import / export. Click to select, Delete or Backspace to remove, Escape to deselect. No external dependency: everything is rendered in HTML and SVG.

Rendering the nodes is up to you: nodeRegistry maps each node type to a render function, its ports (getPorts), a canConnect rule and action buttons. The component is controlled (value + onValueChange) or uncontrolled (defaultValue), and a ref exposes zoomIn, fitToContent, addNode, toJSON, loadJSON… The container takes the full width but no height: give it one through className. "use client" component.

Examples

Basic usage

Uncontrolled: defaultValue provides the initial flow, autoFit frames it on mount.

Open in a tab
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function Basic() {
  return (
    <WorkflowEditor<StepData>
      nodeRegistry={registry}
      defaultValue={workflow}
      autoFit
      className="h-96 rounded-lg border"
    />
  );
}

Controlled mode

Every move, connection or deletion goes through onValueChange.

Open in a tab
import { useState } from "react";
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

export function Controlled() {
  const [value, setValue] = useState(workflow);

  return (
    <div className="space-y-2">
      <p className="text-sm text-gray-600">
        {value.nodes.length} étapes, {value.edges.length} liaisons
      </p>
      <WorkflowEditor<StepData>
        nodeRegistry={registry}
        value={value}
        onValueChange={setValue}
        autoFit
        className="h-96 rounded-lg border"
      />
    </div>
  );
}

Driving it through a ref

addNode adds a step, fitToContent reframes, toJSON serialises the flow.

Open in a tab
import { useRef } from "react";
import { Button, WorkflowEditor, type NodeRegistryEntry, type WorkflowEditorRef, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function WithRef() {
  const ref = useRef<WorkflowEditorRef>(null);

  const addStep = () =>
    ref.current?.addNode({
      id: `n${Date.now()}`,
      type: "action",
      x: 280,
      y: 200,
      width: 160,
      height: 56,
      data: { label: "Nouvelle étape" },
    });

  return (
    <div className="space-y-2">
      <div className="flex gap-2">
        <Button size="sm" onClick={addStep}>
          Ajouter une étape
        </Button>
        <Button size="sm" onClick={() => ref.current?.fitToContent()}>
          Recadrer
        </Button>
        <Button size="sm" onClick={() => console.log(ref.current?.toJSON())}>
          Exporter
        </Button>
      </div>
      <WorkflowEditor<StepData>
        ref={ref}
        nodeRegistry={registry}
        defaultValue={workflow}
        toolbar={false}
        className="h-96 rounded-lg border"
      />
    </div>
  );
}

Snap grid and direction

snapToGrid aligns nodes on gridSize; onDirectionChange adds the direction button to the toolbar.

Open in a tab
import { useState } from "react";
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowDirection, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function GridAndDirection() {
  const [direction, setDirection] = useState<WorkflowDirection>("horizontal");

  return (
    <WorkflowEditor<StepData>
      nodeRegistry={registry}
      defaultValue={workflow}
      snapToGrid
      gridSize={20}
      direction={direction}
      onDirectionChange={setDirection}
      className="h-96 rounded-lg border"
    />
  );
}

Read-only and events

readOnly freezes the flow and hides the toolbar; onNodeClick stays available to show a detail.

Open in a tab
import { useState } from "react";
import { WorkflowEditor, type NodeRegistryEntry, type WorkflowValue } from "@synertech/ui";

interface StepData {
  label: string;
}

const box = (tone: string, selected: boolean, label: string) => (
  <div
    className={`flex h-full items-center justify-center rounded-lg border-2 bg-white px-3 text-xs font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100 ${selected ? "border-blue-500" : tone}`}
  >
    {label}
  </div>
);

const registry: Record<string, NodeRegistryEntry<StepData>> = {
  trigger: {
    title: "Déclencheur",
    render: ({ node, selected }) => box("border-green-300", selected, node.data.label),
    getPorts: () => [{ id: "out", kind: "out", side: "right" }],
  },
  action: {
    title: "Action",
    render: ({ node, selected }) => box("border-gray-300", selected, node.data.label),
    getPorts: () => [
      { id: "in", kind: "in", side: "left" },
      { id: "out", kind: "out", side: "right" },
    ],
    canConnect: ({ fromPort, toPort }) => fromPort.kind === "out" && toPort.kind === "in",
  },
};

const workflow: WorkflowValue<StepData> = {
  nodes: [
    {
      id: "n1",
      type: "trigger",
      x: 40,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Nouvelle commande" },
    },
    {
      id: "n2",
      type: "action",
      x: 280,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Envoyer la facture" },
    },
    {
      id: "n3",
      type: "action",
      x: 520,
      y: 80,
      width: 160,
      height: 56,
      data: { label: "Notifier l’entrepôt" },
    },
  ],
  edges: [
    { id: "e1", from: { nodeId: "n1", portId: "out" }, to: { nodeId: "n2", portId: "in" } },
    { id: "e2", from: { nodeId: "n2", portId: "out" }, to: { nodeId: "n3", portId: "in" } },
  ],
};

export function ReadOnly() {
  const [selected, setSelected] = useState<string | null>(null);

  return (
    <div className="space-y-2">
      <p className="text-sm text-gray-600">Étape : {selected ?? "aucune"}</p>
      <WorkflowEditor<StepData>
        nodeRegistry={registry}
        defaultValue={workflow}
        readOnly
        autoFit
        onNodeClick={({ node }) => setSelected(node.data.label)}
        className="h-80 rounded-lg border"
      />
    </div>
  );
}

API reference

Components

WorkflowEditor

ComponentClient
import { WorkflowEditor } from "@synertech/ui";

Props — WorkflowEditorProps

PropTypeDefaultDescription
nodeRegistry• Record<string, NodeRegistryEntry<TData>> —
autoFit boolean false

Fits the view to the nodes once, as soon as there are nodes to fit: at mount, or when a workflow loaded later arrives.

className string —
connectable boolean true
defaultValue WorkflowValue<TData> —
direction WorkflowDirection "horizontal"
disabled boolean false
draggableNodes boolean true
gridSize number 20
labels UILabelsOf<"workflowEditor" | "editor"> —

The strings the editor writes itself, for this instance: any workflowEditor… key of UILabels, and the editor… keys its toolbar shares with SchemaEditor. Wins over UILabelsProvider.

maxZoom number 3
minZoom number 0.1
multiSelect boolean false
onDirectionChange (direction: WorkflowDirection) => void —
onNodeAction (event: WorkflowNodeActionEvent<TData>) => void —
onNodeClick (event: WorkflowNodeEvent<TData>) => void —
onNodeContextMenu (event: WorkflowNodeEvent<TData>) => void —
onNodeDoubleClick (event: WorkflowNodeEvent<TData>) => void —
onValueChange (next: WorkflowValue<TData>) => void —
panOnDrag boolean true
readOnly boolean false
ref React.Ref<WorkflowEditorRef> —
selectable boolean true
showGrid boolean true
snapToGrid boolean false
toolbar boolean true
value WorkflowValue<TData> —
zoomOnWheel boolean true

Types

type NodeRegistryEntry<TData = unknown> = { … }
FieldTypeDefaultDescription
render• (args: { node: WorkflowNode<TData>; selected: boolean; disabled: boolean }) => ReactNode —
actions | WorkflowNodeAction<TData>[] | ((node: WorkflowNode<TData>) => WorkflowNodeAction<TData>[]) —
canConnect (args: { fromNode: WorkflowNode<TData>; toNode: WorkflowNode<TData>; fromPort: WorkflowPort; toPort: WorkflowPort; }) => boolean —
getPorts (node: WorkflowNode<TData>) => WorkflowPort[] —
icon ReactNode —
title string —

WorkflowEdge

Interface
type WorkflowEdge = { … }
FieldTypeDefaultDescription
from• { nodeId: string; portId: string } —
id• string —
to• { nodeId: string; portId: string } —
disabled boolean —
label string —
type WorkflowEditorRef = { … }
FieldTypeDefaultDescription
addNode• (node: WorkflowNode<unknown>) => void —
fitToContent• () => void —
loadJSON• (json: string) => boolean —

Loads a workflow written as JSON, and says whether it did: text that is not JSON, or JSON that is not a workflow this editor can open, changes nothing.

removeNode• (nodeId: string) => void —
resetView• () => void —
toJSON• () => string —
zoomIn• () => void —
zoomOut• () => void —

WorkflowNode

Interface
type WorkflowNode<TData = unknown> = { … }
FieldTypeDefaultDescription
data• TData —
id• string —
type• string —
x• number —
y• number —
height number —
locked boolean —
ports WorkflowPort[] —
width number —
type WorkflowNodeAction<TData = unknown> = { … }
FieldTypeDefaultDescription
icon• ReactNode —
id• string —
className string —
hidden boolean | ((node: WorkflowNode<TData>) => boolean) —
label string —
position WorkflowNodeActionPosition —
type WorkflowNodeActionEvent<TData = unknown> = { … }
FieldTypeDefaultDescription
actionId• string —
nativeEvent• globalThis.MouseEvent —
node• WorkflowNode<TData> —
type WorkflowNodeEvent<TData = unknown> = { … }
FieldTypeDefaultDescription
nativeEvent• globalThis.MouseEvent —
node• WorkflowNode<TData> —

WorkflowPort

Interface
type WorkflowPort = { … }
FieldTypeDefaultDescription
id• string —
kind• "in" | "out" —
disabled boolean —
label string —
side "left" | "right" | "top" | "bottom" —

WorkflowValue

Interface
type WorkflowValue<TData = unknown> = { … }
FieldTypeDefaultDescription
edges• WorkflowEdge[] —
nodes• WorkflowNode<TData>[] —
viewport WorkflowViewport —
type WorkflowViewport = { … }
FieldTypeDefaultDescription
x• number —
y• number —
zoom• number —
type WorkflowDirection = "horizontal" | "vertical"
type WorkflowNodeActionPosition = "top-right" | "top-left" | "bottom-right" | "bottom-left"