Skip to content
D
Documentation

Core

reference
3 min readUpdated

Import these from @grafloria/element.

Functions

defineGrafloriaFlow

Register the element. Idempotent, and safe to call on the server (where customElements does not exist) — which is what lets a bundle be imported from an SSR entry point without a typeof window dance at every call site.

ts
function defineGrafloriaFlow(tagName = 'grafloria-flow'): void

fromDocument

Turn a saved document back into something render() can mount.

ts
const json = JSON.stringify(new DiagramSerializer().serialize(api.getModel()));
// …later, in a fresh page:
render(fromDocument(json), host);

Accepts the flat serializer form, the portable envelope, or the JSON string of either.

ts
function fromDocument(
  document: SavedDiagram,
  options: FromDocumentOptions = {}
): LoadedDiagramSpec

getNodeType

ts
function getNodeType(type: string): NodeTypeRenderer | undefined

hasNodeType

ts
function hasNodeType(type: string): boolean

registeredNodeTypes

Every registered type name.

ts
function registeredNodeTypes(): string[]

registerNodeType

Register (or replace) a node type globally.

ts
function registerNodeType(type: string, renderer: NodeTypeRenderer): void

render

Mount spec into target and return the live instance.

target may be an element or a CSS selector. Custom nodes (custom: true) are rendered by the types registered with {@link registerNodeType}.

SCOPE, stated plainly: spec is data (an object or its JSON), not a Mermaid- style text DSL. The engine does have a DSL, but wiring it in is a separate card — render() is the embedding surface, not a parser.

ts
function render(
  spec: RenderSpec,
  target: HTMLElement | string,
  options: RenderOptions = {}
): DiagramInstance

renderFromTemplate

Render a node from a slotted <template data-node-type="...">.

Clones the template's content into element and substitutes node.data into every [data-field="key"] descendant's text. Values are written with textContent, never innerHTML: a diagram's data is frequently user-supplied, and a template engine that injected raw HTML here would be an XSS vector in every host that embeds us.

ts
function renderFromTemplate(
  template: HTMLTemplateElement,
  node: NodeModel,
  element: HTMLElement
): void

renderStatic

Server-side render. Re-exported so the tiny API is self-contained.

ts
function renderStatic(options: StaticRenderOptions = {}): StaticRenderResult

unregisterNodeType

Drop a registration (mostly for tests).

ts
function unregisterNodeType(type: string): void

Classes

GrafloriaFlowElement

ts
class GrafloriaFlowElement extends HTMLElementBase

Methods

  • static get observedAttributes(): string[] (static)
  • get nodes(): NodeSpec[]
  • set nodes(value: NodeSpec[])
  • get edges(): EdgeSpec[]
  • set edges(value: EdgeSpec[])
  • get diagram(): DiagramInstance | null — The headless instance — the escape hatch to everything else.
  • connectedCallback(): void
  • disconnectedCallback(): void
  • attributeChangedCallback(name: string, previous: string | null, next: string | null): void
  • fitView(padding?: number): void

Constants

Grafloria

The namespace object, for import { Grafloria } and for <script> globals.

ts
const Grafloria: { render: (spec: RenderSpec, target: string | HTMLElement, options?: RenderOptions) => DiagramInstance; renderStatic: (options?: StaticRenderOptions) => StaticRenderResult; registerNodeType: (type: string, renderer: NodeTypeRenderer) => void; registeredNodeTypes: () => string[]; define: (tagName?: string) => void; }

GRAFLORIA_EVENTS

Events emitted on the element. All bubble and cross shadow boundaries.

ts
const GRAFLORIA_EVENTS: { readonly ready: "grafloria-ready"; readonly nodesChange: "grafloria-nodes-change"; readonly edgesChange: "grafloria-edges-change"; readonly selectionChange: "grafloria-selection-change"; readonly connect: "grafloria-connect"; readonly nodeClick: "grafloria-node-click"; readonly edgeClick: "grafloria-edge-click"; readonly viewportChange: "grafloria-viewport-change"; }

Interfaces

DiagramSpec

What Grafloria.render() accepts: an object spec, or the JSON string of one.

ts
interface DiagramSpec

Properties

NameTypeDefaultDescription
nodes?NodeSpec[]
edges?EdgeSpec[]
groups?GroupSpec[]Zones around some boxes, each with its own frame and caption. See GroupSpec.
layout?'architecture''architecture': compose the drawing — zones as regions, boxes sized to their words in rows, straight lines where boxes line up. Positions are not needed.

FromDocumentOptions

ts
interface FromDocumentOptions

Properties

NameTypeDefaultDescription
renderWidget?WidgetRendererThe app's own widget painter — the same function it passed to dashboard({ renderWidget }). A board authored with a custom painter must be RELOADED with it, or the reload silently drops the app's chrome.
renderCustomNode?(node: NodeModel, host: HTMLElement)Full override of the custom-node painter. Outranks everything.
interactive?booleanRe-attach kit interaction wiring (row selection, in-canvas editing, the dashboard grid binder). Default true — a loaded diagram should behave like the one that was saved. Pass false for a read-only viewer.

LoadedDiagramSpec

What {@link fromDocument} returns: a render() spec, plus the way back in.

ts
interface LoadedDiagramSpec

Properties

NameTypeDefaultDescription
nodesNodeModel[]
edgesLinkModel[]
renderCustomNode(node: NodeModel, host: HTMLElement)
finalize(api: unknown)
modelDiagramModelThe deserialized model — the escape hatch, available before any render.
boardsMap<string, DashboardGridHandle>Live grid binders for the boards finalize() re-attached, keyed by group id. Empty for a document that is not a dashboard. This is the SAME Map instance the handle drives (handle.binderOf returns from it), so the two can never disagree — boards is now derived from the handle's own binders rather than a parallel copy.
handleDashboardHandleThe dashboard toolbar handle over the reloaded board(s) — the SAME DashboardHandle dashboard() returns, built by the one shared builder so it cannot drift from the authoring surface: addWidget/showView/setSizing/ setColumns/toJSON/exportIds and the widget handles, all live on the reload. INERT (empty views, every op a no-op) for a document that is not a dashboard — an ER/UML load carries no
renderOptions?{ minZoom?: number; maxZoom?: number }Instance options the loaded spec asks render() to apply (a fluid board pins zoom).

Types

NodeTypeRenderer

Fills element with the visual for node. Called once per mounted node.

ts
type NodeTypeRenderer = (node: NodeModel, element: HTMLElement) => void;

RenderOptions

ts
type RenderOptions = Omit<CreateDiagramOptions, 'nodes' | 'edges'>;

RenderSpec

ts
type RenderSpec = DiagramSpec | DashboardSpec | KitDiagramSpec | string;

SavedDiagram

Anything DiagramSerializer.deserialize() accepts, or the JSON string of it.

ts
type SavedDiagram =
  | SerializedDiagramData
  | DiagramDocumentEnvelope
  | Record<string, unknown>
  | string;

Was this page helpful?