UI layer
ui.attach(chart, parts) lays menus, icon buttons and handles over a chart you have already made. Each part acts through external control: it fires the edits your spec names and writes values with set, so constraints and undo apply. The layer is HTML over the drawing. It works the same on the SVG and canvas renderers, and it is not part of the spec.
API reference
ui.attach(chart, parts, options?)
Lay parts over a chart. The layer is one HTML element over the chart’s drawing, added once the chart has drawn. Parts act through external control, so the spec stays the statement of what can change.
const layer = ui.attach(chart, [ui.contextMenu(), ui.handles()]);
layer.detach();| Option | Type · default | Description |
|---|---|---|
chart | Chart | What Elicit(spec) or dataset.chart(spec) returns. |
parts | UiPart | | The parts to mount. Nested arrays are flattened. |
options.motion | boolean | defaultfalse | Fade icons, handles and menus in as they appear, and icons and handles out as they go. true takes 150 ms. Off when the reader’s system asks for reduced motion. |
{ detach() }. chart.destroy() detaches the layer too.
ui.contextMenu(options?)
A menu on right-click, over a mark or over empty space. With no items, it offers what the spec’s named edits allow: add on empty space, a declared value edit’s choices and remove on a row.
ui.contextMenu({ items: [{ edit: "add" }, { channel: "fill" }, { separator: true }, { edit: "delete" }] })
ui.contextMenu({ items: ({ view, point }) => [ … ] })| Option | Type · default | Description |
|---|---|---|
items | MenuEntry[] | defaultfrom the named edits | The entries, or a function of what was right-clicked: { view, point }, where view is null over empty space. |
entry.edit | string | The name of a declared edit. Fires on the row under the pointer, or at the pointer for an edit that adds a row. |
entry.channel | string | A channel of the mark: one row per value, the current one checked. With edit naming an edit that adds a row, each value adds a row with that value. |
entry.mark | stringdefaultthe mark under the pointer | The id of the mark the channel is on. |
entry.values | any[]defaultthe scale's domain | The choices, for a channel whose scale lists none. |
entry.label | string | defaultby kind | The row’s text, or the heading over a channel’s values. false drops the heading. |
entry.icon · entry.tone | icon · "danger"defaultby kind | An icon before the label; the removing colour. |
entry.on | "plane" | defaultwhere it applies | Show the entry only over empty space, or only on the pieces a scene query matches: { mark: "note" }. |
entry.onSelect | (target) => | A row of your own, with label. |
entry.separator · entry.heading | true · string | A rule between groups; a caption over the rows that follow. |
ui.actions(options?)
Icon buttons beside the drawn piece the pointer is near. An item drives a named edit, or runs your own function.
ui.actions({ on: { mark: "note" }, items: [{ edit: "delete", icon: "trash", label: "Remove", tone: "danger" }] })| Option | Type · default | Description |
|---|---|---|
on | { mark?, defaultevery row's pieces | Which drawn pieces get the icons. |
items | ActionItem[]default[] | Each has icon and label, then edit (a named edit) or onClick(view). show(view) leaves an icon out for some pieces; tone: "danger" marks one that removes. |
reach | numberdefault24 | How near the pointer must come, in pixels. |
placement | "top-right" | default"top-right" | The side or corner of the piece the icons sit on. |
An item whose edit can’t run on that row (another stage, a locked row) is left out.
ui.handles(options?)
Grips on a mark’s box: resize it along the sides it sizes from data, turn it, or move it. Each drag is one undo step.
width: { field: "w", scale: { range: [70, 260] } } // on the mark
ui.handles({ on: { mark: "note" } })
ui.handles({ on: { mark: "note" }, rotate: true, move: true })| Option | Type · default | Description |
|---|---|---|
on | { mark?, defaultevery row of the first mark that sizes from data | Which mark and rows get grips, as a scene query. A row gets them when any piece of it matches; the grips sit on its body. |
resize | booleandefaulttrue | Grips on the left and right where the mark binds width, on the top and bottom where it binds height. A grip moves its own side and holds the opposite one still. |
rotate | booleandefaultfalse | A grip above the box that turns it about its centre, writing the mark’s angle field. Shift snaps to 15°. |
move | booleandefaultfalse | A grip below the box that moves it, writing the mark’s x and y fields. |
Where a row already has a handle in the middle of a side (a port), the grip sits at the corner after it.
ui.custom(mount)
A part of your own. mount(ctx) builds it in the layer and may return a function that takes it down.
ui.custom((ctx) => { … return () => { … }; })| Option | Type | Description |
|---|---|---|
ctx.chart | Chart | The chart the layer is over. |
ctx.layer | HTMLElement | The layer. It ignores the pointer; a control you add opts in with its own pointer-events. |
ctx.place(el, | → () => | Keep el on an anchor: a point in plot pixels, a scene view, or a function read on each render. placement is { at, offset, dx, dy }, or a function returning one. An anchor of null hides the element. Returns its removal. |
ctx.refresh() | void | Re-place every element now, after you change what one is anchored to. |
ctx.button(options) | → HTMLButtonElement | An icon button: { icon, label, tone, onClick }. label is its accessible name. |
ctx.menu(items, | → { element, | Open a menu at a point in plot pixels. Rows are { label, icon, swatch, checked, disabled, tone, onSelect }, { separator: true } or { heading }. |
ctx.icon(icon) | → Element | An icon element from a name, SVG markup or an element. |
ctx.onRender(cb) | → () => | Run after each frame the chart draws. |
ctx.onPointer(cb) | → () => | The pointer over the chart: { type, point, event }. type is "move" (once per frame), "leave" or "menu" (a right-click; call event.preventDefault() to take it). point is in plot pixels. |
ctx.drag(el, | → () => | Make a layer element a drag source: start, move, end, cancel (Escape) and click (a press that never moved). Each step carries { point, dx, dy, event }. |
ctx.capture(handlers) | → () => | Take the chart’s next press for the layer: move, press, cancel. Until you call the returned function, the chart’s own gestures stay out. |
theme.ui · authoring.registerIcon(name, path)
The layer’s tokens, and a way to add an icon name. The tokens are set on the layer as CSS variables named --elicit-ui-*.
theme: { ui: { accent: "#7c3aed", size: 26 } }
authoring.registerIcon("star", "M8 2l1.8 3.9 4.2.5-3.1 2.9.8 4.2L8 11.4l-3.7 2.1.8-4.2L2 6.4l4.2-.5z")| Option | Type · default | Description |
|---|---|---|
surface · ink · muted · border | colourdefault#ffffff · #1f2937 · #6b7280 · #d5dbe5 | A menu's and a button's background; labels and icons; a heading or an unavailable row; edges. |
accent · danger | colourdefault#2563eb · #dc2626 | Hover, focus and the active icon; an action that removes. |
shadow | colourdefaultrgba(15, | Under a menu and a button. |
radius · size · gap · fontSize | pxdefault8 · 22 · 6 · 12 | A menu's corner; an icon button's side; the space between a mark and its icons; a menu's text. |
Attach and detach
Make the chart, then attach the parts you want. ui.contextMenu, ui.actions and ui.handles work on any chart. ui.network is a preset for node-link charts. ui.custom is a part of your own.
const chart = Elicit(spec);const layer = ui.attach(chart, [ui.contextMenu(), ui.handles()]);layer.detach(); // take the parts down; chart.destroy() does it too
The layer has no JSON form. A chart built with compile(json) takes the same ui.attach call.
Fade in and out
Pass { motion: true } to fade the parts in as they appear. Icons and handles fade out as they go, and menus fade in. { motion: { duration } } sets the time in milliseconds (150 by default). With reduced motion turned on in the reader's system, they appear at once.
ui.attach(chart, [ui.actions(), ui.handles()], { motion: true });
The chart's own feedback fades with effects.motion. See Fade in and out.
What a part can change
A part writes in two ways, and which one depends on what changes.
A row or a link. Adding or removing a row, connecting, re-pointing and dragging run an edit the spec declares. Give that edit a name: a part can only drive a named edit, and it logs a warning for one it needs that has none.
A value in a row. A category, a colour, a width or a position needs no edit. The part writes it with chart.set. Where the spec declares a set() edit on that channel, the part uses it, so the edit's own constrain applies.
Both routes run the dataset's constraints and the lock, and each action is one undo step.
An edit placed on a mark also answers the pointer. For an edit only the UI should fire, give it gesture: "commit", which is a gesture no pointer makes:
edits: [create({ name: "add", gesture: "commit" }), // from a menu, never from a clickremove({ name: "delete", gesture: "commit" }),]
A part names its edit, so the edit's when is not asked. when chooses between pointer gestures.
Icons beside a mark
ui.actions({ on, items }) shows icon buttons beside the drawn piece the pointer comes near. on says which pieces, as a scene query: { mark: "note" }. reach is how near, in pixels (default 24), and placement is the corner or side the icons sit on.
The icons stay while the pointer travels to them, and for a moment after it leaves the piece. The piece is lit as the chart lights a hovered mark. A line's icons sit beside the point the pointer met it at.
An item with edit drives that edit on the row. A click edit fires at once. A drag edit is dragged from its button; a click on the button arms it, and the next click on the chart is the drop. Escape cancels. An item with onClick is a button of your own.
Move and remove icons on a note
The grip drives the note's move edit; the bin fires a remove that no click on the note reaches.
const chart = Elicit({width: 460, height: 280,margins: { top: 24, right: 24, bottom: 24, left: 24 },axes: false,schema: {note: { type: "categorical", open: true },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},data: [{ note: "Come near me for my icons.", x: 28, y: 66 },{ note: "Drag the grip to move me.", x: 70, y: 34 },],marks: [sticker({id: "note",fill: "#EBD6AD",stroke: "rgba(70, 89, 107, 0.13)",maxWidth: 150,edits: [remove({ name: "delete", gesture: "commit" })],channels: {x: { field: "x", edit: move({ name: "drag", channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },text: { field: "note" },},}),],});mount(chart.dom());ui.attach(chart, [ui.actions({on: { mark: "note" },placement: "top-right",items: [// A drag edit: drag the button, or click it and then click where to drop.{ edit: "drag", icon: "grip", label: "Move" },// A commit edit: one click fires it on the note.{ edit: "delete", icon: "trash", label: "Remove", tone: "danger" },],}),], { motion: true }); // the icons fade in and out
Handles
ui.handles({ on }) puts a grip on each side a mark sizes from data: left and right where it binds width to a field, top and bottom where it binds height. Dragging a grip moves that side and holds the opposite side still. The width and the position change together as one undo step. Escape during the drag puts the box back, and the arrow keys step a focused grip. on is a scene query, as in ui.actions: { mark: "note" } names the mark, and { mark: "note", index: 0 } keeps the grips to one row.
The size channel needs a continuous scale with a pixel range, which also sets the smallest and largest size.
rotate: true adds a grip above the box that turns it about its centre. It writes the mark's angle field, through the angle scale when there is one. Hold Shift to turn in steps of 15°. move: true adds a grip below the box that moves it, writing its x and y fields. Both grips turn with the box, and the arrow keys step a focused grip.
Resize a note from either side
The note binds width to a field. No edit is declared on it: the grips write the width and the centre with set.
const chart = Elicit({width: 460, height: 300,margins: { top: 20, right: 20, bottom: 20, left: 20 },axes: false,schema: {note: { type: "categorical", open: true },w: { type: "quantitative", domain: [70, 260] },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},data: [{ note: "Drag a grip on my side. The text re-wraps to the width I end up.", w: 170, x: 32, y: 62 },{ note: "Narrow notes get taller.", w: 100, x: 72, y: 28 },],marks: [sticker({id: "note",fill: "#EBD6AD",stroke: "rgba(70, 89, 107, 0.13)",maxWidth: 260,edits: [editText({ multiline: true })],channels: {x: { field: "x", edit: move({ channels: ["x", "y"], mode: "relative" }) },y: { field: "y" },// The width is data, so there is a side to drag. No edit is declared on it.width: { field: "w", scale: { range: [70, 260] } },text: { field: "note" },},}),],});mount(chart.dom());ui.attach(chart, [ui.handles({ on: { mark: "note" } })]);
Turn and move a note
The note binds its tilt to a field. The grips write the tilt and the position with set.
const chart = Elicit({width: 460, height: 300,margins: { top: 20, right: 20, bottom: 20, left: 20 },axes: false,schema: {note: { type: "categorical", open: true },tilt: { type: "quantitative", domain: [-180, 180] },x: { type: "quantitative", domain: [0, 100] },y: { type: "quantitative", domain: [0, 100] },},data: [{ note: "Turn me with the grip above.", tilt: 8, x: 30, y: 62 },{ note: "Move me with the grip below.", tilt: -6, x: 70, y: 34 },],marks: [sticker({id: "note",fill: "#EBD6AD",stroke: "rgba(70, 89, 107, 0.13)",maxWidth: 150,channels: {x: { field: "x" },y: { field: "y" },// The tilt is data, in degrees counter-clockwise, so there is something to turn.angle: { field: "tilt", scale: { range: [-180, 180] } },text: { field: "note" },},}),],});mount(chart.dom());ui.attach(chart, [ui.handles({ on: { mark: "note" }, rotate: true, move: true })]);
A part of your own
ui.custom(mount) is a part you write. mount(ctx) builds it and may return a teardown. ctx.chart is the chart, read through scene, hit, scale and edits. ctx.layer is the element everything goes in. The helpers on ctx release what they register when the layer detaches:
ctx.place(el, anchor, placement)keeps an element on a point or a drawn piece, on every render.ctx.button(options),ctx.menu(items, at)andctx.icon(icon)build the layer's own controls.ctx.onPointer(cb)reports the pointer in plot pixels, and a right-click as"menu".ctx.drag(el, handlers)makes an element a drag source.ctx.capture(handlers)takes the chart's next press, for a two-step action.
The built-in parts use only this context and the chart's public methods.
A stepper over the hovered bar
A custom part: two buttons that follow the bar under the pointer and write its value with set.
const chart = Elicit({width: 440, height: 280,margins: { top: 30, right: 20, bottom: 40, left: 44 },schema: {quarter: { type: "categorical", domain: ["Q1", "Q2", "Q3", "Q4"] },n: { type: "quantitative", domain: [0, 100] },},data: [{ quarter: "Q1", n: 40 }, { quarter: "Q2", n: 62 },{ quarter: "Q3", n: 55 }, { quarter: "Q4", n: 71 },],constraints: [clamp({ field: "n", min: 0, max: 100 })],marks: [barY({ id: "bar", channels: { x: { field: "quarter" }, y: { field: "n", edit: move() } } }),],});mount(chart.dom());// A part of your own: a stepper over the bar the pointer is on.const stepper = ui.custom((ctx) => {const { chart } = ctx;const box = document.createElement("div");box.style.cssText = "display:flex;gap:4px;pointer-events:none";let row = null;const step = (by) => () => {if (row == null) return;// No edit needed for a value: set runs the clamp and is one undo step.chart.set("n", chart.getData()[row].n + by, { index: row });};const minus = ctx.button({ icon: "<svg viewBox='0 0 16 16'><path d='M3 8h10'/></svg>", label: "Five fewer", onClick: step(-5) });const plus = ctx.button({ icon: "plus", label: "Five more", onClick: step(5) });box.append(minus, plus);// Keep the box above the bar's top. scene() is read again on every render.ctx.place(box, () => (row == null ? null : chart.scene({ mark: "bar", index: row })[0]), { at: "top", offset: 6 });ctx.onPointer((e) => {if (e.type === "leave") row = null;if (e.type === "move") {const view = chart.hit(e.point, { reach: 30 });row = view && view.mark === "bar" ? view.index : null;}ctx.refresh();});});ui.attach(chart, [stepper]);
Theme, icons and CSS
The layer takes its colours and sizes from theme.ui: surface, ink, muted, border, accent, danger, shadow, radius, size, gap and fontSize. Set them in spec.theme like any other token.
Elicit({ ...spec, theme: { ui: { accent: "#7c3aed", size: 26 } } });
The tokens reach the page as CSS variables on the layer (--elicit-ui-accent, --elicit-ui-size, …), and every control has a class: elicit-ui-button, elicit-ui-menu, elicit-ui-item, elicit-ui-handle. Override either from your own stylesheet.
An icon is a built-in name (plus, link, unlink, trash, swap, palette, grip, check), SVG markup or an element. authoring.registerIcon(name, path) adds a name, from path data on a 16 × 16 grid.