API: primitives (@fragiola/flow-react, @fragiola/flow-vue, @fragiola/flow-svelte, @fragiola/flow-angular)
Root
Flow.Root holds the model and the engine of one flow, takes its nodes and edges controlled or uncontrolled. Give it a size, and the flow fills it.
Flow.Root is the flow. It owns the model (or takes yours with model), creates the engine once
it is mounted, and attaches the engine to its element: the engine listens to the root itself
(pointer, wheel, keys, focus), so no event is forwarded by the framework. It renders a div with only your
children, no role and no name; no Provider is needed above it. Its only structural style is
position: relative; overflow: hidden: give it a size, and the flow fills it (see
Sizing the root).
import { Flow, type FlowEdgeInput, type FlowNodeInput, type FlowTypes } from "@fragiola/flow-react";
import { connection } from "@fragiola/flow-react/connection";
import { drag } from "@fragiola/flow-react/drag";
import { selection } from "@fragiola/flow-react/selection";
interface Types extends FlowTypes {
readonly nodes: { step: { label: string } };
}
const features = [selection(), drag(), connection()] as const;
const nodes: FlowNodeInput<Types>[] = [
{ id: "a", type: "step", position: { x: 0, y: 0 }, data: { label: "Start" } },
{ id: "b", type: "step", position: { x: 0, y: 140 }, data: { label: "Save" } },
];
const edges: FlowEdgeInput<Types>[] = [{ id: "a-b", source: "a", target: "b" }];
export function Editor() {
return (
<div style={{ height: 480 }}>
<Flow.Root<Types, typeof features>
features={features}
defaultNodes={nodes}
defaultEdges={edges}
fitView={{ padding: 0.2 }}
className="size-full"
>
<Flow.Viewport>
<Flow.Edges<Types>>{(edge) => <Flow.Edge<Types> edge={edge} />}</Flow.Edges>
<Flow.Nodes<Types>>
{(node) => (
<Flow.Node<Types> node={node}>
{node.data.label}
<Flow.Handle type="target" side="top" />
<Flow.Handle type="source" side="bottom" />
</Flow.Node>
)}
</Flow.Nodes>
</Flow.Viewport>
</Flow.Root>
</div>
);
}Pass the registry and the features as type arguments (Flow.Root<Types, typeof features>): the
commands, keys and data are then typed. The features are fixed when the model is created: a
changed features prop warns and is ignored.
Controlled or uncontrolled
- Uncontrolled:
defaultNodesanddefaultEdgesare read once, at mount. Read and change the flow through the model (useFlow, a flow ref). - Controlled:
nodesandedgesare your state. Each render brings the model to them with the commands a user would run (model.get("commands-to"), onebatchmarkedmeta: { source: "controlled" }), andonNodesChange/onEdgesChangereceive the model's lists after every other change (a drag step too). When a middleware vetoes a change, or a rule keeps a node in its parent, the callbacks receive what the model holds, so your state follows it. Keep theFlow.NodesandFlow.Edgeschildren functions stable (a module function,useCallback): a new one re-renders every node. - Your own model:
modeltakes a model made withcreateFlowModel, with its features and its middleware;defaultNodes,featuresandcreateIdare then unused.
The engine is created in the root's layout effect (Vue: its onMounted; Svelte: an $effect; Angular: after its first render): the first render, and a
server render, have no engine. They draw the document's static geometry instead: nodes at their
place (origin applied), and the edges whose nodes declare their sizes and handles
(Server rendering).
Props
RootProps<T, F> extends the common primitive props, every div attribute, and
RootEngineProps, the engine's options a root takes as props (each change applies at once,
through engine.adapter.setOptions).
| prop | type | default | description |
|---|---|---|---|
model | FlowModel<T, F> | created by the root | the flow model: its middleware sees every command, its listeners every change |
defaultNodes | readonly FlowNodeInput<T>[] | [] | the nodes of the model the root creates (read once) |
defaultEdges | readonly FlowEdgeInput<T>[] | [] | the edges of the model the root creates (read once) |
nodes | readonly FlowNode<T>[] | none | controlled nodes; give every node its id |
onNodesChange | (nodes: readonly FlowNode<T>[]) => void | none | the model's nodes, after every command that changes them |
edges | readonly FlowEdge<T>[] | none | controlled edges |
onEdgesChange | (edges: readonly FlowEdge<T>[]) => void | none | the model's edges, after every command that changes them |
features | F | [] | the opt-in features of the model the root creates (read once), each from its entry (@fragiola/flow-react/drag) |
createId | CreateId | `${kind}-${n}` | the id generator of the model the root creates (read once) |
defaultEdgeOptions | DefaultEdgeOptions<T> | none | the fields every edge.add of the model the root creates fills in where it leaves them out (read once; the model option): { type: "smoothstep" } makes every new connection a smooth step |
viewport | Viewport | none | controlled viewport: a new value runs set-viewport |
defaultViewport | Viewport | { x: 0, y: 0, zoom: 1 } | the viewport the flow starts at (read once) |
onViewportChange | (viewport: Viewport, gesture: boolean) => void | none | every viewport change; gesture is true for a pan, a zoom or a pinch |
fitView | boolean | FitViewPayload | false | fits the nodes once the root and every visible one have a size, and once more on the measured sizes unless the view moved (read once) |
flowRef | FlowRef<T, F> | none | access to the model and the engine from outside the root (flow ref) |
minZoom | number | 0.5 | the least zoom |
maxZoom | number | 2 | the greatest zoom |
translateExtent | Rect | none | the flow rectangle the view stays inside |
panOnDrag | boolean | number[] | true | a drag on the pane pans: true with the primary button (a touch, a pen), or the mouse buttons listed (0, 1, 2) |
panOnScroll | boolean | false | the wheel pans (Ctrl + wheel zooms) |
zoomOnScroll | boolean | true | the wheel zooms around the pointer |
zoomOnPinch | boolean | true | a two-finger pinch (and a trackpad's) zooms |
zoomOnDoubleClick | boolean | true | a double click on the pane zooms in |
zoomStep | number | 1.2 | the factor of zoom-in and zoom-out |
interactive | boolean | true | gestures and keys may change the flow (pan and zoom always work) |
onlyRenderVisible | boolean | false | render only the nodes and edges in view |
pathOptions | PathOptions | { curvature: 0.5, borderRadius: 5, offset: 20 } | the shape of the built-in edge paths (path helpers) |
autoPan | boolean | AutoPanOptions | true | the viewport pans while a drag, a connection, a reconnection, a selection box or a resize is near the root's edge ({ speed: 10, margin: 30 }; { node, connect, selection, resize } turn one gesture's off) |
panActivationKey | string | null | " " | held, a primary drag pans from anywhere, nodes included; null for none |
panOnScrollMode | PanOnScrollMode | "free" | which way the wheel pans with panOnScroll: "free", "horizontal", "vertical" |
panOnScrollSpeed | number | 1 | the factor of the wheel's pixels when it pans |
zoomActivationKey | string | null | Meta on Apple platforms, Control elsewhere | held, the wheel zooms while panOnScroll is on; null for none |
easing | ViewportEasing | a cubic ease in and out | how an animated viewport change (a duration) eases |
nodeOrigin | XY | { x: 0, y: 0 } | the point of a node's box its position stands at, as shares of its size (for the nodes on the flow) |
nodeExtent | Rect | none | the flow rectangle every node's box stays inside when dragged, moved by keys or resized |
elevateNodesOnSelect | boolean | true | a selected node, and its children, is drawn above the others |
elevateEdgesOnSelect | boolean | true with zIndexMode="auto", false with "basic" | a selected edge is drawn above the other edges (in "auto", above the nodes too) |
zIndexMode | ZIndexMode | "auto" | how nodes and edges stack (z-order): "basic" edges always below the nodes, "auto" an edge of a child or a selected one may stand above nodes, "manual" only the zIndex values count |
paneClickDistance | number | 3 | how far, in pixels, a press on the pane may move and still be a click (onPaneClick, clearing the selection) |
nodeClickDistance | number | 3 | how far a press on a node, an edge or a handle may move and still be a click (onNodeClick, selecting) |
preventScrolling | boolean | true | the wheel over the flow never scrolls the page, even with zoomOnScroll and panOnScroll off |
nodesFocusable | boolean | true | nodes take focus (with keyboard(); a node's focusable: false still wins) |
edgesFocusable | boolean | true | edges take focus (with keyboard(); an edge's focusable: false still wins) |
children | ReactNode | none | the flow's layers: Flow.Background, Flow.Viewport, Flow.Controls, … |
onInit | (event: FlowEngineEventOf<"ready">) => void | none | ready: the root has a size and the fitView fit is done, once; it waits for no node measurement |
onMoveStart | (event: FlowEngineEventOf<"viewport-change-start">) => void | none | viewport-change-start: the viewport starts a move (a pan, the wheel, a pinch, an animation, an action) |
onMoveEnd | (event: FlowEngineEventOf<"viewport-change-end">) => void | none | viewport-change-end: the move ended (a release, the animation's end, the wheel at rest for 150 ms) |
onNodeClick | (event: FlowEngineEventOf<"node-click">) => void | none | node-click: a click on a node: a press released within nodeClickDistance that dragged nothing |
onNodeDoubleClick | (event: FlowEngineEventOf<"node-double-click">) => void | none | node-double-click: a double click on a node (two clicks) |
onNodeContextMenu | (event: FlowEngineEventOf<"node-context-menu">) => void | none | node-context-menu: a context menu on a node (event.event.preventDefault() to show your own) |
onEdgeClick | (event: FlowEngineEventOf<"edge-click">) => void | none | edge-click: a click on an edge |
onEdgeDoubleClick | (event: FlowEngineEventOf<"edge-double-click">) => void | none | edge-double-click: a double click on an edge |
onEdgeContextMenu | (event: FlowEngineEventOf<"edge-context-menu">) => void | none | edge-context-menu: a context menu on an edge |
onPaneClick | (event: FlowEngineEventOf<"pane-click">) => void | none | pane-click: a click on the pane: a press within paneClickDistance |
onPaneDoubleClick | (event: FlowEngineEventOf<"pane-double-click">) => void | none | pane-double-click: a double click on the pane (it zooms in with zoomOnDoubleClick) |
onPaneContextMenu | (event: FlowEngineEventOf<"pane-context-menu">) => void | none | pane-context-menu: a context menu on the pane |
onSelectionContextMenu | (event: FlowEngineEventOf<"selection-context-menu">) => void | none | selection-context-menu: a context menu on one of several selected nodes |
onSelectionStart | (event: FlowEngineEventOf<"box-select-start">) => void | none | box-select-start: a selection box started (selection()) |
onSelectionEnd | (event: FlowEngineEventOf<"box-select">) => void | none | box-select: a selection box ended |
onNodeDragStart | (event: FlowEngineEventOf<"drag-start">) => void | none | drag-start: a drag of nodes started (drag()) |
onNodesMove | (event: FlowEngineEventOf<"nodes-move">) => void | none | nodes-move: nodes were dropped, or moved by the arrows |
onNodeDragCancel | (event: FlowEngineEventOf<"drag-cancel">) => void | none | drag-cancel: a drag was cancelled |
onSelectionDragStart | (event: FlowEngineEventOf<"selection-drag-start">) => void | none | selection-drag-start: a drag of the rectangle around the selection started |
onSelectionDrag | (event: FlowEngineEventOf<"selection-drag">) => void | none | selection-drag: the rectangle's drag moved its nodes (each step) |
onSelectionDragStop | (event: FlowEngineEventOf<"selection-drag-end">) => void | none | selection-drag-end: the rectangle's drag dropped its nodes |
onConnectStart | (event: FlowEngineEventOf<"connect-start">) => void | none | connect-start: a connection started (connection()) |
onConnectTarget | (event: FlowEngineEventOf<"connect-target">) => void | none | connect-target: a keyboard connection points at another target |
onConnect | (event: FlowEngineEventOf<"connect">) => void | none | connect: an edge was added by a connection |
onConnectCancel | (event: FlowEngineEventOf<"connect-cancel">) => void | none | connect-cancel: a connection ended without an edge, with where (from, to, client, point): React Flow's onConnectEnd with its connection state |
onReconnectStart | (event: FlowEngineEventOf<"reconnect-start">) => void | none | reconnect-start: an edge's end was grabbed (reconnection()) |
onReconnect | (event: FlowEngineEventOf<"reconnect">) => void | none | reconnect: an edge was reconnected |
onReconnectCancel | (event: FlowEngineEventOf<"reconnect-cancel">) => void | none | reconnect-cancel: a reconnection ended with the edge as it was, with where |
onResizeStart | (event: FlowEngineEventOf<"resize-start">) => void | none | resize-start: a resize started (resize()); its steps are transient node.resize commands (onNodesChange) |
onResizeEnd | (event: FlowEngineEventOf<"resize">) => void | none | resize: a node was resized |
onResizeCancel | (event: FlowEngineEventOf<"resize-cancel">) => void | none | resize-cancel: a resize was cancelled |
onDelete | (event: FlowEngineEventOf<"remove">) => void | none | remove: a delete key removed nodes and edges (keyboard(), after its beforeDelete) |
The callbacks are RootEventProps: one per engine event but
viewport-change (onViewportChange has its own), named from the core's ENGINE_EVENT_CALLBACKS
(a CallbackEventType to its EventCallbackName), each called with the event (its
FlowEngineEventOf<K>), the latest one given. The names follow React Flow's callbacks; Vue emits
them camelCase (nodeClick: @node-click in a template, onNodeClick in h() or TSX), Svelte takes them lowercase (onnodeclick), Angular as outputs
of flowRoot ((nodeClick)).
<Flow.Root
features={features}
onNodeClick={(event) => setOpen(event.nodeId)}
onPaneContextMenu={(event) => {
event.event.preventDefault();
showMenu(event.client);
}}
>
{/* … */}
</Flow.Root>State
RootState, what className, style and render receive:
| field | type | description |
|---|---|---|
interactive | boolean | gestures and keys may change the flow |
panning | boolean | the viewport is being panned |
dragging | boolean | nodes are being dragged (drag()) |
connecting | boolean | a connection is being drawn (connection()) |
selecting | boolean | a selection box is being drawn (selection()) |
Data attributes
| attribute | value |
|---|---|
data-flow-part | "root" |
data-interactive | present while gestures may change the flow; absent before the engine exists (the first render, a server render), while state.interactive is already true |
data-panning | present while panning |
data-dragging | present while nodes are dragged (drag()) |
data-connecting | present while a connection is drawn (connection()) |
data-selecting | present while a box is drawn (selection()) |
Structural style
position: relative; overflow: hidden, and the engine writes touch-action: none (without it a
browser cancels touch gestures). Its size is yours.
Common props
Every primitive takes these on top of its element's attributes (PrimitiveProps<State, E>; the
div primitives' props are DivPrimitiveProps<State>, the buttons' ButtonPrimitiveProps<State>,
the SVG ones' SvgPrimitiveProps<State, E>):
| prop | type | description |
|---|---|---|
render | RenderProp<State, E> | replaces the element: an element whose props are merged (render={<section />}), or a function (props, state) => element whose props (RenderedProps<E>) you spread on what it returns. Never asChild |
className | string | ((state: State) => string | undefined) | a class, or a function of the primitive's state |
style | CSSProperties | ((state: State) => CSSProperties | undefined) | a style, or a function of the state; the structural keys a primitive sets always win |
ref | Ref<E> | the element, merged with the primitive's own ref |
In Vue, render is the as prop and the render scoped slot, className and style are class
and style without the function form, and ref is a template ref on the component: see
Vue components. In Svelte, render is a snippet of
{ props, state, children }, className is class (a value or a function of the state, as
style), and ref is bind:ref: see Svelte components. In Angular, every
part is a directive on your own element, so there is no render; className and style are
your class, style and their bindings, and ref is your template reference: see
Angular directives.
Your handlers run after the primitive's own. A primitive sets no text and no aria-label: names
are yours (Accessible names).