@macrulez/visual-linker-core
v0.4.11
Published
Framework-agnostic engine that draws smart, auto-routed SVG connector lines between DOM blocks
Readme
Visual Linker Core

Framework-agnostic engine that draws auto-routed SVG connector lines
between DOM blocks you already control. No Vue — just
createVisualLinker(container, config), returning a small imperative
API any framework adapter (or vanilla code) can drive directly.
Part of the visual-linker
monorepo. Framework adapter:
@macrulez/visual-linker-vue
(which also wires up
@macrulez/visual-linker-nuxt).
Features
createVisualLinker(container, config?)— measures the blocks you hand it (setBlocks) and draws the SVG path between their resolved ports (setConnections), batched onto a sharedResizeObserver+requestAnimationFramerender loop- Auto-side port routing —
side: 'auto'(the default) picks whichever side of a block best faces the other endpoint on every render; aFixedSide[]narrows the automatic choice to a subset instead of fixing it outright - Three curve types —
bezier(configurable curvature/reach/angle-lean),smoothstep(orthogonal routing with rounded corners and shared branch points for siblings on one port),straight - Per-connection styling — color, width, dashed, start/end markers (
circle/square/diamond/arrow, or raw custom SVG), and and a distinct look for the hover, selected and focus states — one structured configuration for lines, markers, ports, labels and colors, overridable per connection, and changeable at runtime - Pointer-driven drag & drop — an optional
dragHandle, ablocks.drag.gridsnap, andblocks.drag.boundsconfined to the container/an element/an inset box, re-resolved live on every pointer move - A typed event API — drag/hover/click events, plus a
layoutevent fired on every render pass with every connection's and port's resolved geometry (points, angles, midpoints), for positioning your own overlay content - Creating connections — the user drags from port to port, clicks two ports, or uses the keyboard; the library reports
connection:create-requestand your code decides (interaction.connect, off by default) - Fallback ends — a connection whose block is collapsed or not rendered is drawn to its nearest visible ancestor, in a
proxiedstate you can style - Built for hundreds of blocks — only changed lines are redrawn, blocks without connections cost nothing,
batch()groups changes,performance.cullskips lines nobody can see,getStats()shows what happened - Paint order —
stacking.lines: 'under'draws strokes behind the blocks while ports and markers stay in front; a connection or block can override the instance-wide plane - Auto-managed positioning — sets
container's CSS position torelativefor you if it's stillstatic, so the SVG overlay lines up with your blocks with zero required setup CSS - Zero runtime dependencies, zero peer dependencies — usable standalone in any environment, including outside a framework entirely
When you'd reach for this
You're writing a framework adapter of your own, working somewhere Vue isn't an option, or just want the raw engine without a component/composable layer on top.
- A vanilla script with no build step or framework at all — Not every diagram needs Vue for one set of connector lines.
createVisualLinker()works the same way loaded straight from a<script type="module">. - Building an adapter for something not already covered — Svelte, a vanilla web component, a custom internal framework. The engine exposes a plain imperative API (
setBlocks/setConnections/on) with no assumptions about your reactivity — wrapping it is a thin layer, not a rewrite. - You need direct control over exactly when a render happens —
refresh()forces an immediate recompute, bypassing the engine's ownrequestAnimationFramebatching — useful right before taking a screenshot of the diagram.
Installation
npm install @macrulez/visual-linker-coreNo peer dependencies. Requires Node.js 18+ if built in a Node/SSR
context; in the browser, any environment with ResizeObserver and
requestAnimationFrame works.
Quick start
import { createVisualLinker } from '@macrulez/visual-linker-core'
const container = document.querySelector('#diagram')
const linker = createVisualLinker(container, { lines: { curve: 'smoothstep' } })
linker.setBlocks([
{ id: 'a', el: document.querySelector('#block-a') },
{ id: 'b', el: document.querySelector('#block-b') },
])
linker.setConnections([{ id: 'a-b', from: { blockId: 'a' }, to: { blockId: 'b' } }])
linker.on('connection:click', ({ connection }) => console.log(connection.id))
// later: linker.destroy()Configuration
One structured object configures everything, grouped by what it styles:
const linker = createVisualLinker(el, {
theme: darkTheme, // colors in one place — see Themes below
lines: {
curve: 'smoothstep',
color: '#6fcf97',
width: 2,
dashed: false,
highlight: { width: 2.5 },
hover: { width: 3 },
selected: { color: '#8ab4ff' },
focus: { width: 4 },
opacity: 1,
bezier: { curvature: 0.5, minReach: 24, maxReach: 160, angleBlend: 0.55, angleMaxOffset: 30 },
smoothstep: { cornerRadius: 8, maxTrunkReach: 48, stub: 20 },
routing: { avoidObstacles: true, padding: 12 },
jumps: { radius: 6 },
animated: { shape: 'dots', speed: 50 },
},
markers: { end: { shape: 'arrow', hover: { size: 10 } }, sizes: { arrow: 8 } },
ports: { show: true, radius: 4, fill: '#fff', stroke: '#2e8b57', hover: { radius: 6 }, spread: { gap: 16 } },
labels: { background: '#fff', color: '#1c1e2b', fontSize: 11, hover: { background: '#eef' } },
blocks: { draggable: true, drag: { grid: 20, bounds: 'container', collision: { gap: 16 } } },
interaction: {
hover: true,
highlight: true,
selectable: true,
clipToScrollParents: 'pin',
connect: { enabled: true, mode: 'both' }, // users draw connections — see Creating connections below
},
performance: { cull: true, maxConnectionsForJumps: 150 }, // see Many blocks and connections below
stacking: { lines: 'under' }, // strokes behind blocks; ports and markers stay in front
})ConnectionDescriptor.style has the same shape as lines (plus
markers: { start, end }), so anything that can be set for every line can be
set for one — in the same words. Resolution, from the strongest: the field on
the connection (or port, block, label) → the group in the configuration →
the theme token → a CSS variable of your own → the built-in value. The
state buckets merge field by field at every level.
Changing the configuration at runtime
linker.setConfig({ lines: { color: '#e0526c' } }) // deep-merges; `undefined` unsets a key
linker.replaceConfig(nextConfig) // replaces everything
linker.getConfig() // a copy of what is in effectEverything re-renders, and the listeners behind blocks/interaction
(dragging, selection) are re-attached or removed to match.
Stacking (lines behind blocks)
The SVG overlay paints over the blocks by default. stacking.lines: 'under'
draws connector strokes (and their flow animation) behind the blocks, while
ports and endpoint markers stay in front — so a line can tuck under a card
without hiding the arrow or the port.
createVisualLinker(el, {
stacking: {
lines: 'under', // default 'over'
ports: 'over',
markers: 'over',
},
})One connection overrides the instance-wide line plane:
{ id: 'highlight', from, to, style: { stacking: 'over' } }One block overrides ports:
{ id: 'hero', el, stacking: { ports: 'under' } }Vanilla createVisualLinker(container) inserts an underlay SVG as the
container's first child when anything resolves to 'under'. Pass a dedicated
host with linker.setUnderlay(host) when the blocks aren't siblings of that
SVG (the Vue component does this). 'under' has no effect in the Vue
component's scope="page" — that overlay is teleported to <body>.
Themes
A theme is a set of color tokens — line, lineHover, lineSelected,
selectedHalo, focusRing, portFill, portStroke, labelBackground,
labelBorder, labelText — written to CSS variables (--vl-line-color, …)
on the diagram, so plain CSS can still override them. lightTheme and
darkTheme are included, defineTheme(overrides, base = lightTheme) makes
your own, and switching is one call:
import { createVisualLinker, darkTheme, defineTheme } from '@macrulez/visual-linker-core'
const midnight = defineTheme({ line: '#8b9bff', lineHover: '#c4ccff', portFill: '#1b1d2b' }, darkTheme)
createVisualLinker(el, { theme: midnight })
linker.setConfig({ theme: lightTheme })More examples
Blocks and ports
interface BlockDescriptor {
id: string
el: HTMLElement
ports?: PortDescriptor[] // omit → connections anchor to the block's own border, side 'auto'
draggable?: boolean // overrides the instance-wide default for this block
dragHandle?: string | HTMLElement
dragBounds?: 'container' | HTMLElement | { top?: number; right?: number; bottom?: number; left?: number }
collision?: boolean | { gap?: number } // overrides blocks.drag.collision
stacking?: { ports?: 'over' | 'under' } // overrides config.stacking.ports
}
interface PortDescriptor {
id: string
target?: string | HTMLElement // CSS selector or element — default: the block's own el
side?: 'auto' | FixedSide | FixedSide[] // FixedSide = 'top' | 'right' | 'bottom' | 'left'
offset?: number // 0..1 along the resolved side, default 0.5
anchorBlockId?: string // draw the point on ANOTHER block's border, still tracking target's real position
anchorEl?: HTMLElement
}A port's target lets a connection anchor somewhere more specific than a
block's own border — a particular row in a list, a specific field in a
form:
linker.setBlocks([
{
id: 'form',
el: formEl,
ports: [
{ id: 'email-field', target: '#email-row', side: 'right' },
{ id: 'submit', target: '.submit-btn', side: 'bottom' },
],
},
])How far a line leaves a block (stub)
A smoothstep line keeps going straight out of a block's side for stub px (default 20) before it may turn. The same
distance is the clearance of a trunk that wraps around the blocks (both ends on the same side, or the target lying behind the
exit) and the minimum reach of a shared branch point — so a line never runs along a block's own edge.
createVisualLinker(el, { lines: { smoothstep: { stub: 32 } } }) // every line
{ id: 'a-b', from, to, style: { curve: 'smoothstep', smoothstep: { stub: 8 } } } // this connection onlyFor a group of connections sharing a port, the largest stub among them is used for the branch point.
Spreading lines that share a port
By default every connection attached to the same port side meets at one
point. portSpread gives each of them its own virtual port along that side
instead — gap px apart, centered on the port's own point, ordered by where
each line's other end is (so they don't cross, and they re-order as blocks
are dragged). If the row wouldn't fit inside the side minus padding at
both corners, the gap shrinks until it does. Top/bottom and left/right
sides alike.
linker.setBlocks([
{ id: 'hub', el: hubEl, portSpread: { gap: 24, padding: 8 } }, // whole block
{ id: 'list', el: listEl, ports: [{ id: 'out', spread: true }] }, // one port only
])
createVisualLinker(el, { ports: { spread: true } }) // every block; `spread: false` opts a port back outPriority: port spread > block portSpread > ports.spread; false at
any level switches an inherited setting off. Defaults: gap: 16, padding: 8.
Hops at crossings
createVisualLinker(el, { lines: { jumps: true } }) // or { lines: { jumps: { radius: 8 } } }
{ curve: 'smoothstep', jumps: false } // opt a single line outWhere a smoothstep line crosses another connection's line, its horizontal
stretch makes a small semicircular hop over the vertical one — as on electrical
schematics. Lines running along each other (a shared trunk), T-junctions and
crossings too close to a bend are left alone. Combine with routing.avoidObstacles for
tidy diagrams.
Routing around blocks
createVisualLinker(el, { lines: { routing: { avoidObstacles: true, padding: 12 } } })
{ curve: 'smoothstep', routing: { avoidObstacles: false } } // opt a single connection outBy default smoothstep lines run straight through any block that happens to
be in between. With routing.avoidObstacles they are routed around the other blocks
(A* over the blocks' padded edges, fewest turns first), re-routing live as
blocks are dragged. A line whose plain route is already clear is left exactly
as it was; blocks that are an endpoint of the line (or contain / sit inside
one) are not obstacles; and when no route exists the plain one is kept, so a
line never disappears. Only blocks within ~240px of a line are considered.
Labels along a line
{
id: 'a-b', from, to,
labels: [
{ id: 'method', position: 'start', text: 'POST' },
{ id: 'name', position: 0.5, text: 'follows the line', rotate: true, offset: -14 },
{ id: 'custom', position: 'end' }, // no text: just a position, for your own content
],
}position is 'start' / 'middle' / 'end' (start/end sit a little in from
the endpoint, clear of its marker) or a 0..1 fraction of the line's length;
offset moves the label along the line's normal (positive = right of the
direction of travel); rotate turns it to follow the line, kept upright.
A label with text is drawn by the library as an SVG pill (style it with
--vl-label-bg / --vl-label-border / --vl-label-color, or className
and .vl-label-bg / .vl-label-text). Every label's resolved position is in
ConnectionLayout.labels of the layout event, for rendering your own.
Animated flow
{ id: 'a-b', from, to, style: { animated: true } }
{ id: 'a-c', from, to, style: { animated: { shape: 'dots', speed: 50, direction: 'forward' } } }
createVisualLinker(el, { lines: { animated: true } }) // every line; `animated: false` opts one outA pattern travels along the line from from to to (or back with
direction: 'backward') — handy for live traffic and for showing direction.
By default the pattern takes the line's own color and the line underneath is
dimmed, so it reads on any background; give color to draw it in that color
over the unchanged line instead. It is a separate overlay, so it composes with
dashed, the hover state and markers; pure CSS (no per-frame JS) and switched off
for prefers-reduced-motion.
Selecting connections, keyboard and screen readers
const linker = createVisualLinker(el, { interaction: { selectable: true } })
linker.on('connection:selectionchange', ({ selectedIds }) => {})
linker.on('connection:delete-request', ({ connections }) => {}) // you remove them — or don't
linker.setSelectedConnections(['a-b']) // controlled use; emits nothingWith interaction.selectable, every connection is a focusable button (Tab order = the
order of connections). Click or Enter/Space selects it (Ctrl/Cmd/Shift
toggles within a multi-selection); Escape or a click elsewhere clears the
selection; Delete/Backspace asks to delete the selection — the library never
removes data. Style a selected line with style.selected (same fields
as hover, hover wins while both apply); the default is a width bump
plus a halo (--vl-selected-color, focus ring: --vl-focus-color). Lines
always carry role="img" and an aria-label — ariaLabel on the
connection, or "Connection: {from} → {to}".
Ports scrolled out of view
A port inside an overflow: auto/scroll/hidden/clip element (a row in a
scrolling list, say) that has scrolled out of the visible area no longer
leaves a line dangling over unrelated content. By default
(interaction.clipToScrollParents: 'pin') that end is pulled to the edge of the visible
area — the line "continues off-screen" — and its marker and port dot are
dropped; 'hide' hides the whole connection; false ignores clipping. The
layout event flags such ends as fromClipped/toClipped.
Connections and curves
linker.setConnections([
{ id: 'c1', from: { blockId: 'a' }, to: { blockId: 'b' } }, // bezier, the default
{ id: 'c2', from: { blockId: 'a' }, to: { blockId: 'c' }, style: { curve: 'smoothstep' } }, // orthogonal
{ id: 'c3', from: { blockId: 'a' }, to: { blockId: 'd' }, style: { curve: 'straight' } }, // plain line
])bezier's curvature/curveMinReach/curveMaxReach control how far the
curve bows out; curveAngleBlend/curveAngleMaxOffset control how much
the exit/entry angle leans toward the target instead of staying
perpendicular to the block's border. smoothstep's cornerRadius sets
the bend radius, and maxTrunkReach caps how far a shared trunk extends
before splitting — relevant only when 2+ connections share the same
(block, port, side).
Markers and states
linker.setConnections([
{
id: 'a-b',
from: { blockId: 'a' },
to: { blockId: 'b' },
style: {
color: '#6366f1',
hover: { color: '#312e81', width: 3 },
markers: {
end: { shape: 'arrow', size: 8, hover: { size: 11, color: '#312e81' } },
},
},
},
])markers.start / markers.end accept a bare shape name, a full marker config
(shape/size/color/strokeColor/strokeWidth/className/svg/orient/arrow),
or false to suppress even the built-in port dot. Every visual entity has the
same four state buckets — highlight, hover, selected, focus — with the fields of the
entity itself, so a marker can change its shape, size, colors or whole svg while
its connection is hovered or selected. hover is the pointer over the line
itself; highlight is a line lit because the pointer is on one of its blocks
(it behaves like hover until a highlight bucket is set). Both are off by default — turn them on
with interaction: { hover: true, highlight: true }, or per entity with hoverable on a connection and
highlightable on a block (the local value wins); without them a diagram does not react to the pointer and
shows no pointer cursor, while the events still fire. States stack as base →
selected → highlight → hover → focus, falling back to the base for any field left unset; a marker
follows the line's color of the current state unless it sets its own.
A shape marker can carry a direction arrow too — the shape stays on the endpoint, and the arrow's tip stops exactly on the shape's outer edge (outline included), instead of the two overlapping:
markers: { end: { shape: 'circle', color: '#fff', strokeColor: '#6366f1', strokeWidth: 2, arrow: true } }
markers: { end: { shape: 'square', arrow: { color: '#e0526c', gap: 1 } } } // own color, 1 unit of air before the edgeThe arrow follows the line's direction, so a marker with arrow always
uses orient: 'auto'. For a custom svg marker the edge can't be
measured — set arrow.gap to the distance from the endpoint instead.
Drag & drop
const linker = createVisualLinker(diagramEl, {
blocks: {
draggable: true, // every block draggable by default
drag: {
grid: 20,
bounds: 'container',
collision: { gap: 16 }, // keep this much air between blocks; the larger gap of a pair wins
},
},
})
linker.setBlocks([{ id: 'a', el: cardEl, dragHandle: '.card-header' }])
linker.on('block:drag', ({ blockId, x, y }) => console.log(blockId, x, y))Connected lines re-route in real time as a block moves — no manual
refresh() call needed. drag.bounds also accepts a specific
HTMLElement (a drop-zone elsewhere in the layout) or an inset object
({ top, right, bottom, left }, shrinking the container's own box).
drag.collision stops a dragged block from overlapping the others.
Off by default. true uses a 16px gap; { gap: n } sets it. A block's
own collision wins over the instance value (false opts that block out
of being an obstacle). Between two blocks the larger of their gaps is
kept — the same way CSS margins collapse. The helper separateBox is
exported for the same rule outside of a pointer drag (e.g. dropping a new
block onto the diagram).
Creating connections
const linker = createVisualLinker(el, {
interaction: {
connect: {
enabled: true, // off by default: no handles, no tabindex, no live region
mode: 'both', // 'drag' | 'click' | 'both'
validate: (from, to) => to.portId !== 'locked' || 'This field is locked',
},
},
})
linker.on('connection:create-request', ({ from, to, via }) => {
connections.push({ id: `${from.portId}>${to.portId}`, from, to }) // the library adds nothing itself
linker.setConnections(connections)
})Drag from a port handle to another port, click two ports in turn, or focus a port and press Enter, Tab to a target,
Enter again (Escape cancels). connect: 'source' | 'target' | 'both' | false on a block or port decides which ends
take part; ports.connect.{source, candidate, target, invalid} and the data-vl-connecting /
data-vl-connect-state attributes style the gesture; an aria-live region announces it (announce). Your own
handlers can drive it with startConnect(from), completeConnect(to), cancelConnect() and isConnecting().
Fallback ends
{
id: 'first>fullName',
from: { blockId: 'user.name.first', fallback: ['user.name', 'user'] }, // the nearest visible ancestor
to: { blockId: 'person.fullName' },
}When a block is not registered (a collapsed tree node, a row a virtualized list has not rendered), or its port is
scrolled out of view, the end is drawn to the first usable fallback block and the connection takes the proxied
state (lines.proxied, style.proxied). ConnectionLayout.proxied tells which end moved.
Ports and roles in lists
const linker = createVisualLinker(container, {
lines: { endGap: 2 },
ports: {
show: 'all', // a circle on every declared port, connected or not
clearance: 'auto', // lines stop at the circle's edge
sides: { source: 'right', target: 'left' },
source: { fill: '#3b6fd4' },
target: { fill: '#2f9e6b' },
},
interaction: { connect: { enabled: true, handle: 'never', dimOthers: 0.3 } },
})A port is one circle: the end of a line and the drag handle at once, the same size connected or not. Colors come from
ports.source / ports.target, ports.connected, a port's own style / className, and ports.connect.<state>.
Buttons, links and inputs inside a block keep working: a press that starts on an element matching
interaction.connect.ignore never starts or finishes a connection. The block element carries
data-vl-connect-state, data-vl-hover and data-vl-proxy-count, so a whole row can be styled in CSS.
ports.render: 'dom' leaves the port to your own element; lines.kinds, lines.overlap and
markers.<position>.units: 'px' give finer control of the lines. connect:candidate reports the end under the pointer.
Many blocks and connections
linker.batch(() => {
for (const link of links) linker.addConnection(link) // one render for all of them
})
linker.getStats() // { blocks, connections, rendered, culled, proxied, lastRenderMs, medianRenderMs, observedElements }Only changed connections are redrawn, every rectangle is measured once per frame before any write, and a block without
connections is neither observed nor measured. performance: { cull, maxConnectionsForJumps, maxRoutingNodes,
maxObstacleBlocks } bounds the expensive modes. pnpm test:perf measures the bench stand and writes docs/PERFORMANCE.md.
Events
linker.on('layout', ({ connections, ports }) => {
// fired on EVERY render pass — connections: ConnectionLayout[] (from/to/mid points, angles),
// ports: PortLayout[] (resolved port points) — for positioning your own overlay content
})block:dragstart/drag/dragend, block:mouseenter/mouseleave,
connection:click/mouseenter/mouseleave/selectionchange/delete-request, connect:start,
connection:create-request/create-cancel, and layout — every on()
call returns its own unsubscribe function.
Documentation & links
- 📖 Full documentation: npm.vuecraft.ru/en/packages/visual-linker
- 🌐 VueCraft: vuecraft.ru/en
- 👤 Author: macrulez.ru/en
- 💻 GitHub: macrulezru/visual-linker/packages/core
- 📦 NPM: @macrulez/visual-linker-core
- 🐛 Issues: github.com/macrulezru/visual-linker/issues
License
MIT
💖 Support the project
Open source takes time and effort. If this library saves you time or brings value, consider supporting further development.
Thank you for being part of this journey. ❤️
