Skip to content

API

Each section is one entry point of the package. The options reference has the fields of each options object.

The core: the model, the engine, the strategies, the routers and the builder. It imports nothing.

function layout(input: LayoutInput, options?: LayoutOptions): Layout;
function createEngine(config?: EngineConfig): Engine;
interface Engine {
layout(input: LayoutInput, options?: LayoutOptions): Layout;
extend(config: EngineConfig): Engine;
readonly strategies: ReadonlyMap<string, AnyStrategy>;
readonly routers: ReadonlyMap<string, Router>;
readonly limits: Limits;
}
class IrekoError extends Error {
readonly issues: readonly Issue[]; // the findings with the severity "error"
}

layout uses a shared engine with the built-in strategies and routers. It throws only in the strict mode, or for an input that is not an object with a root.

The option linkedShape of EngineConfig sets the code that gives the shape of a linked node. The default is the code of ireko. This option and its types LinkedShape and ShapePart have the tag @beta, so a release can change them.

const grid: Builder<GridOptions>;
const radial: Builder<RadialOptions>;
const tree: Builder<TreeOptions>;
const layers: Builder<LayersOptions>;
function builder<O>(type: string): Builder<O>; // a builder for a strategy that you register
function node(id: string, options?: Omit<ContainerOptions, "gap" | "padding" | "header">): NodeSpec;
type Child = NodeSpec | string; // a node, or the identifier of a leaf
type BuildOptions<O> = Partial<O> & ContainerOptions;
interface ContainerOptions { label?; size?; fit?; data?; gap?; padding?; header? } // each container has these options, for each strategy
interface Builder<O> {
(id: string, ...children: readonly Child[]): NodeSpec; // a linked node
(id: string, options: BuildOptions<O>, ...children: readonly Child[]): NodeSpec;
nested: { // a container
(id: string, ...children: readonly Child[]): NodeSpec;
(id: string, options: BuildOptions<O>, ...children: readonly Child[]): NodeSpec;
};
}

A string child is a leaf with that identifier. The options object of a builder has the options of the strategy, and label, size, fit, data, gap, padding and header.

function defineStrategy<O>(strategy: Strategy<O>): Strategy<O>;
function defineRouter(router: Router): Router;
const gridStrategy: Strategy<GridOptions>;
const radialStrategy: Strategy<RadialOptions>;
const treeStrategy: Strategy<TreeOptions>;
const layersStrategy: Strategy<LayersOptions>;
const BUILTIN_STRATEGY_NAMES: readonly string[]; // grid, radial, tree, layers
const straight: Router;
const orthogonal: Router;
const avoid: Router; // around the other boxes
const builtinStrategies: readonly AnyStrategy[];
const builtinRouters: readonly Router[];
const defaultLimits: Limits; // { maxNodes: 100000, maxDepth: 10000, maxChildren: 10000 }
function borderPoint(box: Rect, toward: Point): Point; // where the line to a point leaves a box
function defaultsOf<O>(schema: OptionSchema<O>): O;

Write a strategy has the contract of Strategy, and edges has the contracts of Router and GraphRouter. These types are the inputs and the outputs of those contracts.

interface ArrangeInput<O> { id; mode: Mode; self: Size; children: readonly MeasuredChild[]; options: O; gap; depth; edges?: readonly ChildEdge[] } // the input of Strategy.arrange
interface Arrangement { direction?: Direction; flow?: Direction; offsets: readonly Point[]; content?: Rect } // the output of Strategy.arrange
interface AuditInput<O> { node: PlacedNode; children: readonly PlacedNode[]; options: O; gap } // the input of Strategy.audit, with absolute rectangles
interface RouteInput { id; kind: "tree" | "extra"; source: Rect; target: Rect; direction?: Direction; obstacles?: readonly Rect[] } // the input of Router.route, with absolute rectangles
interface GraphRouter { name?; route(input: GraphRouterInput): GraphRouterResult } // the option graphRouter: it routes all edges together
interface GraphRouterInput { nodes: readonly GraphNode[]; edges: readonly GraphEdge[] } // all boxes and all edges of one layout
interface GraphNode { id; box: GraphBox; parent? } // parent: the nested container of the box
interface GraphBox { x; y; w; h } // an absolute box: w is the width, and h is the height
interface GraphEdge { id; source: { node }; target: { node } }
interface GraphRouterResult { routes: readonly GraphRoute[] }
interface GraphRoute { edge; points: readonly Point[]; through?: readonly string[] } // through: the boxes that the route goes through
interface MeasuredChild { id; box: Size; extent: Rect; mode: Mode; shape?: Shape } // a child after the measure pass
interface ChildEdge { from; to } // an edge between the subtrees of two children, as their indexes
interface Shape { rows: readonly Band[]; columns: readonly Band[] } // the shape of a subtree, relative to the top-left corner of its root box
interface Band { from; to; min; max } // one band of a shape
type Direction = "down" | "up" | "left" | "right"; // the direction from a node to its children
type OptionSpec = NumberOption | BooleanOption | EnumOption; // the schema of one option
interface NumberOption { type: "number"; default: number; min?; max?; integer?; description }
interface BooleanOption { type: "boolean"; default: boolean; description }
interface EnumOption<V extends string = string> { type: "enum"; values: readonly V[]; default: V; description }
type OptionSpecFor<T> = [T] extends [number] ? NumberOption : [T] extends [boolean] ? BooleanOption : [T] extends [string] ? EnumOption<T> : never;

Between from and to on one axis, a subtree is between min and max on the other axis. In the rows of a shape, from and to are y values. In the columns, they are x values. A tidy strategy uses the shape to put a subtree closer to its neighbors than the extent permits.

interface LayoutInput { root: NodeSpec; edges?: EdgeSpec[] }
interface NodeSpec { id; label?; layout?; mode?; fit?; size?; children?; data? }
interface EdgeSpec { source; target; id?; label?; data? }
interface Layout {
nodes: PlacedNode[]; // in paint order: each container before the boxes inside it
edges: PlacedEdge[];
bounds: Rect;
issues: Issue[];
}
interface PlacedNode extends Rect {
id; label; parent?; container?; depth; mode; layout; scale; clip; childCount; data?;
}
interface PlacedEdge { id; source; target; kind: "tree" | "extra"; points: Point[]; label?; data? }
type LayoutRef = string | LayoutWithOptions; // the name of a strategy, or the name with options
interface LayoutWithOptions { type: string; [option: string]: unknown }
type Mode = "nested" | "linked"; // the children are inside the box of the node, or around it
type Fit = "grow" | "scale" | "clip";
type TreeEdges = "linked" | "all" | "none"; // the tree edges that the engine adds
type Severity = "error" | "warning"; // an error changes the output, and a warning does not
type IssueCode = "DUPLICATE_ID" | "EMPTY_ID" | "UNKNOWN_LAYOUT" | "UNKNOWN_ROUTER" | "INVALID_OPTION" | "INVALID_SIZE" | "UNKNOWN_EDGE_END" | "SELF_LOOP" | "LIMIT_NODES" | "LIMIT_DEPTH" | "LIMIT_CHILDREN" | "STRATEGY_FAILED" | "ROUTER_FAILED" | "EDGE_THROUGH_BOX" | "NON_FINITE" | "OVERLAP" | "ESCAPE" | (string & {});
interface TraceEvent { phase: "normalize" | "measure" | "place" | "route" | "audit"; data: unknown } // the output of one phase of the pipeline

All types are plain data. JSON holds each value with no loss.

With the fit grow, the container grows to fit its content. With scale, the content becomes smaller to fit the container. With clip, the content keeps its size and the renderer cuts it at the edge of the container.

function fitText(options?: FitTextOptions): (node: NodeSpec) => Size;
interface FitTextOptions { fontSize?; padding?: { x; y }; minWidth?; minHeight?; widthOf? }
function estimateWidth(text: string, fontSize: number): number; // a sans-serif font, with no font engine

Give fitText() to the measure option. widthOfCanvas(fontFamily) from ireko/render gives exact widths in a browser.

Point, Size and Rect, and small functions on them: centerOf, union, unionAll, translate, inflate, overlaps, contains, rectAt.

The layout of ireko, with the shapes of linked nodes from a module of the Vex compiler. It gives the same layout, to the bit. This entry shows the module of Vex. ireko is the choice for production use.

function layout(input: LayoutInput, options?: LayoutOptions): Layout;

The bundle of this entry is 20.5 kB with gzip. The bundle of ireko is 18.6 kB. These sizes come from the build of this page.

This table gives the time of a layout with this entry, divided by the time with ireko. A value above 1 is slower. The A/A column is a second copy of ireko, and it shows the noise of the measure.

InputA/Aireko/shapes
Full tree, 111 nodes1.0131.098
Full tree, 1111 nodes1.0011.026
Full tree, 11111 nodes1.0061.073
Examples0.9951.026
Random trees0.9990.990

This table is a fixed study of ireko 0.1.4, from the commit 1d575a6 of 2026-11-04. It tells nothing about a newer release. The entry is slower on full trees and on the examples, and 1 % faster on random trees. The values are medians of 9 processes with 31 rounds each, on Node 24 in CI. Both layouts run in one process, so one call in the measure pass receives two functions. A program that imports one entry does not have this condition, and this measure does not give its time.

The opt-in strategy force. Register it with createEngine({ strategies: [forceStrategy] }).

const forceStrategy: Strategy<ForceOptions>;
const force: Builder<ForceOptions>;
function relaxForces(nodes: RelaxNode[], edges: RelaxEdge[], options: Partial<RelaxOptions> & { bound }): Map<string, Point>;
const RELAX_DEFAULTS; // { iterations: 150, springLength: 340, repulsion: 90000, margin: 12, inset: 4, minDistance: 20, centering: 0.005, maxStep: 30, separationPasses: 300, theta: 0 }
const MAX_SPRING_STRENGTH: number; // 0.5: a larger strength throws a RangeError
const MAX_THETA: number; // 1: a larger theta throws a RangeError
function parse(text: string, options?: ParseOptions): ParseResult;
function print(input: LayoutInput, options?: PrintOptions): string;
interface ParseOptions { strategies?: readonly string[] }
interface PrintOptions { strategies?: readonly string[]; width?: number }
const KEYWORDS: ReadonlySet<string>; // nested, linked, grow, scale, clip
type ParseResult = { ok: true; input: LayoutInput } | { ok: false; error: SyntaxIssue };
interface SyntaxIssue { message; offset; length; line; column }

Refer to the text language.

function toSVG(layout: Layout, options?: SvgOptions): string;
function mountSvg(container: HTMLElement, options?: RendererOptions): Renderer;
function svgSurface(host: HTMLElement, theme: Theme): Surface;
function mountDom(container: HTMLElement, options?: RendererOptions): Renderer;
function domSurface(host: HTMLElement, theme: Theme): Surface;
function mountCanvas(container: HTMLElement, options?: RendererOptions): Renderer;
function canvasSurface(host: HTMLElement, theme: Theme): Surface;
function drawFrame(ctx: Context2D, frame: Frame, view: View, theme: Theme, screen: Screen): void;

drawFrame draws on any object with the methods of Context2D, for example a canvas on a server.

The parts that the live renderers share.

interface Renderer {
readonly element: HTMLElement;
readonly viewport: Viewport;
update(layout: Layout): void;
fit(padding?: number): void;
select(ids: Iterable<string>): void;
pick(point: Point): string | undefined;
destroy(): void;
}
function mount(container: HTMLElement, create: (host, theme) => Surface, options?: RendererOptions): Renderer;
function createViewport(options?: ViewportOptions): Viewport;
interface ViewportOptions { minZoom?: number; maxZoom?: number }
interface Viewport {
get(): View; // { x, y, k }
set(view: Partial<View>): void;
zoom(factor: number, at: Point): void;
pan(dx: number, dy: number): void;
fit(bounds: Rect, width: number, height: number, padding?: number): void;
subscribe(listener: (view: View) => void): () => void;
toLayout(screen: Point): Point; // a point of the element in the coordinates of the layout
attach(element: HTMLElement): () => void; // pan and zoom with the pointer and the wheel
}
const lightTheme: Theme;
const darkTheme: Theme;
function themeOf(partial: Partial<Theme> | undefined, base?: Theme): Theme;
function frameOf(layout: Layout): Frame;
function interpolate(from: Frame, to: Layout, t: number): Frame;
function easeInOut(t: number): number;
function labelAt(points: Point[]): Point; // the middle of a route, for its label
function widthOfCanvas(fontFamily: string): (text: string, fontSize: number) => number;
interface FrameNode extends PlacedNode { opacity: number } // a box of one frame of an animation
interface FrameEdge extends PlacedEdge { opacity: number } // an edge of one frame of an animation
function morph(a: readonly Point[], b: readonly Point[], t: number): Point[]; // moves one polyline to another
function resample(points: readonly Point[], count: number): Point[]; // count points, at equal distances along a polyline
function pickIn(frame: Frame, p: Point): string | undefined; // the identifier of the top box at a point
function isContainer(n: { mode: string; childCount: number }): boolean; // a node in the nested mode with children
function paintOrder(nodes: readonly FrameNode[]): { containers: FrameNode[]; leaves: FrameNode[] }; // the two layers of the paint order
function clipRoots(nodes: readonly FrameNode[]): Map<string, FrameNode>; // for each node, the nearest container with the fit clip that holds it
function haloOf(theme: Theme): string; // the color of the halo of a label
const LABEL_SCALE: number; // 0.85: the size of the text of an edge label, relative to the text of a node

ireko/svg also exports isContainer.

function useLayout(input: LayoutInput, options?: LayoutOptions, engine?: Engine): Layout;
function LayoutSvg(props: LayoutSvgProps): ReactElement;
function LayoutView(props: LayoutViewProps): ReactElement;
interface LayoutSvgProps { layout; theme?; padding?; title?; className?; style? }
interface LayoutViewProps { layout; renderer?: RendererKind; theme?; viewport?; duration?; interactive?; selected?; onPick?; className?; style? }
type RendererKind = "svg" | "dom" | "canvas"; // the live renderers of LayoutView

useLayout makes the layout again only when input, options or engine change, so keep them in a memo or a state. LayoutSvg has no state and no effects. LayoutView mounts a live renderer in an effect.

function layoutFlow<N extends Node, E extends Edge>(nodes: readonly N[], edges: readonly E[], options?: LayoutFlowOptions): { nodes: N[]; edges: E[]; layout: Layout };
function toXYFlow(layout: Layout, options?: ToXYFlowOptions): { nodes: IrekoNode[]; edges: IrekoEdge[] };
function PolylineEdge(props: EdgeProps): ReactElement;
function GroupNode(props: NodeProps): ReactElement;
const edgeTypes: { ireko: typeof PolylineEdge };
const nodeTypes: { group: typeof GroupNode };
function pathOf(points: readonly Point[]): string; // the SVG path of a polyline
interface ToXYFlowOptions { nodeType?: (node: PlacedNode) => string | undefined; edgeType?: string }
interface FlowNodeSpec { layout?: LayoutRef; mode?: Mode; fit?: Fit } // the layout settings of one node of a flow
interface IrekoNodeData extends Record<string, unknown> { label: string; ireko: PlacedNode } // the data of a node that toXYFlow makes
interface IrekoEdgeData extends Record<string, unknown> { points: readonly Point[] } // the data of an edge that toXYFlow or layoutFlow makes

Refer to the React Flow guide.

const examples: readonly Example[];
interface Example { name; title; description; input: LayoutInput; options?: LayoutOptions }

The gallery shows each example.