Fragiola

Guides

Selection

Select nodes and edges with a click, a modifier click or a box, read the selection anywhere in the root, and change it from code.

selection() adds the selection to the document: a selected node or edge has selected: true, so the selection is saved, restored and undone like any other change. Its engine side selects on a click, adds with a modifier and draws a box. What a selected node looks like, and what the app does with the selection (a panel, a toolbar, a Delete button), is yours.

Selection and keyboard
Gallery

Turn it on

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

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

<Flow.Root<Types, typeof features> features={features} defaultNodes={nodes} defaultEdges={edges}>
    {/* … */}
    <Flow.SelectionBox className="border border-(--fl-selection-line) bg-(--fl-selection-bg)" />
</Flow.Root>;
  • A click on a node or an edge selects it alone; a click on the canvas clears the selection.
  • Shift, Ctrl or ⌘ with a click toggles it in the selection (multiple: true, the default; the keys are multiKey).
  • Shift with a drag on the canvas draws a box (the key is boxKey), rendered by Flow.SelectionBox (in screen pixels, so outside Flow.Viewport). Releasing it selects the nodes it holds and the edges between them; Escape puts the selection back.
  • Pressing a selected node and dragging it (with drag()) moves the whole selection.

The options

optiondefaultdoes
boxMode"full"a box takes the nodes it holds whole ("full") or every node it touches ("partial")
multipletruea modifier click toggles the item in the selection; false: a click always selects one
boxOnDragfalseany drag on the canvas draws a box, instead of Shift + drag (the canvas then never pans by dragging)
multiKey["Meta", "Control", "Shift"]the keys that, held with a click, toggle the item in the selection: one key, several (any of them held) or null for none
boxKey"Shift"the key that, held with a drag on the canvas, draws a box; null leaves the box to boxOnDrag
rect"box"when the selection rectangle shows: after a box selected two or more nodes ("box"), or whenever two or more are selected ("always")
const features = [selection({ boxMode: "partial", boxOnDrag: true }), drag()] as const;

A key is a KeyboardEvent.key ("Alt", "Meta", "a") or .code ("KeyA"); Meta, Control, Alt and Shift are read from the click itself, any other key while it is held in the root's document. So Shift draws the box only and Ctrl or ⌘ toggle:

const features = [selection({ multiKey: ["Meta", "Control"], boxKey: "Shift" }), drag()] as const;

A held panActivationKey (Space by default) pans instead of selecting (The viewport).

Style what is selected

A selected node, edge, edge path, label and minimap node carries data-selected; the root carries data-selecting while a box is drawn.

<Flow.Node<Types>
    node={node}
    className="border border-(--fl-node-line) data-selected:border-(--fl-edge-selected) data-selected:ring-2"
>
    {node.data.label}
</Flow.Node>

Read the selection

useSelection re-renders its component when the selection changes, anywhere inside the root:

import { useSelection } from "@fragiola/flow-react";

function SelectionStatus() {
    const selected = useSelection<Types>();
    if (selected.nodes.length === 0 && selected.edges.length === 0) {
        return <p role="status">Nothing selected</p>;
    }
    return (
        <p role="status">
            {selected.nodes.length} nodes, {selected.edges.length} edges selected
        </p>
    );
}

Outside a root, or without React, read the model: model.get("selected-nodes"), model.get("selected-edges"), and model.is("node-selected", { nodeId }).

The selection rectangle

After a box selected two or more nodes, the engine holds the rectangle around them (engine.get("selection-rect"), a flow Rect), until the selection changes another way (a click, a modifier click, selection.set). Flow.NodesSelection draws it: render it inside Flow.Viewport, after Flow.Nodes (it is in flow coordinates, above the nodes), and give it its look. A press on it drags the whole selection, with the drag's own threshold, snap, extent and auto-pan; its context menu is the selection's. While no rectangle is held it renders nothing.

// the rectangle for any selection of two or more nodes, not only a box's
const features = [selection({ rect: "always" }), drag()] as const;

A drag by the rectangle is told by its own events instead of drag-start: selection-drag-start, selection-drag at each step and selection-drag-end at the drop (after its nodes-move), each with the nodeIds it drags, the client point and the flow point. A cancelled one (Escape, cancel-gesture) puts the nodes back and is told as drag-cancel, like a node's.

<Flow.Root
    features={features}
    onSelectionDragStart={(event) => setMoving(event.nodeIds)}
    onSelectionDragStop={() => setMoving([])}
    onNodeDragCancel={() => setMoving([])}
>
    <Flow.Viewport>
        <Flow.Nodes>{(node) => <Flow.Node node={node} />}</Flow.Nodes>
        <Flow.NodesSelection className="rounded border border-blue-500 bg-blue-500/5 data-dragging:cursor-grabbing" />
    </Flow.Viewport>
</Flow.Root>

With keyboard() installed, the rectangle takes focus (tabindex="0", from the engine), and its keys are a focused selected node's: the arrows move the whole selection by the keyboard's step (Shift: fastStep), within the extent and the snap a node's arrows keep; a delete key (deleteKeys) removes the selection (after beforeDelete, told as remove); Escape clears the selection. Enter and Space do nothing on it. Name it, since it has no text of its own:

<Flow.NodesSelection
    aria-label="Selected nodes"
    className="rounded border border-blue-500 bg-blue-500/5 focus-visible:outline-2"
/>

See NodesSelection and Keyboard and accessibility.

A toolbar for the selection

Flow.NodeToolbar with nodeIds stands beside several nodes' bounds, outside the zoom. Pass the selected nodes' ids: by default it shows while every listed node is selected, so it follows the selection as it grows, shrinks and moves. Render it inside the root, outside Flow.Nodes:

Node toolbar
Gallery
selection-toolbar.tsx
import { Flow, useFlow, useSelection } from "@fragiola/flow-react";

export function SelectionToolbar() {
    const { model } = useFlow<Types, typeof features>();
    const selected = useSelection<Types>();
    if (selected.nodes.length < 2) return null;
    const nodeIds = selected.nodes.map((node) => node.id);
    return (
        <Flow.NodeToolbar nodeIds={nodeIds} side="bottom" role="toolbar" aria-label={`${nodeIds.length} nodes: actions`}>
            <button type="button" onClick={() => model.run("selection.remove", {})}>
                Delete them
            </button>
        </Flow.NodeToolbar>
    );
}

A node's own toolbar still shows while it is selected; give the selection's another side (here below the nodes), or pass each node's toolbar open from your own rule. A toolbar for a fixed group of nodes (a lane, a cluster) takes their ids the same way and shows only while all of them are selected. See NodeToolbar.

Clicks, double clicks and context menus

The root tells what a user clicked: onNodeClick, onEdgeClick, onPaneClick, their double-click and context-menu callbacks, and onSelectionContextMenu for a right click on one of several selected nodes or on the selection rectangle. Each gets the event: the node's or edge's id, the flow point (point), the client point (client) and the DOM event (event), whose default you prevent to show your own menu. A click is the engine's own tap: a press released within nodeClickDistance (on the pane paneClickDistance, both 3 pixels) that started no gesture, so the click that ends a drag or a pan is never one. A pan that stayed within the pane's distance is still a click (the few pixels it moved stay), and a click on a handle is not its node's. It comes after the click selected. Clicks are told while the flow is locked too: they change nothing then.

<Flow.Root
    features={features}
    onNodeDoubleClick={(event) => rename(event.nodeId)}
    onNodeContextMenu={(event) => {
        event.event.preventDefault();
        openMenu(event.nodeId, event.client);
    }}
>
    {/* … */}
</Flow.Root>
Context menu
Gallery

Drag without selecting

A drag of a node that is not selected selects it first. With drag({ selectOnDrag: false }) it is dragged alone and the selection stays as it was:

const features = [selection(), drag({ selectOnDrag: false })] as const;

engine.run("set-interactive", { select: false }) stops selecting altogether (clicks, the box, Enter and Space) while dragging goes on; Flow.ToggleInteractive switches dragging, connecting and selecting together.

Change it from code

The selection is three commands, so a button, a shortcut or an assistant changes it the way a click does:

model.run("selection.set", { nodeIds: ["build", "review"] }); // replace
model.run("selection.set", { nodeIds: ["docs"], mode: "add" });
model.run("selection.set", { edgeIds: ["build-docs"], mode: "toggle" });
model.run("selection.clear");
model.run("selection.remove"); // remove what is selected and deletable, with their edges

selection.set refuses an id that is not selectable (selectable: false) or does not exist. selection.remove is what the Delete key runs: it skips what is not deletable and removes the rest in one step.

Nodes that cannot be selected

selectable: false on a node or an edge keeps it out of every selection: a click and a box skip it, and selection.set refuses it. A node made unselectable while selected is deselected.

model.run("node.set-flags", { nodeId: "legend", selectable: false });