Fragiola

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: defaultNodes and defaultEdges are read once, at mount. Read and change the flow through the model (useFlow, a flow ref).
  • Controlled: nodes and edges are your state. Each render brings the model to them with the commands a user would run (model.get("commands-to"), one batch marked meta: { source: "controlled" }), and onNodesChange / onEdgesChange receive 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 the Flow.Nodes and Flow.Edges children functions stable (a module function, useCallback): a new one re-renders every node.
  • Your own model: model takes a model made with createFlowModel, with its features and its middleware; defaultNodes, features and createId are 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).

proptypedefaultdescription
modelFlowModel<T, F>created by the rootthe flow model: its middleware sees every command, its listeners every change
defaultNodesreadonly FlowNodeInput<T>[][]the nodes of the model the root creates (read once)
defaultEdgesreadonly FlowEdgeInput<T>[][]the edges of the model the root creates (read once)
nodesreadonly FlowNode<T>[]nonecontrolled nodes; give every node its id
onNodesChange(nodes: readonly FlowNode<T>[]) => voidnonethe model's nodes, after every command that changes them
edgesreadonly FlowEdge<T>[]nonecontrolled edges
onEdgesChange(edges: readonly FlowEdge<T>[]) => voidnonethe model's edges, after every command that changes them
featuresF[]the opt-in features of the model the root creates (read once), each from its entry (@fragiola/flow-react/drag)
createIdCreateId`${kind}-${n}`the id generator of the model the root creates (read once)
defaultEdgeOptionsDefaultEdgeOptions<T>nonethe 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
viewportViewportnonecontrolled viewport: a new value runs set-viewport
defaultViewportViewport{ x: 0, y: 0, zoom: 1 }the viewport the flow starts at (read once)
onViewportChange(viewport: Viewport, gesture: boolean) => voidnoneevery viewport change; gesture is true for a pan, a zoom or a pinch
fitViewboolean | FitViewPayloadfalsefits 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)
flowRefFlowRef<T, F>noneaccess to the model and the engine from outside the root (flow ref)
minZoomnumber0.5the least zoom
maxZoomnumber2the greatest zoom
translateExtentRectnonethe flow rectangle the view stays inside
panOnDragboolean | number[]truea drag on the pane pans: true with the primary button (a touch, a pen), or the mouse buttons listed (0, 1, 2)
panOnScrollbooleanfalsethe wheel pans (Ctrl + wheel zooms)
zoomOnScrollbooleantruethe wheel zooms around the pointer
zoomOnPinchbooleantruea two-finger pinch (and a trackpad's) zooms
zoomOnDoubleClickbooleantruea double click on the pane zooms in
zoomStepnumber1.2the factor of zoom-in and zoom-out
interactivebooleantruegestures and keys may change the flow (pan and zoom always work)
onlyRenderVisiblebooleanfalserender only the nodes and edges in view
pathOptionsPathOptions{ curvature: 0.5, borderRadius: 5, offset: 20 }the shape of the built-in edge paths (path helpers)
autoPanboolean | AutoPanOptionstruethe 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)
panActivationKeystring | null" "held, a primary drag pans from anywhere, nodes included; null for none
panOnScrollModePanOnScrollMode"free"which way the wheel pans with panOnScroll: "free", "horizontal", "vertical"
panOnScrollSpeednumber1the factor of the wheel's pixels when it pans
zoomActivationKeystring | nullMeta on Apple platforms, Control elsewhereheld, the wheel zooms while panOnScroll is on; null for none
easingViewportEasinga cubic ease in and outhow an animated viewport change (a duration) eases
nodeOriginXY{ 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)
nodeExtentRectnonethe flow rectangle every node's box stays inside when dragged, moved by keys or resized
elevateNodesOnSelectbooleantruea selected node, and its children, is drawn above the others
elevateEdgesOnSelectbooleantrue with zIndexMode="auto", false with "basic"a selected edge is drawn above the other edges (in "auto", above the nodes too)
zIndexModeZIndexMode"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
paneClickDistancenumber3how far, in pixels, a press on the pane may move and still be a click (onPaneClick, clearing the selection)
nodeClickDistancenumber3how far a press on a node, an edge or a handle may move and still be a click (onNodeClick, selecting)
preventScrollingbooleantruethe wheel over the flow never scrolls the page, even with zoomOnScroll and panOnScroll off
nodesFocusablebooleantruenodes take focus (with keyboard(); a node's focusable: false still wins)
edgesFocusablebooleantrueedges take focus (with keyboard(); an edge's focusable: false still wins)
childrenReactNodenonethe flow's layers: Flow.Background, Flow.Viewport, Flow.Controls, …
onInit(event: FlowEngineEventOf<"ready">) => voidnoneready: the root has a size and the fitView fit is done, once; it waits for no node measurement
onMoveStart(event: FlowEngineEventOf<"viewport-change-start">) => voidnoneviewport-change-start: the viewport starts a move (a pan, the wheel, a pinch, an animation, an action)
onMoveEnd(event: FlowEngineEventOf<"viewport-change-end">) => voidnoneviewport-change-end: the move ended (a release, the animation's end, the wheel at rest for 150 ms)
onNodeClick(event: FlowEngineEventOf<"node-click">) => voidnonenode-click: a click on a node: a press released within nodeClickDistance that dragged nothing
onNodeDoubleClick(event: FlowEngineEventOf<"node-double-click">) => voidnonenode-double-click: a double click on a node (two clicks)
onNodeContextMenu(event: FlowEngineEventOf<"node-context-menu">) => voidnonenode-context-menu: a context menu on a node (event.event.preventDefault() to show your own)
onEdgeClick(event: FlowEngineEventOf<"edge-click">) => voidnoneedge-click: a click on an edge
onEdgeDoubleClick(event: FlowEngineEventOf<"edge-double-click">) => voidnoneedge-double-click: a double click on an edge
onEdgeContextMenu(event: FlowEngineEventOf<"edge-context-menu">) => voidnoneedge-context-menu: a context menu on an edge
onPaneClick(event: FlowEngineEventOf<"pane-click">) => voidnonepane-click: a click on the pane: a press within paneClickDistance
onPaneDoubleClick(event: FlowEngineEventOf<"pane-double-click">) => voidnonepane-double-click: a double click on the pane (it zooms in with zoomOnDoubleClick)
onPaneContextMenu(event: FlowEngineEventOf<"pane-context-menu">) => voidnonepane-context-menu: a context menu on the pane
onSelectionContextMenu(event: FlowEngineEventOf<"selection-context-menu">) => voidnoneselection-context-menu: a context menu on one of several selected nodes
onSelectionStart(event: FlowEngineEventOf<"box-select-start">) => voidnonebox-select-start: a selection box started (selection())
onSelectionEnd(event: FlowEngineEventOf<"box-select">) => voidnonebox-select: a selection box ended
onNodeDragStart(event: FlowEngineEventOf<"drag-start">) => voidnonedrag-start: a drag of nodes started (drag())
onNodesMove(event: FlowEngineEventOf<"nodes-move">) => voidnonenodes-move: nodes were dropped, or moved by the arrows
onNodeDragCancel(event: FlowEngineEventOf<"drag-cancel">) => voidnonedrag-cancel: a drag was cancelled
onSelectionDragStart(event: FlowEngineEventOf<"selection-drag-start">) => voidnoneselection-drag-start: a drag of the rectangle around the selection started
onSelectionDrag(event: FlowEngineEventOf<"selection-drag">) => voidnoneselection-drag: the rectangle's drag moved its nodes (each step)
onSelectionDragStop(event: FlowEngineEventOf<"selection-drag-end">) => voidnoneselection-drag-end: the rectangle's drag dropped its nodes
onConnectStart(event: FlowEngineEventOf<"connect-start">) => voidnoneconnect-start: a connection started (connection())
onConnectTarget(event: FlowEngineEventOf<"connect-target">) => voidnoneconnect-target: a keyboard connection points at another target
onConnect(event: FlowEngineEventOf<"connect">) => voidnoneconnect: an edge was added by a connection
onConnectCancel(event: FlowEngineEventOf<"connect-cancel">) => voidnoneconnect-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">) => voidnonereconnect-start: an edge's end was grabbed (reconnection())
onReconnect(event: FlowEngineEventOf<"reconnect">) => voidnonereconnect: an edge was reconnected
onReconnectCancel(event: FlowEngineEventOf<"reconnect-cancel">) => voidnonereconnect-cancel: a reconnection ended with the edge as it was, with where
onResizeStart(event: FlowEngineEventOf<"resize-start">) => voidnoneresize-start: a resize started (resize()); its steps are transient node.resize commands (onNodesChange)
onResizeEnd(event: FlowEngineEventOf<"resize">) => voidnoneresize: a node was resized
onResizeCancel(event: FlowEngineEventOf<"resize-cancel">) => voidnoneresize-cancel: a resize was cancelled
onDelete(event: FlowEngineEventOf<"remove">) => voidnoneremove: 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:

fieldtypedescription
interactivebooleangestures and keys may change the flow
panningbooleanthe viewport is being panned
draggingbooleannodes are being dragged (drag())
connectingbooleana connection is being drawn (connection())
selectingbooleana selection box is being drawn (selection())

Data attributes

attributevalue
data-flow-part"root"
data-interactivepresent while gestures may change the flow; absent before the engine exists (the first render, a server render), while state.interactive is already true
data-panningpresent while panning
data-draggingpresent while nodes are dragged (drag())
data-connectingpresent while a connection is drawn (connection())
data-selectingpresent 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>):

proptypedescription
renderRenderProp<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
classNamestring | ((state: State) => string | undefined)a class, or a function of the primitive's state
styleCSSProperties | ((state: State) => CSSProperties | undefined)a style, or a function of the state; the structural keys a primitive sets always win
refRef<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).