Fragiola

Guides

Custom edges

Draw the five built-in edge types, an edge type of your own from the path's ends, HTML labels that pan and zoom with their edge, and an edge toolbar.

An edge is an SVG group you render from Flow.Edges' children function. The engine computes its path once both nodes have a size, and recomputes it whenever they move or change size. Five edge types are drawn by the core: bezier (an edge with no type), simplebezier, smoothstep, step and straight. Any other type is yours: the engine gives you the path's ends and the sides they leave and enter by, and you draw the d. Strokes, colours, markers and labels are yours too.

Edge types
Gallery

The built-in types

Flow.Edge draws a Flow.EdgePath by default, whose d is the engine's path for the edge's type. Give the path its stroke: it is unfilled, and has no colour of its own.

<Flow.Edges<Types>>
    {(edge) => (
        <Flow.Edge<Types> edge={edge}>
            <Flow.EdgePath className="edge" />
        </Flow.Edge>
    )}
</Flow.Edges>
const edges: FlowEdgeInput<Types>[] = [
    { id: "a-b", source: "a", target: "b" }, // bezier
    { id: "a-c", source: "a", target: "c", type: "simplebezier" },
    { id: "b-c", source: "b", target: "c", type: "smoothstep" },
    { id: "c-d", source: "c", target: "d", type: "step" },
    { id: "d-e", source: "d", target: "e", type: "straight" },
];

simplebezier is one curve whose control points stand halfway between the ends, each on its own end's axis: with no least reach, a target behind its source makes it turn back at once, where bezier keeps a loop out of the node (path helpers). Their shape is the root's pathOptions: curvature (bezier, default 0.5), borderRadius (smooth step corners, default 5) and offset (how far a step leaves its node before turning, default 20); simplebezier takes none.

<Flow.Root pathOptions={{ borderRadius: 12, offset: 30 }} … />

The root's pathOptions also take stepPosition (where a step's turn sits between its source, 0, and its target, 1; default 0.5) and centerX / centerY (a turn at a fixed place, in flow coordinates): see the path helpers.

One edge's own shape

An edge's own pathOptions go over the root's, key by key, for that edge only: one flat object with the same keys (curvature, borderRadius, offset, stepPosition, centerX, centerY), each read by the types it applies to. It is part of the document, so it is saved, validated and undone with the edge:

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

The engine draws the edge with the merged options (edge-path-by), so an edge type of your own that reads state.path and calls a path helper can spread them too. In a controlled flow (edges given to the root), an edge whose pathOptions changed is replaced: the sync removes it and adds the new one, keeping its id.

Animated edges

animated: true on an edge (a flag of the document, default false) puts data-animated on its edge, edge-path and edge-label parts. The package animates nothing: the motion is your CSS, so it follows your theme and prefers-reduced-motion:

flow.css
@keyframes flow-dash {
    to {
        stroke-dashoffset: -10;
    }
}
[data-flow-part="edge-path"][data-animated] {
    stroke-dasharray: 6 4;
    animation: flow-dash 0.5s linear infinite;
}
@media (prefers-reduced-motion: reduce) {
    [data-flow-part="edge-path"][data-animated] {
        animation: none;
    }
}

With Tailwind, the variant is data-animated: (data-animated:[stroke-dasharray:6_4]). Turn it on and off from code like any flag:

model.run("edge.set-flags", { edgeId: "b-c", animated: true });

An edge type of your own

Name the type and its data in the registry. The engine gives an app type the bezier path, and its ends: draw your own d from state.path.source and state.path.target (and their sides), read through useEdge:

wave-edge.tsx
import { type EdgePath, Flow, type FlowEdge, type FlowTypes, useEdge } from "@fragiola/flow-react";

interface Types extends FlowTypes {
    readonly nodes: { point: { label: string } };
    readonly edges: { wave: { amplitude: number } };
}

/** A sine wave of `amplitude` across the straight line between the two ends. */
function wavePath({ source, target }: EdgePath, amplitude: number): string {
    const dx = target.x - source.x;
    const dy = target.y - source.y;
    const length = Math.hypot(dx, dy) || 1;
    const nx = -dy / length;
    const ny = dx / length;
    const waves = Math.max(1, Math.round(length / 60));
    const steps = waves * 16;
    const points: string[] = [];
    for (let i = 0; i <= steps; i++) {
        const t = i / steps;
        const offset = Math.sin(t * waves * 2 * Math.PI) * amplitude;
        points.push(`${source.x + dx * t + nx * offset},${source.y + dy * t + ny * offset}`);
    }
    return `M ${points.join(" L ")}`;
}

function WaveEdge({ edge }: { edge: Extract<FlowEdge<Types>, { type?: "wave" }> }) {
    const { state } = useEdge<Types>(edge);
    if (!state.path) return null; // its nodes have no size yet
    return (
        <Flow.Edge<Types> edge={edge}>
            <Flow.EdgePath d={wavePath(state.path, edge.data?.amplitude ?? 8)} className="edge" />
        </Flow.Edge>
    );
}

function renderEdge(edge: FlowEdge<Types>) {
    if (edge.type === "wave") return <WaveEdge edge={edge} />;
    return (
        <Flow.Edge<Types> edge={edge}>
            <Flow.EdgePath className="edge" />
        </Flow.Edge>
    );
}

The core's own path math is public too: bezierPath, smoothStepPath, stepPath and straightPath take the ends and return { path, label }, a start for a path of your own. See Path helpers.

A bigger press target

Several Flow.EdgePaths may share an edge. A wide transparent one under the visible stroke makes the edge easier to click (to select it) without a thicker line:

<Flow.Edge<Types> edge={edge}>
    <Flow.EdgePath className="stroke-transparent stroke-[14px]" />
    <Flow.EdgePath className="edge" />
</Flow.Edge>

Flow.Edge takes presses on its stroke only (pointer-events: visibleStroke): a transparent stroke counts, a fill does not.

Labels

Edge labels
Gallery

Flow.EdgeLabel is HTML centred on the path's label point (halfway along it), in flow coordinates: it pans and zooms with its edge. It may hold anything; a button in it keeps its own clicks.

function renderEdge(edge: FlowEdge<Types>) {
    return (
        <Flow.Edge<Types> edge={edge}>
            <Flow.EdgePath className="edge" />
            <Flow.EdgeLabel className="label">
                {edge.type === "text" && <span>{edge.data?.text}</span>}
                {edge.type === "removable" && <RemoveButton edge={edge} />}
            </Flow.EdgeLabel>
        </Flow.Edge>
    );
}

function RemoveButton({ edge }: { edge: FlowEdge<Types> }) {
    const { model } = useFlow<Types, typeof features>();
    return (
        <button
            type="button"
            aria-label={`Remove the edge from ${edge.source} to ${edge.target}`}
            onClick={() => model.run("edge.remove", { edgeId: edge.id })}
        >
            ×
        </button>
    );
}

Labels render in an HTML layer of Flow.Edges (SVG holds no HTML), so they sit above the edges and below the nodes.

A toolbar on an edge

Flow.EdgeToolbar, inside Flow.Edge, holds your buttons for that edge. Unlike a label it stands outside the zoom, so it keeps its size at any zoom, and by default it shows only while its edge is selected. It is centred on the edge's label point (position="source" or "target" for an end), moved by offset screen pixels, and follows the edge as its nodes move and the view pans or zooms:

Edge labels
Gallery
edge-actions.tsx
import { Flow, type FlowEdge, useFlow } from "@fragiola/flow-react";
import { RefreshCwIcon, Trash2Icon } from "lucide-react";

export function EdgeActions({ edge }: { edge: FlowEdge<Types> }) {
    const { model } = useFlow<Types, typeof features>();
    return (
        <Flow.EdgeToolbar offset={{ x: 0, y: -36 }} role="toolbar" aria-label={`${edge.source} to ${edge.target}: actions`}>
            {edge.type === "status" && (
                <button
                    type="button"
                    aria-label="Flip the status"
                    onClick={() =>
                        model.run("edge.set-data", {
                            edgeId: edge.id,
                            data: { status: edge.data?.status === "failing" ? "passing" : "failing" },
                        })
                    }
                >
                    <RefreshCwIcon />
                </button>
            )}
            <button type="button" aria-label="Remove it" onClick={() => model.run("edge.remove", { edgeId: edge.id })}>
                <Trash2Icon />
            </button>
        </Flow.EdgeToolbar>
    );
}

// in renderEdge: <Flow.Edge edge={edge}><Flow.EdgePath /><EdgeActions edge={edge} /></Flow.Edge>

open shows it whatever the selection (a toolbar always beside an end, position="target"), and edgeId places one outside its edge's group. Its props and state are on the EdgeToolbar page.

Arrowheads

No marker is built in, and there is no MarkerType: markers are plain SVG. Define one in an svg of your own and point the path's markerEnd at it. Flow.EdgePath passes every SVG attribute through (Edge markers).

<svg width="0" height="0" aria-hidden="true">
    <defs>
        <marker id="arrow" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="8" markerHeight="8" orient="auto-start-reverse">
            <path d="M0,0 L10,5 L0,10 z" fill="currentColor" />
        </marker>
    </defs>
</svg>

<Flow.EdgePath markerEnd="url(#arrow)" className="edge text-palette-line" />

Selected edges

With selection(), a selected edge's group and paths carry data-selected. Style it there:

[data-flow-part="edge-path"] {
    stroke: var(--fl-edge-color);
    stroke-width: 1.5px;
}

[data-flow-part="edge-path"][data-selected] {
    stroke: var(--fl-edge-selected);
    stroke-width: 2.5px;
}