Fragiola

API: core (@fragiola/flow)

Path helpers

The edge path functions the engine draws with, bezier, simple bezier, smooth step, step and straight, their label points and options, and their types.

The engine draws the built-in edge types (bezier, simplebezier, smoothstep, step, straight) with five pure functions, exported so an app can draw its own edge types from the same geometry. Each takes the two ends (and, for the curved and orthogonal ones, the sides they leave and enter by) and returns a PathResult: the SVG path data and the point a label goes at, in the middle of the path (a bezier's at its parameter t = 0.5, the others halfway along their length). They are written from the geometry (no code is ported from another library) and tested on known points.

import { Flow, type FlowEdge, smoothStepPath } from "@fragiola/flow-react";

/** An app edge type drawn from the engine's ends: a smooth step with wider corners. */
function RoundedEdge({ edge }: { edge: FlowEdge<Types> }) {
    return (
        <Flow.Edge<Types> edge={edge}>
            <Flow.EdgePath
                render={(props, state) => (
                    <path {...props} d={smoothStepPath({ ...state.path, borderRadius: 16 }).path} />
                )}
            />
        </Flow.Edge>
    );
}

state.path is the edge's EdgePath (engine.get("edge-path-by", { edgeId })): its own path and label, and its ends (PathEnds), so spreading it gives a helper everything it needs. More on app edge types: Custom edges.

bezierPath

bezierPath(options): one cubic curve whose control points stand out from each end in the direction of its side, so the edge leaves and enters its handles square. A control point stands out by a share (curvature) of how far the other end lies along that direction, and never less than 20 flow units, so an end whose other end is behind it still bends out before turning back. The label is the curve's middle.

BezierPathOptions (with the PathEnds below):

optiontypedescription
curvaturenumberthe share of the distance along a side its control point stands out by; default 0.5

It returns a BezierPathResult: the PathResult below and labelOffset, the distance across (x) and down (y) from the source to the label point, both positive (as React Flow counts it), whichever way the edge runs.

import { bezierPath } from "@fragiola/flow";

const { path, label, labelOffset } = bezierPath({
    source: { x: 0, y: 0 },
    sourceSide: "right",
    target: { x: 200, y: 100 },
    targetSide: "left",
});
// label is the curve's middle, { x: 100, y: 50 }; labelOffset is { x: 100, y: 50 }

edgeCenter(source, target) gives an EdgeCenter: center, the point halfway between two ends, and offset, half their distance across (x) and down (y), both positive. It is where a straight edge's label goes, and a point an app's own path may centre on (a wave, an arc):

import { edgeCenter } from "@fragiola/flow";

const { center, offset } = edgeCenter({ x: 0, y: 0 }, { x: 200, y: -100 });
// center { x: 100, y: -50 }, offset { x: 100, y: 50 }
const arc = `M 0 0 Q ${center.x} ${center.y - offset.x} 200 -100`;

simpleBezierPath

simpleBezierPath(options) (the simplebezier type): one cubic curve whose control points lie on each end's side axis, halfway to where the other end projects on that axis. It has no least reach and no curvature: two ends on one line draw a straight curve, and an end whose other end is behind it turns back at once. A side and its opposite give the same curve. The label is the curve's middle. SimpleBezierPathOptions is the PathEnds below.

smoothStepPath and stepPath

smoothStepPath(options): an orthogonal route (horizontal and vertical segments only) that leaves and enters each handle along its side, each corner rounded. stepPath(options) is the same route with square corners. Each end first stands out from its node by offset; the route then turns once halfway between two ends facing each other, goes out, across and back in when the target is behind, and joins perpendicular sides with one or two corners. When the target is behind on the same line as the source (a right side facing a left side at one height, within a pixel), the run across is offset below the lower end (right of the rightmost for vertical ends): the route draws a loop, never a line back over itself; centerX / centerY still place that run. The label goes halfway along the route's length. Where that one turn (or the run across) sits is the StepTurnOptions': stepPosition of the way from the source, or centerX / centerY for a turn at a fixed place.

SmoothStepPathOptions (with the PathEnds and the StepTurnOptions):

optiontypedescription
borderRadiusnumberthe radius of each rounded corner, in flow units; default 5
offsetnumberhow far each end stands out from its node before the route turns; default 20
stepPositionnumberwhere the turn sits between the source (0) and the target (1), along the ends' axis (across it when the target is behind), counted from the points offset out from each end (so 0 and 1 keep the stand-out); default 0.5, also for a value that is not a finite number
centerXnumberthe x of a vertical turn, in flow coordinates, over stepPosition (ignored when not a finite number)
centerYnumberthe y of a horizontal turn, in flow coordinates, over stepPosition (ignored when not a finite number)

StepPathOptions (with the PathEnds and the StepTurnOptions): offset, stepPosition, centerX and centerY, as above.

The turn is placed between the two points that stand offset out from the ends, not between the ends themselves: stepPosition: 0 turns right where the source's stand-out ends, 1 where the target's begins, and the route never cuts into a node's margin. centerX and centerY are flow coordinates, for edges that must turn on one line (a lane, a bus); a value that is not a finite number is ignored, and the default applies.

import { stepPath } from "@fragiola/flow";

const ends = { source: { x: 0, y: 0 }, sourceSide: "right", target: { x: 300, y: 120 }, targetSide: "left" } as const;
stepPath({ ...ends, stepPosition: 0.25 }).path; // turns a quarter of the way, near the source
stepPath({ ...ends, centerX: 240 }).path; // turns at x = 240, whatever the ends

straightPath

straightPath(options): one line from end to end, its label halfway. The sides do not change a straight line, so it takes the points only.

StraightPathOptions:

optiontypedescription
sourceXYwhere it starts
targetXYwhere it ends

The shared types

PathEnds, the ends every curved or orthogonal helper takes:

fieldtypedescription
sourceXYwhere the edge starts, in flow coordinates
sourceSideHandleSidethe side of the source node it leaves by
targetXYwhere it ends
targetSideHandleSidethe side of the target node it enters by

PathResult, what every helper returns:

fieldtypedescription
pathstringthe SVG path data, for a path element's d
labelXYwhere a label goes, in flow coordinates: a bezier's point at t = 0.5, halfway along the length for the others

EdgePath is both: what "edge-path-by" returns for an edge. HandleSide is "top" | "right" | "bottom" | "left": physical diagram directions (x to the right, y down) that right-to-left text does not flip. An edge whose node has no handle leaves its source by the bottom and enters its target by the top.

PathOptions, the engine's pathOptions (a Flow.Root prop): the shape of the built-in paths for every edge of the flow:

optiontypedescription
curvaturenumberbezier: the share of the distance a control point stands out by; default 0.5
borderRadiusnumbersmooth step: the corners' radius; default 5
offsetnumbersmooth step and step: how far an end stands out before turning; default 20
stepPositionnumbersmooth step and step: where the turn sits between the source (0) and the target (1); default 0.5
centerXnumbersmooth step and step: the x of a vertical turn, in flow coordinates; default none
centerYnumbersmooth step and step: the y of a horizontal turn, in flow coordinates; default none

An edge's own pathOptions (an EdgePathOptions, in the JSON document) go over these, key by key, for that edge: one flat object with the same keys, each read by the types it applies to (a borderRadius on a bezier edge changes nothing).

const edges: FlowEdgeInput<Types>[] = [
    { id: "a-b", source: "a", target: "b", type: "step", pathOptions: { stepPosition: 0.25 } },
    { id: "b-c", source: "b", target: "c", type: "smoothstep", pathOptions: { borderRadius: 16 } },
];

engine.get("edge-path-by", { edgeId }) draws with the merged options, so a custom edge that reads state.path gets them too. See Custom edges.

Telling a node from an edge

isNode(value) and isEdge(value) are type guards for a mixed list (a clipboard, an assistant's answer, a drop): a node is a record with a string id and a position of two numbers, and no source or target; an edge one with a string id, source and target. They check the shape, not the rules: validateFlow does (JSON document).

import { isEdge, isNode } from "@fragiola/flow";

const pasted: unknown[] = JSON.parse(clipboardText);
const nodes = pasted.filter(isNode<Types>);
const edges = pasted.filter(isEdge<Types>);

Rect, an axis-aligned rectangle (a node's box, the selection box, a minimap's view):

fieldtypedescription
xnumberits left
ynumberits top
widthnumberits width
heightnumberits height

Viewport, what the flow shows (view state, the engine's): a flow point p is drawn at p * zoom + (x, y) in the root:

fieldtypedescription
xnumberthe screen offset of the flow's origin from the root's left, in pixels
ynumberfrom the root's top
zoomnumberthe scale

XY is a point, { x, y }, in flow coordinates unless a page says otherwise.