Getting Started with vanilla-diagram-toolbar
<vn-diagram-toolbar> is a diagram's command bar. You write its bar in
markup: the standard commands you want, in your order, each in the style you
pick, beside commands of your own. Each step shows the running example and
its whole source, a complete page you can copy.
What you need. Two scripts from the build's
dist/vanilla-components/latest/ folder: vanilla-diagram.bundle.js and
vanilla-diagram-toolbar.bundle.js, in either order. Start with the
diagram's getting-started guide if
<vn-diagram> is new to you.
1. Choose the commands and their style
The diagram attribute names the diagram by its id. Inside the toolbar,
each <vn-diagram-toolbar-command command="..."> is one standard command
(undo, zoomToFit, delete, exportPng, ...), <vn-diagram-toolbar-swatches>
the colour menu, <vn-diagram-toolbar-connector-type> and
<vn-diagram-toolbar-arrows> the menus that change a selected connector's
type and arrows, and <vn-diagram-toolbar-status> the status text; leave out
what you don't want. variant picks a button's style: ghost (the default),
secondary, primary or danger, and label adds text beside the icon.
Each button turns itself off while its command can't run: select a shape to
turn Delete and the colour menu on.
A toolbar with nothing inside draws the default bar, every standard command.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Choose the commands and their style</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: grid; grid-template-rows: auto 1fr; gap: 8px; box-sizing: border-box; padding: 8px; }
vn-diagram { min-height: 0; }
</style>
</head>
<body>
<!-- The bar is written in markup: the status text, then only the
standard commands this page wants, in its order. `variant` picks a
button's style: ghost (the default), secondary, primary or danger.
A `label` puts text beside the icon. With no item inside, the
toolbar would draw its default bar. -->
<vn-diagram-toolbar diagram="flow">
<vn-diagram-toolbar-status></vn-diagram-toolbar-status>
<vn-diagram-toolbar-command command="undo"></vn-diagram-toolbar-command>
<vn-diagram-toolbar-command command="redo"></vn-diagram-toolbar-command>
<vn-diagram-toolbar-separator></vn-diagram-toolbar-separator>
<vn-diagram-toolbar-command command="zoomToFit" label="Fit" variant="secondary"></vn-diagram-toolbar-command>
<vn-diagram-toolbar-swatches></vn-diagram-toolbar-swatches>
<vn-diagram-toolbar-command command="delete" label="Delete" variant="danger"></vn-diagram-toolbar-command>
<vn-diagram-toolbar-command command="exportPng" label="Export PNG" variant="primary"></vn-diagram-toolbar-command>
</vn-diagram-toolbar>
<vn-diagram id="flow"></vn-diagram>
<script src="path/to/vanilla-diagram/vanilla-diagram.bundle.js"></script>
<!-- The toolbar is a component of its own, loaded beside the diagram. -->
<script src="path/to/vanilla-diagram-toolbar/vanilla-diagram-toolbar.bundle.js"></script>
<script type="module">
await window.VanillaDiagramReady;
const diagram = document.getElementById('flow');
await diagram.ready();
const nodes = [
{ id: 'start', symbol: 'ellipse', x: 0, y: 0, width: 120, height: 72, text: 'Start', swatch: 'col3' },
{ id: 'work', symbol: 'rounded-rectangle', x: 200, y: 6, width: 140, height: 60, text: 'Do the work' },
{ id: 'done', symbol: 'ellipse', x: 420, y: 0, width: 120, height: 72, text: 'Done', swatch: 'col2' },
];
const connectors = [
{ id: 'c1', type: 'straight', from: { node: 'start', port: 'right' }, to: { node: 'work', port: 'left' } },
{ id: 'c2', type: 'straight', from: { node: 'work', port: 'right' }, to: { node: 'done', port: 'left' } },
];
diagram.initializeDiagram();
diagram.setDataManager(new VanillaDiagramStaticDataManager({ nodes, connectors }));
await diagram.loadDiagramAsync();
// Each button turns itself off when its command can't run: select a
// shape to enable Delete and the colour menu.
const toolbar = document.querySelector('vn-diagram-toolbar');
toolbar.setStatusMessage('Select a shape, then delete it or pick a colour.');
diagram.addEventListener('vn-diagram-changed', (e) => toolbar.setStatusMessage(e.detail.command.label));
</script>
</body>
</html>
2. A command of your own
Any other command name is yours. registerCommand(name, fn) gives it a
handler, called with the diagram; without one, listen to the toolbar's
vn-diagram-toolbar-command event, which every command fires first and a
listener can cancel. registerIcon() adds an icon. disabled-when and
show-when read the toolbar's state: here Save stays off until there is
something to undo, and Duplicate shows only while a shape is selected.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>A command of your own</title>
<style>
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body { display: grid; grid-template-rows: auto 1fr; gap: 8px; box-sizing: border-box; padding: 8px; }
vn-diagram { min-height: 0; }
</style>
</head>
<body>
<!-- Two commands of the page's own beside a standard one. `save` has a
handler (registerCommand() below); `duplicate-shape` has none: the
page handles its event. disabled-when and show-when read the
toolbar's state: here, whether a shape is selected. -->
<vn-diagram-toolbar diagram="flow">
<vn-diagram-toolbar-status></vn-diagram-toolbar-status>
<vn-diagram-toolbar-command command="duplicate-shape" label="Duplicate" icon="copy"
show-when="hasSelectedNode"></vn-diagram-toolbar-command>
<vn-diagram-toolbar-command command="undo"></vn-diagram-toolbar-command>
<vn-diagram-toolbar-command command="save" label="Save" icon="save" variant="primary"
disabled-when="!canUndo"></vn-diagram-toolbar-command>
</vn-diagram-toolbar>
<vn-diagram id="flow"></vn-diagram>
<script src="path/to/vanilla-diagram/vanilla-diagram.bundle.js"></script>
<script src="path/to/vanilla-diagram-toolbar/vanilla-diagram-toolbar.bundle.js"></script>
<script type="module">
await window.VanillaDiagramReady;
const diagram = document.getElementById('flow');
await diagram.ready();
diagram.initializeDiagram();
diagram.setDataManager(new VanillaDiagramStaticDataManager({
nodes: [
{ id: 'a', symbol: 'rectangle', x: 0, y: 0, width: 120, height: 60, text: 'Edit me' },
{ id: 'b', symbol: 'diamond', x: 200, y: -10, width: 100, height: 80, text: 'Then save' },
],
connectors: [],
}));
await diagram.loadDiagramAsync();
const toolbar = document.querySelector('vn-diagram-toolbar');
// Icons are SVG path data in a 16 x 16 box, stroked in the button's colour.
VanillaDiagramToolbarElement.registerIcon('save', ['M3 2.5h8l2.5 2.5v8.5h-10.5Z', 'M5 2.5v3.5h5V2.5', 'M5 13.5v-4h6v4']);
VanillaDiagramToolbarElement.registerIcon('copy', ['M5.5 5.5h8v8h-8Z', 'M10.5 5.5v-3h-8v8h3']);
// A handler: runs after the cancelable command event, with the diagram.
// Saving here keeps the drawing and clears the undo history, so Save
// turns itself off again until the next change.
toolbar.registerCommand('save', (d) => {
console.info('A real page would send this to its server:', d.toJSON());
d.clearHistory();
toolbar.setStatusMessage('Saved ' + d.getNodeCount() + ' shapes.', 'success');
});
// No handler: the event is the command. Duplicate the selected shapes.
toolbar.addEventListener('vn-diagram-toolbar-command', (e) => {
if (e.detail.command === 'duplicate-shape') e.detail.diagram.duplicate();
});
toolbar.setStatusMessage('Move a shape: Save turns on. Select one: Duplicate shows.');
</script>
</body>
</html>
Next steps
- The README lists the elements, their attributes, the state fields and the standard commands.
- The process-flowchart-js sample writes its bar in markup with a Reset command of its own.