Documentation / Advanced
WorkflowEditor
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.
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.
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.
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.
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.
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
import { WorkflowEditor } from "@synertech/ui";Props — WorkflowEditorProps
| Prop | Type | Default | Description |
|---|---|---|---|
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 |
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
NodeRegistryEntry
Interfacetype NodeRegistryEntry<TData = unknown> = { … }| Field | Type | Default | Description |
|---|---|---|---|
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
Interfacetype WorkflowEdge = { … }| Field | Type | Default | Description |
|---|---|---|---|
from• |
{ nodeId: string; portId: string } |
— | |
id• |
string |
— | |
to• |
{ nodeId: string; portId: string } |
— | |
disabled |
boolean |
— | |
label |
string |
— |
WorkflowEditorRef
Interfacetype WorkflowEditorRef = { … }| Field | Type | Default | Description |
|---|---|---|---|
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
Interfacetype WorkflowNode<TData = unknown> = { … }| Field | Type | Default | Description |
|---|---|---|---|
data• |
TData |
— | |
id• |
string |
— | |
type• |
string |
— | |
x• |
number |
— | |
y• |
number |
— | |
height |
number |
— | |
locked |
boolean |
— | |
ports |
WorkflowPort[] |
— | |
width |
number |
— |
WorkflowNodeAction
Interfacetype WorkflowNodeAction<TData = unknown> = { … }| Field | Type | Default | Description |
|---|---|---|---|
icon• |
ReactNode |
— | |
id• |
string |
— | |
className |
string |
— | |
label |
string |
— | |
position |
WorkflowNodeActionPosition |
— |
WorkflowNodeActionEvent
Interfacetype WorkflowNodeActionEvent<TData = unknown> = { … }| Field | Type | Default | Description |
|---|---|---|---|
actionId• |
string |
— | |
nativeEvent• |
globalThis.MouseEvent |
— | |
node• |
WorkflowNode<TData> |
— |
WorkflowNodeEvent
Interfacetype WorkflowNodeEvent<TData = unknown> = { … }| Field | Type | Default | Description |
|---|---|---|---|
nativeEvent• |
globalThis.MouseEvent |
— | |
node• |
WorkflowNode<TData> |
— |
WorkflowPort
Interfacetype WorkflowPort = { … }| Field | Type | Default | Description |
|---|---|---|---|
id• |
string |
— | |
kind• |
"in" | "out" |
— | |
disabled |
boolean |
— | |
label |
string |
— | |
side |
"left" | "right" | "top" | "bottom" |
— |
WorkflowValue
Interfacetype WorkflowValue<TData = unknown> = { … }| Field | Type | Default | Description |
|---|---|---|---|
edges• |
WorkflowEdge[] |
— | |
nodes• |
WorkflowNode<TData>[] |
— | |
viewport |
WorkflowViewport |
— |
WorkflowViewport
Interfacetype WorkflowViewport = { … }| Field | Type | Default | Description |
|---|---|---|---|
x• |
number |
— | |
y• |
number |
— | |
zoom• |
number |
— |
type WorkflowDirection = "horizontal" | "vertical"type WorkflowNodeActionPosition = "top-right" | "top-left" | "bottom-right" | "bottom-left"