Guides
Keyboard and accessibility
Let keyboard users select, move, delete, connect and resize nodes, name every part yourself, and announce what happened in your own words.
keyboard() makes nodes and edges focusable and gives them keys: Tab walks them, the arrows move
nodes, Enter selects, Delete removes, Escape clears. With connection() it draws edges from a
focused handle, and with resize() it resizes from a focused resize handle. The package names
nothing and says nothing aloud: it ships no text, no roles and no aria-label. The names, the
descriptions and the announcements are yours, in your words and your language.
Turn it on
import { keyboard } from "@fragiola/flow-react/keyboard";
import { selection } from "@fragiola/flow-react/selection";
const features = [selection(), drag(), keyboard()] as const;With it, every node and edge gets tabIndex={0} (engine.is("node-focusable", { nodeId })), so
Tab walks them in drawing order, and a node that takes focus is panned into view. On a focused
node or edge:
| key | does |
|---|---|
| Arrow keys | move the selected nodes, or the focused node, by step flow units (default 5), kept inside their parents; one step per press |
| Shift + arrow | move by fastStep (default 20) |
| Enter, Space | select it; with Shift, Ctrl or ⌘, toggle it in the selection |
Delete, Backspace (deleteKeys) | remove the selection (selection.remove), or without selection() the focused node or edge |
| Escape | cancel a gesture under way, then clear the selection |
const features = [selection(), drag(), keyboard({ step: 10, fastStep: 50 })] as const;deleteKeys (default ["Delete", "Backspace"]) are the keys that remove, each a
KeyboardEvent.key or .code; [] turns removing by key off, for a flow whose app removes from a
menu or a button of its own:
const features = [selection(), keyboard({ deleteKeys: ["Delete"] })] as const; // Delete only, Backspace does nothingA node that is not draggable does not move, and one that is not deletable stays. The root's
interactive={false} (or Flow.ToggleInteractive) stops the keys from changing the flow.
Confirm before deleting
keyboard({ beforeDelete }) is asked before a delete key removes anything, with the nodes and the
edges it would remove (a removed node's edges among them). It answers true (remove them),
false (keep everything) or a { nodes, edges } subset (only those), or a promise of one: the key
is taken at once, and when the promise settles what is still there goes, in one step (a node
removed meanwhile is skipped). Your confirmation, a dialog of your app, waits there. With a
beforeDelete, the delete runs as one batch of node.remove and edge.remove, not
selection.remove: a middleware watching selection.remove does not see it.
const features = [
selection(),
keyboard({
beforeDelete: async (nodes, edges) => confirmDelete(nodes.length, edges.length), // your UI
}),
] as const;Focus and the view
keyboard({ panOnFocus: false }) keeps the view still when a node out of it takes focus, or when a
keyboard connection points at a handle of one. A node or
an edge with focusable: false takes no focus (it leaves the tab order), and the root's
nodesFocusable={false} / edgesFocusable={false} take every node's or edge's out:
model.run("node.set-flags", { nodeId: "legend", focusable: false });Draw an edge from the keyboard
With connection() installed beside keyboard(), every connectable handle (of a connectable
node) takes focus too, so Tab reaches a node's handles right after the node. Without
connection() handles stay out of the tab order, so an app that does not connect keeps its tab
order.
import { connection } from "@fragiola/flow-react/connection";
import { keyboard } from "@fragiola/flow-react/keyboard";
const features = [selection(), drag(), connection(), keyboard()] as const;On a focused handle:
| key | does |
|---|---|
| Enter, Space | start a connection from it: the root gets data-connecting, and it points at the nearest handle that accepts it |
| Tab, Arrow Right, Arrow Down | point at the next target, round the list |
| Shift + Tab, Arrow Left, Arrow Up | point at the previous target |
| Enter, Space (again) | connect: edge.add to the target, one undo step |
| Escape | cancel |
The targets are the handles a drag could end at (the connection's mode, connectable nodes and
handles) that model.can("edge.add", …) accepts, nearest first. So a rule of yours
(Connection rules) skips a handle it vetoes, and an existing
edge's handles are not offered again. The list is read again on every step: a handle rendered
while the connection waits is offered on the next Tab, and a rule that starts vetoing a handle
drops it from the next step. The handle pointed at stays pointed at across a step; when it is
gone, the step points at the handle that took its place. Enter runs edge.add, which asks the rules
once more: a veto then ends the connection with connect-cancel { refused: true }. A connection
with no target still starts, and Enter then cancels it.
The handle pointed at carries data-valid and the connection line is drawn to it; the focus stays
on the source handle. Its node is panned into view when the connection starts and after each
step, as a focused node is: centred, at the same zoom, when it is not wholly in view.
keyboard({ panOnFocus: false }) keeps the view still.
It ends by itself when the focus leaves the source handle (a click in a field, a button
focused, the window left), when its node is removed (a delete key while it waits), on a pointer
press (which does nothing else), and on set-interactive false. A held Enter does not connect
twice. Keys pressed anywhere else (a field, a toolbar button) are never taken by it.
An edge's ends move with the pointer (reconnection()).
From the keyboard, a menu of yours on a focused edge runs edge.reconnect with the new ends, one
undo step that the same rules check:
model.run("edge.reconnect", { edgeId, source: "hall", target: "garden" });Name the handles, since a screen reader reads a focused handle by its name:
<Flow.Node<Types> node={node} aria-label={node.data.label} className="node">
{node.data.label}
<Flow.Handle type="target" side="left" aria-label={`${node.data.label} input`} className="handle" />
<Flow.Handle type="source" side="right" aria-label={`${node.data.label} output`} className="handle" />
</Flow.Node>Show the target as you show a valid drop, and the focused handle as any focus:
[data-flow-part="handle"]:focus-visible {
outline: var(--fl-focus-width) solid var(--fl-focus-line);
outline-offset: 2px;
}
[data-flow-part="handle"][data-valid] {
background: var(--fl-valid);
}Resize from the keyboard
With resize() installed beside keyboard(), every resize handle of a NodeResizer takes focus.
The arrows on a focused handle move the sides that handle moves, by step flow units (fastStep
with Shift): Arrow Right on the right side grows the node, Arrow Left shrinks it, and a corner
takes both of its axes. An arrow across the handle (Arrow Up on right), or with Ctrl, Alt or ⌘,
is left to the page. Each press is one node.resize within the resizer's limits (at a limit the
key does nothing), and a resize event with source: "keyboard". See
Resizing nodes to name the handles.
The selection rectangle
With selection() and keyboard(), the rectangle around a box's selection
(Flow.NodesSelection) takes focus too, so a keyboard user
can act on the selection as a whole:
| key | on the focused rectangle |
|---|---|
| the arrows | move every selected node by step (Shift: fastStep), within nodeExtent and each node's extent, as the arrows on a selected node do |
a delete key (deleteKeys) | removes the selection, after beforeDelete (a remove event) |
| Escape | clears the selection: the rectangle goes |
| Enter, Space | nothing |
It has no text: give it an aria-label ("Selected nodes"), as in
Selection. While dragging is off
(set-interactive { drag: false }) the arrows move nothing, and an arrow with Ctrl, Alt or ⌘
is left to the page.
Name every part
A node is an element you render, so you name it. A flow is easier to follow when each node says what it is and each edge says what it joins:
function renderNode(node: FlowNode<Types>) {
return (
<Flow.Node<Types>
node={node}
role="group"
aria-roledescription="node"
aria-label={node.data.label}
className="node"
>
{node.data.label}
<Flow.Handle type="target" side="left" className="handle" />
<Flow.Handle type="source" side="right" className="handle" />
</Flow.Node>
);
}
function renderEdge(edge: FlowEdge<Types>) {
return (
<Flow.Edge<Types>
edge={edge}
role="group"
aria-roledescription="edge"
aria-label={`${edge.source} to ${edge.target}`}
>
<Flow.EdgePath className="edge" />
</Flow.Edge>
);
}Name the root too (role="group" aria-label="Release plan"), and every control button: a
Flow.ZoomIn renders only its children, an icon, so it needs aria-label="Zoom in". See
Accessible names.
Show focus
Focus is DOM focus: style it with :focus-visible.
[data-flow-part="node"]:focus-visible {
outline: var(--fl-focus-width) solid var(--fl-focus-line);
outline-offset: 2px;
}Announce what happened
A screen reader user does not see a node move. The engine tells what happened
(engine.subscribe): nodes-move (with source: "keyboard" or "pointer"), drag-cancel,
connect, connect-cancel, remove, resize, box-select, and more. Turn the events you care
about into sentences and write them into a live region:
import { type FlowEngineEvent, type FlowModel, useFlow } from "@fragiola/flow-react";
import { useEffect, useState } from "react";
type Model = FlowModel<Types, typeof features>;
/** Your words for an event, or nothing for one you keep quiet about. */
function describe(event: FlowEngineEvent, model: Model): string | undefined {
const name = (id: string) => model.get("node-by", { id })?.data.label ?? id;
switch (event.type) {
case "nodes-move":
return `Moved ${event.nodeIds.map(name).join(", ")}.`;
case "connect":
return `Connected ${name(event.source)} to ${name(event.target)}.`;
case "connect-cancel":
return event.refused ? "That connection is not allowed." : undefined;
case "remove":
return `Removed ${event.nodeIds.length} node(s) and ${event.edgeIds.length} edge(s).`;
default:
return undefined;
}
}
export function LiveRegion() {
const { model, engine } = useFlow<Types, typeof features>();
const [message, setMessage] = useState("");
useEffect(
() =>
engine.subscribe((event) => {
const next = describe(event, model);
if (next) setMessage(next);
}),
[model, engine],
);
return (
<p role="status" className="sr-only">
{message}
</p>
);
}Render LiveRegion inside the root (it reads the root's engine through useFlow).
A keyboard connection has more to say: where it starts, which handle it points at, and how it ends.
connect-start, connect and connect-cancel carry input: "keyboard" (or "pointer"), and
connect-target comes on every move of the target, with its place in the list (index, from 0,
of count; -1 of 0 when no handle accepts the connection). connect-start and the first
connect-target come at once, and a live region reads only its last text: say the start, the
first target and the keys in one message. Give describe the node the connection started from,
and add these cases:
// describe(event, model, startedFrom?: string): startedFrom is set only for the first target
case "connect-target": {
const target =
event.nodeId === undefined
? "No handle accepts this connection."
: `Target ${event.index + 1} of ${event.count}: ${name(event.nodeId)}.`;
if (startedFrom === undefined) return target;
const keys = event.nodeId === undefined ? "Escape to cancel." : "Tab for the next, Enter to connect, Escape to cancel.";
return `Connecting from ${name(startedFrom)}. ${target} ${keys}`;
}
case "connect-cancel":
if (event.refused) return "That connection is not allowed.";
return event.input === "keyboard" ? "Connection cancelled." : undefined;
case "resize":
return event.source === "keyboard" ? `${name(event.nodeId)} is ${event.width} by ${event.height}.` : undefined;The listener remembers the start until the first target:
useEffect(() => {
let startedFrom: string | undefined; // a keyboard connection just started, until its first target
return engine.subscribe((event) => {
if (event.type === "connect-start") startedFrom = event.input === "keyboard" ? event.nodeId : undefined;
const next = describe(event, model, startedFrom);
if (event.type === "connect-target") startedFrom = undefined;
if (next) setMessage(next);
});
}, [model, engine]);The announcements example above does it: Tab to a handle and press Enter.