Skip to content

API Reference

Exhaustive surface area of the Lego runtime. Every entry on this page maps directly to source under src/. If something isn't here, it isn't part of the public API.

Runtime

SymbolPurpose
Lego.init(root?, options?)Boot the engine. Discover templates, hydrate inline blocks, install router & loader, run startup-time route match.
Lego.block(tag, html, logic?, styles?, cascade?, error?, props?)Define a block from a string template.
Lego.defineLegoFile(content, filename?)Define a block from .lego SFC content (used by the Vite plugin and runtime loaders).
Lego.route(path, tagName, middleware?)Register a client-side route. Supports :params, * wildcards, and async middleware.
Lego.snap(el) / Lego.unsnap(el)Manually mount / unmount a block element.
Lego.mount(tag, target?, props?)Imperative mount with optional initial state.
Lego.db(key)Persistence descriptor builder (also exposed as $db inside blocks).
Lego.getLegos() / Lego.getActiveBlocksCount()Introspection helpers.
Lego.debug.stylesheets(el)Inspect resolved stylesheets for a block.

Reactive globals

SymbolPurpose
Lego.globalsReactive cross-block state. Mutating any property re-renders subscribed blocks.
Lego.globals.$routeCurrent { url, route, params, query, method, body }.
Lego.globals.$go(path, ...targets)Programmatic navigation. Returns { get, post, put, patch, delete }.
Lego.globals.$mountImperative mount helper (alias of Lego.mount).
Lego.globals.$dbPersistence factory (alias of Lego.db).

Configuration

SymbolPurpose
Lego.config.syntaxDelimiter style, 'brackets' ([[ ]], default) or 'mustache' ().
Lego.config.debugVerbose tracing.
Lego.config.onErrorLast-resort error handler when no boundary catches.
Lego.config.sanitizeHTML sanitizer used by b-html.safe.
Lego.config.metricsonRenderStart / onRenderEnd hooks.
Lego.config.loaderAuto-discovery loader for unknown hyphenated tags.
Lego.config.loaderAllowlistTag-name allowlist for the loader.

Block surface

Inside a block's logic (mounted, methods, getters), this exposes:

SymbolPurpose
this.$elementThe host element.
this.$varsDOM refs collected via b-var.
this.$parentNearest ancestor block.
this.$emit(name, detail?)Dispatch a bubbles: true, composed: true CustomEvent.
this.$route, this.$go, this.$mountMirror of the reactive globals.
this.mounted() / this.updated() / this.unmounted()Lifecycle hooks.

Inside template expressions ([[ … ]], @event="…", b-if/b-show/b-html/b-mount), the scope adds:

SymbolPurpose
stateThe block's own state (default scope of every expression).
globalReactive globals, read global.user.name, etc.
selfThe DOM node the binding is on.
event / $eventThe DOM event (event handlers only).
$ancestors(tag)Find the nearest matching block ancestor.
$registry(tag)Reactive shared state for a registered block tag.
$paramsShortcut for $route.params.
$go, $mount, $db, $emit, $elementSame as the JS-side helpers.

Directives

The complete set, all defined under src/directives/:

DirectiveEffect
b-ifConditional mount/unmount.
b-showToggle inline display.
b-forKeyed list rendering.
b-textPath-only text binding.
b-html / b-html.safeRaw HTML, optionally sanitized.
b-sync / .lazy .number .trimTwo-way form binding.
b-initOne-shot expression.
b-enter / .onceIntersectionObserver entry hook.
b-leave / .onceIntersectionObserver exit hook.
b-mountReactively mount a tag computed from state.
b-varCapture DOM refs into $vars.
b-logic / b-dataInject state at template or instance level.
b-propsDeclared prop contract.
b-idInline-template tag name.
b-errorError boundary fallback tag.
b-stylesheets / b-cascadeAdopt named constructable stylesheets.
b-target / b-link / b-actionRouting attributes on <a> / <form>.

Browser support

LegoDOM relies on:

  • Custom Elements v1 + Shadow DOM v1
  • ES6 Proxy
  • IntersectionObserver (only consumed by b-enter / b-leave)
  • CSSStyleSheet constructable + adoptedStyleSheets (used by b-stylesheets)

Tested in Chrome 90+, Firefox 90+, Safari 15+, Edge 90+. The directives that need IntersectionObserver and constructable stylesheets degrade gracefully on older browsers, neither throws, but the affected feature won't run.

Released under the MIT License.