Fragiola

Concepts

Handles and connections

A handle is a node's connection point on one of its sides, and a connection is an edge being drawn between two handles, checked before the drop.

A handle is where an edge starts or ends on a node: a source handle or a target handle, on one of the node's sides. A connection is an edge being drawn: with connection(), a press on a handle starts one, the connection line follows the pointer, and a drop near another handle adds the edge. The model decides whether that edge may exist, so the same rules hold for a drag and for your code.

Handles

Render a Flow.Handle inside Flow.Node for each connection point. It registers with the engine, which measures its centre relative to its node, and it sits at the middle of its side by default:

<Flow.Node<Types> node={node} className="node">
    {node.data.label}
    <Flow.Handle type="target" side="left" handleId="in" className="handle" />
    <Flow.Handle type="source" side="right" handleId="ok" style={{ top: "30%" }} className="handle" />
    <Flow.Handle type="source" side="right" handleId="error" style={{ top: "70%" }} className="handle" />
</Flow.Node>
propmeans
type"source": edges start here; "target": edges end here
side"top", "right", "bottom" or "left": the direction its edges leave or enter by
handleIdits id among the node's handles of its type; none is the node's default handle
connectablefalse: no connection starts or ends here (default true)

An edge names its handles with sourceHandle and targetHandle; one it leaves out uses the node's first handle of that type. A handle has no size and no look: give it both, and a hit area big enough for a finger.

Sides are physical, not logical: a diagram's right stays right in a right-to-left page, so side never flips with the text direction.

The engine measures each handle's centre once it is rendered. Until then, a node may declare its handles in the document (handles: { id?, type, position, x, y, width?, height? }), so its edges are drawn on the server and in the first render; a measured handle always wins (Sizes and handles before measuring).

A connection

With connection() installed, dragging from a handle draws a connection. Flow.ConnectionLine, inside the viewport, draws it while it lasts; useConnection() reads it:

<Flow.Viewport>
    <Flow.Edges<Types>>{renderEdge}</Flow.Edges>
    <Flow.Nodes<Types>>{renderNode}</Flow.Nodes>
    <Flow.ConnectionLine className="stroke-blue-500 data-invalid:stroke-red-500" />
</Flow.Viewport>

While it is drawn, the engine looks for the nearest handle within radius screen pixels of the pointer (connection({ radius: 20 })), and asks the model whether the edge would be accepted: model.can("edge.add", …). The handle it would end at gets data-valid or data-invalid, and so does the connection line. Dropping on a valid handle runs edge.add; anywhere else, nothing changes and the engine tells connect-cancel (with refused: true when the handle was refused).

  • mode: "strict" (the default): a connection ends only at a handle of the other type. mode: "loose": at any other handle.
  • A connection may start from a target handle: the edge is added the other way round, from the handle it ends at.
  • Escape, a cancelled pointer or a lost pointer capture cancels it.

Click to connect

With connection({ clickToConnect: true }), a tap on a handle starts a connection that stays after the release: the connection line follows the pointer, and a tap on another handle draws the edge, checked by model.can("edge.add", …) exactly as a drop (a vetoed handle is data-invalid). A tap anywhere else, Escape (wherever the focus is) or cancel-gesture cancels it, and that tap selects nothing. A drag from a handle still connects as before, so a mouse, a finger and a pen each use what suits them; while a connection waits, the root has data-connecting and the pan key still pans.

const features = [selection(), drag(), connection({ clickToConnect: true })] as const;

It is off by default: a tap on a handle then does nothing, and a slightly shaky press on a handle starts a drag after 1 pixel instead of 3.

A handle's role

A handle starts and ends connections by default. connectableStart={false} makes it an end only (a press, a click or Enter on it starts nothing, and it takes no focus), connectableEnd={false} a start only (no connection ever ends there): React Flow's isConnectableStart and isConnectableEnd. Its part carries data-connectable-start and data-connectable-end while it may, to style the difference.

<Flow.Handle type="source" side="right" connectableEnd={false} />
<Flow.Handle type="target" side="left" connectableStart={false} />

Dropped on the canvas

A connection that ends over no handle is told by onConnectCancel (Vue @connect-cancel, Svelte onconnectcancel, Angular (connectCancel)), with the handle it came from (from), the handle it was over (to, none here) and the drop point, as a client point (client) and a flow point (point): what "drop a connection on the canvas to add a node" needs. dropped is false when the connection was cancelled instead (Escape, the lock, a pointercancel): no node then. Every connection ends in one of the two: onConnect once it added an edge, onConnectCancel when it added none, so there is no onConnectEnd.

<Flow.Root
    features={features}
    onConnectCancel={(event) => {
        if (!event.dropped || event.to || event.input !== "pointer") return;
        const added = model.run("node.add", { type: "task", position: event.point, data: {} });
        if (!added.ok) return;
        const { nodeId } = added.value;
        // from a target handle, the new edge runs the other way
        model.run(
            "edge.add",
            event.from.type === "source"
                ? { source: event.from.nodeId, target: nodeId }
                : { source: nodeId, target: event.from.nodeId },
        );
    }}
>
    {/* … */}
</Flow.Root>
Add nodes
Gallery

The threshold and the line

connection({ threshold }) is how far (in pixels, default 1) a press on a handle moves before it draws a connection, and connection({ lineType }) the connection line's path, one of the built-in edge types ("bezier" by default, "simplebezier", "smoothstep", "step", "straight"):

const features = [connection({ threshold: 4, lineType: "smoothstep" })] as const;

Default edge fields

The model option defaultEdgeOptions (the root's defaultEdgeOptions prop when it creates the model) fills every edge.add where its payload leaves a field out, before any rule sees it, so a connection's new edges get your type and data:

const model = createFlowModel(json, { features, defaultEdgeOptions: { type: "smoothstep" } });

It fills edge.add only: the document's own edges (defaultEdges, a loaded flow) stay as they are, and an edge is drawn with the fields it holds, never with defaults it does not.

From the keyboard

With keyboard() installed too, a connectable handle takes focus: Enter on it starts a connection that points at the nearest handle the model accepts, Tab or the arrows point at the next one, Enter connects through the same edge.add and Escape cancels. The handle pointed at is data-valid, and each move is a connect-target event for your live region. See Keyboard and accessibility.

Rules are middleware

There is no isValidConnection callback. A rule is a middleware on edge.add, and because the engine asks model.can during the drag, a vetoed handle is marked data-invalid before the drop:

model.use((ctx, next) => {
    if (ctx.command === "edge.add" && ctx.payload.source === ctx.payload.target) {
        return veto("an edge joins two different nodes");
    }
    return next();
});

The same rule refuses the same edge from your code or from an assistant. Connection rules builds typed ports, no cycles and one edge per input this way.

Connection validation
Gallery

Moving an edge's end

With reconnection() (which needs connection()), a press on an edge near one of its ends, or on a Flow.ReconnectAnchor, moves that end to another handle: the drop runs edge.reconnect, checked by the model like edge.add. An edge with reconnectable: false keeps its ends; reconnectable: "source" (or "target") moves that end only: the other end's anchor draws nothing, a press near it grabs nothing, and edge.reconnect refuses to move it, from code too.

Events

engine.subscribe hears connect-start (nodeId, handleId, handleType), connect (the new edgeId, source, target), connect-cancel (refused, and where it ended: from, to, client, point), each with input ("pointer" or "keyboard"); connect-target (nodeId, handleId, index, count) while a keyboard connection moves its target; and for reconnection reconnect-start, reconnect and reconnect-cancel (with the end it moved and where it ended). The root calls each back: onConnectStart, onConnect, onConnectCancel, onReconnectCancel, … (Root).