Fragiola

Guides

Sub-flows

Put nodes inside other nodes, keep them inside their parent, let the parent grow, and move a node into a group when it is dropped there.

subflows() lets a node live in another one: a node with a parentId has its position relative to its parent, moves with it, and is removed with it. With extent: "parent" it stays inside its parent's box; with expandParent its parent grows to keep it. What a group looks like, and when a node joins or leaves one, are yours.

Sub-flows
Gallery

A parent and its children

import { selection } from "@fragiola/flow-react/selection";
import { drag } from "@fragiola/flow-react/drag";
import { subflows } from "@fragiola/flow-react/subflows";

interface Types extends FlowTypes {
    readonly nodes: {
        group: { label: string };
        item: { label: string };
    };
}

const features = [selection(), drag(), subflows()] as const;

const nodes: FlowNodeInput<Types>[] = [
    {
        id: "frontend",
        type: "group",
        position: { x: 0, y: 0 },
        width: 260,
        height: 220,
        data: { label: "Frontend" },
    },
    {
        id: "ui",
        type: "item",
        parentId: "frontend",
        extent: "parent",
        position: { x: 20, y: 50 }, // relative to the frontend group
        data: { label: "UI" },
    },
];

Without subflows(), parentId and extent are validation issues: the document is refused, naming the feature. A child may come before its parent in nodes.

Render them flat

Nodes are rendered flat: a child's element is not inside its parent's. Flow.Nodes gives every node to your function, and each Flow.Node is placed at its absolute position (its parents' positions added). Draw the group first in the list so it sits under its children, and give it the size the document gives it:

function renderNode(node: FlowNode<Types>) {
    if (node.type === "group") {
        return (
            <Flow.Node<Types> node={node} className="rounded-lg border border-dashed bg-palette-soft/40">
                <span className="p-2 text-xs">{node.data.label}</span>
            </Flow.Node>
        );
    }
    return (
        <Flow.Node<Types> node={node} className="node">
            {node.data.label}
            <Flow.Handle type="target" side="top" className="handle" />
            <Flow.Handle type="source" side="bottom" className="handle" />
        </Flow.Node>
    );
}

Dragging the group drags its children with it; with extent: "parent", a dragged child stops at its parent's edges (measured sizes during the drag, the document's sizes in the model). extent may also be a rectangle, { x, y, width, height } relative to the parent's top-left corner, to keep a child in one part of its group (Nodes and edges).

Grow the parent instead

expandParent: true makes the parent grow when its child moves or grows past one of its edges, instead of stopping the child. Past the right or bottom edge, the parent grows (when it has a size); past the left or top edge, it moves there and grows, its other children shifted back so nothing else moves on screen:

{ id: "notes", type: "item", parentId: "frontend", expandParent: true, position: { x: 20, y: 160 }, data: { label: "Notes" } }

Drop a node into a group

The model does not decide when a node joins a group: the app does. Listen for the end of a drag (nodes-move), find the group under the dropped node's centre, and run node.set-parent. It keeps the node where it is on screen (its position becomes relative to the new parent), and parentId: null takes it out.

drop-into-groups.tsx
import { useFlow } from "@fragiola/flow-react";
import { useEffect } from "react";

function DropIntoGroups() {
    const { model, engine } = useFlow<Types, typeof features>();
    useEffect(
        () =>
            engine.subscribe((event) => {
                if (event.type !== "nodes-move") return;
                for (const nodeId of event.nodeIds) {
                    const node = model.get("node-by", { id: nodeId });
                    const rect = engine.get("node-rect-by", { nodeId });
                    if (node?.type !== "item" || !rect) continue;
                    const centre = { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
                    const group = model.get("all-nodes").find((candidate) => {
                        if (candidate.type !== "group") return false;
                        const box = engine.get("node-rect-by", { nodeId: candidate.id });
                        return (
                            box !== undefined &&
                            centre.x >= box.x &&
                            centre.x <= box.x + box.width &&
                            centre.y >= box.y &&
                            centre.y <= box.y + box.height
                        );
                    });
                    const parentId = group?.id ?? null;
                    if ((node.parentId ?? null) === parentId) continue;
                    model.run("node.set-parent", {
                        nodeId,
                        parentId,
                        extent: parentId ? "parent" : null,
                    });
                }
            }),
        [model, engine],
    );
    return null;
}

Render <DropIntoGroups /> inside the root. node.set-parent refuses a cycle (a node into its own child).

Read the hierarchy

model.get("node-children-by", { nodeId: "frontend" }); // its children, in drawing order
model.get("node-parent-by", { nodeId: "ui" }); // the frontend group
model.get("absolute-position-by", { nodeId: "ui" }); // { x: 20, y: 50 } plus the parent's position

Removing a parent

node.remove (and the Delete key's selection.remove) removes a parent with its children and every edge they touch, in one step. A child that is not deletable keeps its parent: node.remove refuses, and selection.remove skips both.