Flow
A canvas of nodes joined by animated links, with pan, zoom, drag and a keyboard for all three.
Demo
Usage
import { Flow, type FlowEdge, type FlowNode, type FlowViewport } from '@tuttn/slant'
const nodes: FlowNode[] = [
{ id: 'build', label: 'Build', tone: 'success', position: { x: 0, y: 0 } },
{ id: 'test', label: 'Test', description: '1 flaky', tone: 'warning', position: { x: 240, y: 0 } },
{ id: 'deploy', label: 'Deploy', draggable: false, position: { x: 480, y: 0 } },
]
const edges: FlowEdge[] = [
{ id: 'build-test', from: 'build', to: 'test', label: 'on success', animated: true },
{ id: 'test-deploy', from: 'test', to: 'deploy', dashed: true },
]
// Uncontrolled: the flow keeps its own nodes, selection and view, and fits
// every node into view once it has measured them.
<Flow label="Release pipeline" defaultNodes={nodes} edges={edges} />
// Controlled: you hold the nodes (every drag or arrow key reports the whole
// list), the selection and, if you like, the view.
const [items, setItems] = useState(nodes)
const [selected, setSelected] = useState<string | null>(null)
const [view, setView] = useState<FlowViewport>({ x: 40, y: 80, zoom: 1 })
<Flow label="Release pipeline" nodes={items} onNodesChange={setItems} edges={edges}
selected={selected} onSelectedChange={setSelected} viewport={view} onViewportChange={setView} />
// Read-only, filling a Workbench region, without the zoom cluster.
<Flow label="Release pipeline" defaultNodes={nodes} edges={edges} readOnly variant="plain" controls={false} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
nodes | readonly FlowNode[] | — | The nodes, when controlled: pair it with `onNodesChange`, which reports every move. |
defaultNodes | readonly FlowNode[] | [] | The nodes the flow starts with, when uncontrolled. |
onNodesChange | (nodes: FlowNode[]) => void | — | Called with the whole list whenever a node moves, by a drag or the arrow keys. |
edges | readonly FlowEdge[] | [] | The links between nodes. Edges that name a missing node are left out. |
selected | string | null | — | The selected node's id, or null for none, when controlled. |
defaultSelected | string | null | null | The node selected first, when uncontrolled. |
onSelectedChange | (id: string | null) => void | — | Called with a node's id when it is clicked or pressed, and with null when the selection clears. |
viewport | FlowViewport | — | The pan and zoom, when controlled. |
defaultViewport | FlowViewport | every node fitted into view, once they are measured | The pan and zoom the flow starts with, when uncontrolled. |
onViewportChange | (viewport: FlowViewport) => void | — | Called with the new pan and zoom whenever the view moves. |
label | string | — | The canvas's accessible name; it also names the text outline. |
minZoom | number | 0.25 | The smallest zoom the controls, keys and wheel reach. |
maxZoom | number | 2 | The largest zoom the controls, keys and wheel reach. |
step | number | 10 | Canvas units an arrow key moves the focused node; Shift moves five times as far. |
readOnly | boolean | false | Keeps every node in place; selection, pan and zoom still work. |
controls | boolean | true | Shows the zoom cluster (zoom out, the level, zoom in, fit to view) in the inline-end bottom corner. |
variant | 'framed' | 'plain' | 'framed' | `framed` draws a hairline border with the panel radius and a default height; `plain` has neither and fills its container, for a Workbench region. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the canvas element (role="group"). Give it a height with style or a class; framed defaults to 360px, plain fills its container. |
Data
A node is { id, position, label, text?, description?, icon?, tone?, draggable? }, its position in canvas units (px at 100%) from its top-left corner; text names it when the label is markup, and draggable: false pins it. An edge is { id, from, to, label?, directed?, dashed?, animated?, tone? }: directed (the default) draws an arrowhead at to, animated dashes travel from from to to, and an edge naming a missing node is left out. A viewport is { x, y, zoom }: where the canvas origin sits in the element, in px, and the scale.
Keyboard
The canvas and every node are tab stops, in that order, then the zoom controls. On a node, the arrow keys move it by step canvas units (Shift: five times as far), and the view follows it; Enter or Space selects it, and Escape clears the selection. On the canvas, the arrows pan by 40px (Shift: 160px). Anywhere inside, + or = zooms in and − out by 1.2× around the centre, 0 goes back to 100% and Shift+1 fits every node into view. A node that can't move (readOnly, or draggable: false) pans with the arrows instead. Tabbing to a node outside the view pans it in.
Pointer
Dragging the background pans; dragging a node moves it, by the pointer's travel divided by the zoom. A press that moves less than 3px is a click: on a node it selects, on the background it clears. The wheel pans (Shift turns it sideways), and with Ctrl or Cmd, which a trackpad pinch sends, it zooms around the pointer. The wheel listener is native and not passive, so the page doesn't scroll under the canvas.
Screen readers
The canvas is a group named by label with the role description "canvas" and a hint about the keys. Each node is a toggle button with the role description "node", pressed while selected, named by its label (or text) and described by its description and its links. After the drawing comes a visually hidden list, named by the label and a count of nodes and links, with one item per node: what it links to and what links to it, with edge labels in brackets; an undirected edge links both ways. A move made with the keys is announced politely once the keys stop: "Test moved to 480, 90".
Drawing
The canvas is physical, like an SVG: canvas x grows to the right, nothing mirrors under dir="rtl", and the arrow keys stay physical. Edges are Bézier curves from the side of one node that faces the other; ports and the dot grid are slanted pixels. Nodes are measured as they render (the server lays them out at about 160 × 48), and the flow fits them into view once measured unless you give it a viewport or defaultViewport. Under reduced motion animated edges stand still, dashed, which is how they rest.