v22.18.0 release notes

Diagram

Diagram is a node-based flow editor for workflows, automations and pipelines: nodes drawn by your templates, connected by edges on a canvas that pans and zooms. Nodes are moved, connected and deleted with the mouse, a finger or the keyboard alone, every change is announced to screen readers, and a list of the nodes and their connections is always available. It renders on the server and has no dependency.

View as .md
import { Diagram, DiagramHandle, DiagramNodeTemplate } from '@exhaustive-ui/core/diagram';

A diagram has nodes, each with a unique id, the position of its top left corner in pixels and any data, and edges that connect a source node to a target node. Bind both two-way with [(nodes)] and [(edges)]: the diagram replaces the arrays when the user moves, connects or deletes something.

Without a template, a node shows its data.label (change the field with nodeLabel), with a target handle on its left and a source handle on its right. Drag a node to move it, drag from a handle to another to connect two nodes, drag the background to pan, and hold Ctrl or ⌘ with the wheel, or pinch, to zoom. Click to select, Shift and click to add to the selection, Shift and drag on the background to select with a box, and press Delete to remove the selection. Give the diagram a height: 28rem by default.

Start
Review
Publish
Done
  • Start
    • Connects to Review
    • Connects to Publish
  • Review
    • Connects to Done
  • Publish
    • No outgoing connection
  • Done
    • No outgoing connection
Arrow keys move the node, Shift with the arrow keys moves it further. Space selects it, Shift and Space adds it to the selection. Delete removes it. Its connection handles follow it in the tab order: Enter on a handle lists the nodes it can connect to.

A node is drawn by the template of its type: write <ng-template pDiagramNode="trigger" let-node> for the nodes of type trigger, and <ng-template pDiagramNode let-node> for the nodes without a type. The template receives the node as the implicit variable, its data, its label and the selected and dragging flags. Give the template a size, or set width and height on the node.

Place the connection handles with the pDiagramHandle directive: a source handle starts connections, a target handle receives them, and position puts it on the top, right, bottom or left side. A node without handles can not be connected. The buttons, links and fields of a template work as usual and never start a drag; add the p-diagram-nodrag class to any other element that must not drag the node.

Trigger
Form submitted
Contact form
Action
Create a contact
In the CRM
Action
Send an email
Welcome template
  • Form submitted
    • Connects to Create a contact
    • Connects to Send an email
  • Create a contact
    • No outgoing connection
  • Send an email
    • No outgoing connection
Arrow keys move the node, Shift with the arrow keys moves it further. Space selects it, Shift and Space adds it to the selection. Delete removes it. Its connection handles follow it in the tab order: Enter on a handle lists the nodes it can connect to.

A node can have several handles of the same type: name each one with handleId, and an edge picks them with sourceHandle and targetHandle. Without them, an edge uses the first handle of each type. A handle sits on the middle of its side; move it along the side with a style, such as top: 30%.

isValidConnection receives the source, sourceHandle, target and targetHandle of a connection and tells whether it is allowed. A node is never connected to itself, and the same handles are never connected twice. While a connection is dragged, the handle under the pointer turns green or red; the refused targets are not offered in the keyboard list either. Here, a decision can only lead to an action, and its yes and no handles to one action each. Turn off autoConnect to add the edges yourself from onConnect, for example after asking for a label.

Expense request
Amount over 500?yesno
Ask the manager
Pay
  • Expense request
    • Connects to Amount over 500?
  • Amount over 500?
    • Connects to Ask the manager, yes
  • Ask the manager
    • No outgoing connection
  • Pay
    • No outgoing connection
Arrow keys move the node, Shift with the arrow keys moves it further. Space selects it, Shift and Space adds it to the selection. Delete removes it. Its connection handles follow it in the tab order: Enter on a handle lists the nodes it can connect to.

The type of an edge gives its shape: bezier, a curve, step, right angles, or straight. defaultEdgeType sets it for the edges without a type and for the line drawn while connecting. A label is shown in the middle of the edge and is part of its accessible name. markerEnd draws a filled arrow, arrowclosed by default, an open arrow, or none; animated moves a dash along the edge to show a flow, and stands still when the user prefers reduced motion.

Click an edge or its label to select it, or reach it with Tab after the nodes and press Space; Delete removes it.

Source
Bezier
Step
Straight
  • Source
    • Connects to Bezier, curve
    • Connects to Step, animated
    • Connects to Straight, open arrow
  • Bezier
    • No outgoing connection
  • Step
    • No outgoing connection
  • Straight
    • No outgoing connection
Arrow keys move the node, Shift with the arrow keys moves it further. Space selects it, Shift and Space adds it to the selection. Delete removes it. Its connection handles follow it in the tab order: Enter on a handle lists the nodes it can connect to.

The controls, shown unless showControls is false, zoom in and out, fit the view and show the list of the nodes and their connections. showMinimap adds a map of the whole diagram: click or drag in it to move the view. background draws dots, lines or none, every backgroundGap pixels. With snapToGrid, dragged nodes and arrow key moves snap to a grid of snapGrid pixels.

The wheel scrolls the page, so that a diagram never traps the scroll: zoom with Ctrl or ⌘ and the wheel, a pinch, the controls or the + and - keys on the canvas, or set zoomOnScroll. minZoom and maxZoom limit the zoom. Bind the position and zoom with [(viewport)], and use fitView, zoomIn, zoomOut, zoomTo, setCenter and setViewport from code.

DotsLinesNoneBackground
Commit
Lint
Test
Build
Deploy
Notify
  • Commit
    • Connects to Lint
    • Connects to Test
    • Connects to Build
  • Lint
    • Connects to Deploy
  • Test
    • Connects to Deploy
  • Build
    • Connects to Deploy
  • Deploy
    • Connects to Notify
  • Notify
    • No outgoing connection
Arrow keys move the node, Shift with the arrow keys moves it further. Space selects it, Shift and Space adds it to the selection. Delete removes it. Its connection handles follow it in the tab order: Enter on a handle lists the nodes it can connect to.

The state of a diagram is plain data: nodes, edges and the viewport. toObject() returns the three together, ready for JSON.stringify; set them back to restore the diagram. Save after onNodesChange, onEdgesChange and onViewportChange, which are emitted once a change is done, not during a drag. Keep only the fields you need in data, and merge with your own records when loading.

This demo keeps the diagram in the local storage of the browser: move or connect nodes, save, change them again, then restore.

Idea
Draft
Ship
  • Idea
    • Connects to Draft
  • Draft
    • No outgoing connection
  • Ship
    • No outgoing connection
Arrow keys move the node, Shift with the arrow keys moves it further. Space selects it, Shift and Space adds it to the selection. Delete removes it. Its connection handles follow it in the tab order: Enter on a handle lists the nodes it can connect to.

An order workflow with two node types, a branch on the stock and a loop back after a reorder. layout() places the nodes in layers that follow the edges, from left to right or from top to bottom with direction, using their measured sizes; the edges that close a loop are ignored. The same layered layout is exported as layoutDiagram(nodes, edges, options) to place nodes before the first render, on the server too.

New steps are added by updating nodes; onSelectionChange gives the selected nodes and edges, here shown under the diagram. With readonly, the diagram can still be panned, zoomed and read, but nodes can not be moved, connected or deleted.

Order received
Shop
In stock?
Pick and pack
Warehouse
Ship
Carrier
Notify the customer
Shop
Reorder from supplier
Purchasing
  • Order received
    • Connects to In stock?
  • In stock?
    • Connects to Pick and pack, in stock
    • Connects to Reorder from supplier, out of stock
  • Pick and pack
    • Connects to Ship
  • Ship
    • Connects to Notify the customer
  • Notify the customer
    • No outgoing connection
  • Reorder from supplier
    • Connects to In stock?
Arrow keys move the node, Shift with the arrow keys moves it further. Space selects it, Shift and Space adds it to the selection. Delete removes it. Its connection handles follow it in the tab order: Enter on a handle lists the nodes it can connect to.

Nothing selected

Screen Reader

A diagram is a visual medium, and no ARIA pattern describes a graph. Diagram gives every action a keyboard way and every state a text, and keeps a plain list as the reference: use it to check what a screen reader user gets, and test with your own content.

The diagram is a group named by ariaLabel, ariaLabelledBy or "Diagram". Each node is a focusable group with the role description "node", named by its label, with ", selected" added while it is selected, and described by the keyboard instructions. The nodes come in the tab order of the nodes array, whatever their place on screen, so order the array the way the flow reads. Each source handle follows its node in the tab order: it is a button named "Connect Order received from its right side" that opens the list of the nodes it can connect to. Target handles are hidden from assistive technologies, since they appear in that list. The edges come after the nodes, as toggle buttons with the role description "connection", named "From Order received to Check stock" with their label.

The list of the nodes and their connections is always in the page for screen readers, and the last button of the controls shows it on screen: for each node, the nodes it connects to and the labels of the edges. A polite live region announces the moves, connections, selections, deletions and zoom changes made with the keyboard and the controls. The minimap, the background and the edge labels are hidden from assistive technologies, as they repeat what the list and the edge names say. Every text can be translated with labels; name the source handles of a custom node with ariaLabel when the side is not meaningful, such as "Connect Amount over 500 when yes".

The dash of animated edges stands still and the transitions are removed when the user prefers reduced motion. The edges and the handles have a contrast of at least 3:1 with the canvas in the four presets. A node that gets the focus is panned into view.

<h2 id="flow-title">Order workflow</h2>
<p-diagram [(nodes)]="nodes" [(edges)]="edges" ariaLabelledBy="flow-title" [labels]="{ moved: '{0} déplacé en {1}, {2}', connected: '{0} relié à {1}' }" />

Node Keyboard Support

KeyFunction
tabMoves focus to the next node, its source handles, then the edges and the controls.
arrow keysMove the focused node by keyboardStep pixels, 10 by default, or by the snap grid; when the node is selected, all the selected nodes move. The new position is announced.
shift + arrow keysMove five times further.
space / enterSelects the focused node, or takes it out of the selection.
shift + spaceAdds the focused node to the selection.
delete / backspaceDeletes the focused node and its edges, or the whole selection when the node is selected, then moves the focus to the next node.
escapeEmpties the selection.

Source Handle and Connect List Keyboard Support

KeyFunction
enter / spaceOn a source handle, opens the list of the nodes it can connect to, without the targets refused by isValidConnection, and moves the focus to it. When no node can be connected, it is announced.
down arrow / up arrowMoves to the next or previous target of the list.
home / endMoves to the first or last target.
enter / spaceIn the list, connects the node to the active target, announces it and brings the focus back to the handle.
escape / tabCloses the list and brings the focus back to the handle.

Edge Keyboard Support

KeyFunction
space / enterSelects the edge or takes it out of the selection; with shift, adds it to the selection.
delete / backspaceDeletes the edge, or the selection when the edge is selected.
escapeEmpties the selection.

Canvas Keyboard Support

The canvas gets the focus when its background is clicked; it is not in the tab order.

KeyFunction
arrow keysPan the view, further with shift.
+ / -Zoom in and out.
delete / backspaceDeletes the selection.
space + dragPans the view while the pointer is over the diagram, even over a node.

This first version focuses on the editing of small and medium flows. It does not have yet:

  • undo and redo, copy and paste, or grouping nodes into sub-flows;
  • an edge path editor, edge templates, or edges that avoid the nodes: edges are drawn straight, as a curve or with right angles between their handles;
  • a layout beyond the layered layout() helper: no force-directed or orthogonal layout, and nodes may overlap after a layout when their sizes vary a lot within a layer;
  • rendering of only the visible nodes: every node and edge is in the page, which stays fluid up to about 500 nodes; during a drag, only the edges of the moved nodes get a new path, but larger diagrams are slower to render;
  • node resizing by the user, and node rotation;
  • arrow keys that follow the writing direction: they move nodes left and right on screen, also in right-to-left pages;
  • a structure that screen readers navigate as a graph: they read the nodes, handles and edges in order, and the list of nodes and connections is the reference for the whole flow.