Skip to content

Engine API ​

createVisualLinker(container, config?) — the framework-agnostic engine every other part of the package is built on. You own the block elements and their position; this measures them and draws the SVG connections.

ts
function createVisualLinker(container: HTMLElement, config?: VisualLinkerConfig): VisualLinker

Renders into an absolutely-positioned <svg> layer the engine creates inside container. The engine gives container a positioning context automatically (position: relative, only if its computed position was static) so that overlay lines up with the blocks inside it — no manual CSS needed.

Configuration ​

config is a VisualLinkerConfig: one object, grouped into theme, lines, markers, ports, labels, blocks, interaction, performance and stacking. Every field is optional. The groups and their fields are described in Configuration, the colors in Themes, and the hover, selected and focus looks in Visual States.

Example:

ts
const linker = createVisualLinker(diagramEl, {
  theme: darkTheme, // colors of lines, ports and labels, in one place
  lines: {
    curve: 'smoothstep',
    width: 2,
    hover: { width: 3 },
    routing: { avoidObstacles: true },
    jumps: true,
  },
  markers: { end: { shape: 'arrow', hover: { size: 10 } } },
  ports: { radius: 5, spread: true },
  blocks: { draggable: true, drag: { grid: 20, bounds: 'container', collision: { gap: 16 } } },
  interaction: { selectable: true },
})

Methods ​

setBlocks(blocks: BlockDescriptor[]): void ​

Replaces the full set of blocks the engine measures and draws connections between. See Blocks & Ports for BlockDescriptor's shape. Triggers an immediate render.

setConnections(connections: ConnectionDescriptor[]): void ​

Replaces the full set of connections. See Connections for ConnectionDescriptor's shape. Triggers an immediate render.

updateBlock(id: string, patch: Partial<Omit<BlockDescriptor, 'id'>>): void ​

Merges patch into the block with the given id — e.g. to change its ports or draggable flag without re-passing every other block. No-op if id isn't currently registered.

addConnection(connection: ConnectionDescriptor): void ​

Adds a single connection without replacing the others.

removeConnection(id: string): void ​

Removes a single connection by id.

addBlock(block: BlockDescriptor): void ​

Registers one block, or replaces the block with the same id, without touching the others.

removeBlock(id: string): void ​

Unregisters one block. Connections that pointed at it are skipped until it comes back, or drawn to their fallback ends.

setSelectedConnections(ids: readonly string[]): void ​

Replaces the set of selected connections — for driving the selection from your own state (needs interaction.selectable). Ids that aren't among the current connections are ignored. Emits no event. See Selection & Accessibility.

setConfig(patch: VisualLinkerConfig): void ​

Deep-merges patch into the current configuration and redraws. A key set to undefined is removed. Listeners follow the change: blocks.draggable and interaction.selectable attach or detach what they need. See Configuration.

replaceConfig(next: VisualLinkerConfig): void ​

Replaces the whole configuration with next and redraws.

getConfig(): VisualLinkerConfig ​

Returns a copy of the configuration in effect. Changing the copy does not change the engine.

refresh(): void ​

Forces an immediate path recalculation, bypassing the engine's requestAnimationFrame batching — useful right before taking a screenshot of the diagram.

setDragOffset(id: string, offset: { x: number; y: number }): void ​

Writes the same CSS translate a pointer drag would. { x: 0, y: 0 } clears the offset for that block. Does not run collision — call separateBox first if the offset must not overlap other blocks.

resetDragOffsets(): void ​

Clears every stored drag offset and the CSS translate it applied.

setUnderlay(host: HTMLElement | null): void ​

Host for the underlay SVG when stacking.lines or stacking.ports is 'under'. null auto-inserts it as the first child of the engine container. The Vue component calls this with its back layer; you only need it in vanilla when the blocks are not siblings of that SVG. See Configuration.

batch(fn: () => void): void ​

Runs fn and renders once afterwards: every block, connection and config call inside shares a single pass. Calls can be nested. See Performance.

getStats(): VisualLinkerStats ​

{ blocks, connections, rendered, culled, proxied, lastRenderMs, medianRenderMs, observedElements } — counters of the last render pass. Measures nothing in the DOM. See Performance.

startConnect(from: ConnectionEndpoint): boolean ​

Starts a user connection from from. false when interaction.connect is off or the end is not allowed. See Creating Connections.

completeConnect(to: ConnectionEndpoint): boolean ​

Finishes the running connection at to and emits connection:create-request. false when nothing is running or to is not allowed.

cancelConnect(reason?: 'api'): void ​

Cancels the running connection; emits connection:create-cancel.

isConnecting(): { active: boolean; from?: ConnectionEndpoint } ​

Whether a connection is being made, and from where.

on(event, handler): () => void ​

Subscribes to an engine event (see Events below). Returns an unsubscribe function.

destroy(): void ​

Tears down every listener (resize/scroll/pointer), clears all blocks and connections, and removes the SVG layer. Call this when the container is about to leave the DOM — <VisualLinker> and useVisualLinker() both already do this for you on unmount.

Collision helpers ​

Exported from @macrulez/visual-linker-core (and re-exported by -vue). Pointer drag uses them automatically when blocks.drag.collision (or a block's own collision) is on. Call them yourself to place a block outside a pointer drag — for example dropping a new card onto the diagram.

resolveCollisionOption(own, instance, fallbackGap?) ​

(CollisionOption | undefined, CollisionOption | undefined, number?) => number | false

Own value wins; unset at both levels is off. false opts out. true uses fallbackGap (16). { gap } uses that number, or 16 if gap is omitted.

separateBox(moving, obstacles) ​

(CollisionBox, readonly CollisionBox[]) => { x: number; y: number }

Nudges moving until it sits at least max(moving.gap, other.gap) away from every obstacle — the larger gap of the pair, like CSS margin collapse. CollisionBox is { x, y, w, h, gap } in one coordinate space.

ts
import { separateBox, resolveCollisionOption } from '@macrulez/visual-linker-core'

const gap = resolveCollisionOption(undefined, { gap: 16 })
if (gap !== false) {
  const { x, y } = separateBox(
    { x: dropX, y: dropY, w: 240, h: 80, gap },
    occupied.map((rect) => ({ ...rect, gap })),
  )
}

Events ​

Subscribe via linker.on(eventName, handler).

block:dragstart ​

{ blockId: string }

Fired when a block drag starts.

block:drag ​

{ blockId: string; x: number; y: number }

Fired on every pointer move during a drag, with the block's current offset from its base position — after grid snap, collision, and bounds clamp.

block:dragend ​

{ blockId: string; x: number; y: number }

Fired when a block drag ends, with its final offset — after grid snap, collision, and bounds clamp.

block:mouseenter ​

{ blockId: string; via?: 'line' }

Fired when the pointer enters a block — with interaction.highlight on (or highlightable on the block), it also puts every connection incident to it into the highlight state.

via: 'line' is present when the event comes from the pointer being over a line that ends in the block, not over the block itself.

block:mouseleave ​

{ blockId: string; via?: 'line' }

Fired when the pointer leaves a block.

via: 'line' is present when the event comes from the pointer being over a line that ends in the block, not over the block itself.

connection:click ​

{ connection: ConnectionDescriptor }

Fired when a connection's line (or its wider invisible hit area) is clicked.

connection:mouseenter ​

{ connection: ConnectionDescriptor }

Fired when the pointer enters a connection's line — with interaction.hover on (or hoverable on the connection), it also puts it into the hover state.

connection:mouseleave ​

{ connection: ConnectionDescriptor }

Fired when the pointer leaves a connection's line.

connection:selectionchange ​

{ selectedIds: string[] }

Fired when the set of selected connections changes because of user input (interaction.selectable mode) — not for a setSelectedConnections() call.

connection:delete-request ​

{ connections: ConnectionDescriptor[] }

Fired when Delete or Backspace is pressed on a focused connection: the selected connections, or just the focused one when it isn't selected. Nothing is removed — the app decides.

connect:start ​

{ from: ConnectionEndpoint; via: 'drag' | 'click' | 'keyboard' | 'api' }

Fired when a user connection starts. See Creating Connections.

connect:candidate ​

{ from: ConnectionEndpoint; candidate: ConnectionEndpoint | null; valid: boolean }

Fired when the candidate end of a running gesture changes. candidate is null when nothing is under the pointer or the focus.

connection:create-request ​

{ from, to, via: 'drag' | 'click' | 'keyboard', modifiers: { shift, ctrl, alt, meta } }

Fired when the user finishes a connection on an allowed end. Nothing is added — the app decides.

connection:create-cancel ​

{ from, reason: 'escape' | 'no-target' | 'invalid' | 'pointer-cancel' | 'api', message? }

Fired when a connection in progress is abandoned.

layout ​

{ connections: ConnectionLayout[]; ports: PortLayout[] }

Fired at the end of every render pass with every connection's and port's resolved geometry (points, angles, midpoints) — this is what drives the Vue component's #connection-label/#port/#marker overlay slots. Useful for positioning your own custom overlay content when using the engine directly. Each ConnectionLayout also carries labels — every label resolved to a point and angle on the line — and fromClipped / toClipped, set when an end was pinned to the edge of a clipping ancestor, and proxied: { from?, to? }, set when an end was moved to a fallback block. A connection hidden by culling has no entry.