Extensions API 1.x reference

New to extensions? Start with the Extensions guide, which explains how they work and walks through writing one. This page lists every key and limit.

EmVB’s Extensions API lets a site owner add custom elements, editor panels and read-only data sources at build time. Editors cannot install extensions from the admin. EmVB is in-process, so extension code is not sandboxed; stored data and render output are still checked.

The API version is extensionApi: 1 (EXTENSION_API_VERSION).

Registering

import { emvb } from "@perkyzz/emvb";
import hello from "emvb-ext-hello";

emvb({
  canvasStyles: ["/site.css"],
  extensions: [hello],
});

Use defineExtension({ ... }) from @perkyzz/emvb so extensionApi: 1 is set. See examples/emvb-ext-hello for a full sample (element + panel + source). An extension can register up to 128 elements; any beyond that are ignored.

Namespace

Stored element types are ext:<package>/<name>:

  • package id: ^[a-z][a-z0-9-]{0,63}$
  • name: ^[a-z][a-z0-9-]{0,31}$

Path escape (..), extra colons, HTML/control characters and collisions with built-in element types are refused at registration. An element whose name is a built-in type (menu, grid, button, …) is left out, and the site build prints a warning naming it (1.5.1); give it another name, such as my-menu. A stored type that is not a valid extension type still saves and never runs extension code.

Element contract

An element declares:

  1. schema — a declarative Zod subset (string / number / boolean / enum / array / object). EmVB compiles it. Do not pass a Zod instance or other code as a schema. EmVB’s limits (MAX_TEXT_LENGTH, depth, array size) always apply on top.
  2. fields — declarative inspector fields (text, textarea, number, int, select, boolean, href) mapped onto existing Kumo controls.
  3. render(props, ctx) — returns EmVB layout nodes of built-in types only (never HTML strings, never nested ext: types). Output is re-validated through EmVB’s layout schema; links, media URLs, SVG and text go through the same sanitizers as editor-saved content. Style on the returned nodes is dropped; style the host element in the editor instead, or use the extension’s own classes (see below).
  4. defaults — optional starter props for the Add panel.

ctx.sources holds this package’s resolved read-only source values.

ctx.mode says where the output is drawn: "editor" on the editor canvas, "public" on the public page, in the HTML export and in previews of the page. ctx.childCount is how many children are stored in the element, counted before the slot’s accepts filters them.

Editor affordances only. ctx.mode is for what an editor needs and a visitor must not see: draw a modal, drawer or collapse open so its content can be edited, or show a hint in an element with no children (ctx.mode === "editor" && ctx.childCount === 0). The public page never renders "editor", so nothing a visitor reads or interacts with may depend on it: the same content, the same links, the same behaviour, only the editor’s own help differs. EmVB asks for and caches the output of each mode and child count separately.

Classes, stylesheet and runtime (extensionApi 1.x)

These keys are additive and optional. An extension without them works exactly as before.

import { defineExtension } from "@perkyzz/emvb";

export default defineExtension({
  id: "emvb-ext-hello",            // the package name (unscoped), or package.json "emvb.extension"
  module: import.meta.url,         // this module; needed for css, runtime and server bundling
  classes: { prefix: "hello-", allow: ["hello-card", "hello-title", "hello-wave"] },
  css: "./hello.css",              // relative to `module`, inside the package
  runtime: "./hello-runtime.js",   // relative to `module`, inside the package
  elements: [
    {
      name: "greeting",
      // ...
      render: (props) => ({
        id: "card0001",
        type: "div-block",
        props: {},
        extClasses: ["hello-card"],
        children: [/* nodes; each may carry extClasses too */],
      }),
    },
  ],
});

module

The extension module’s own import.meta.url (a local file: URL). EmDash bundles a plugin only through its entrypoint, so when an extension names its module, emvb() writes a small server entry at <site>/node_modules/.emvb/server-entry-<hash>.ts that imports the module and EmVB (both by absolute path), registers the extension and re-exports createPlugin. This is what makes extension elements render on Cloudflare Workers and on production builds, where astro.config never runs. Without module, an extension renders only in a Node dev server (as in 1.0–1.3); its manifest (Add panel, inspector, classes, stylesheet) still reaches production servers through the plugin options. The module must sit in a package whose package.json name (without scope) equals the extension id; package.json files without a name (such as dist/esm/package.json with only "type": "module") are skipped when looking for it.

  • <site> is the folder passed to astro --root, else the folder the build runs in (EmDash gives plugins no Astro hook, so emvb() reads the CLI’s arguments).
  • The entry is written to a temporary file and renamed into place; older server-entry-* files are removed.
  • astro build fails with an error naming the file when the entry can’t be written (a read-only node_modules, a site path with a quote, backslash or control character, which EmDash would write unescaped into its import). Without the entry, every extension element would render as a placeholder. astro dev only warns.

classes and extClasses

  • prefix: 2–16 characters, lowercase letters, digits and single hyphens, ending in - (d-, acme-ui-). It may not start with emvb. A refused prefix drops the whole allowlist (the extension still registers, without classes).
  • allow: up to 1,024 names, at most 64 characters, starting with the prefix and longer than it. A name starts with the prefix, then uses letters of either case (spectrum-Button), digits, -, and : / ! . _ @ (uk-width-1-2@m). Square brackets, spaces, quotes and markup are not allowed. Names are matched exactly and never parsed: whatever a name means comes from the kit’s CSS. Other names are dropped one by one.
  • Every name starts with the prefix, including a responsive one. Tailwind builds a prefixed variant class variant-first (sm:daisy-card-side), so a kit that ships daisy-sm:card-side asks Tailwind for the variant-first name and renames the selector in the built CSS. The allowlist and the CSS must use the same, prefix-first, spelling.
  • A node returned by render() may carry extClasses: string[]. EmVB keeps only exact names from this extension’s allowlist, at most 16 per node, without duplicates; anything else (free text, names with spaces, quotes or <, emvb-* names, unlisted names) is dropped. They are added after the built-in element’s own classes (class="emvb-heading hello-title"), escaped by the core serializer.
  • extClasses is render output, not stored data: it never enters a layout, and the layout schema is unchanged. Class names never come from props or editors.

extVars (extensionApi 1.5)

A node returned by render() may carry extVars, CSS custom properties for that node:

render: (props) => ({
  type: "heading",
  props: { text: `${props.percent}%`, level: 2 },
  extClasses: ["daisy-radial-progress"],
  extVars: { "--daisy-value": String(props.percent) },
});
  • A name must start with -- and the extension’s class prefix (--daisy-value for the prefix daisy-), with something after it. A kit whose CSS reads fixed names lists them in classes.vars (vars: ["--tw-ring-color"], up to 64 names). Any other name is dropped, so an extension cannot set a site variable such as --color-primary. Names that start with --emvb are never allowed.

  • A value is one number, length, percentage, angle or colour, and nothing else:

    • number: 70, -1.5, .5
    • length: 12px, 1.5rem, 50vw (the units EmVB’s own styles take: px, rem, em, vw, vh, ch)
    • percentage: 50%
    • angle: 90deg, .25turn (deg, grad, rad, turn)
    • colour: #fff, #1a2b3c, #1a2b3c80, transparent, currentColor, or a function with three or four numeric arguments: rgb(0 0 0 / 50%), hsl(120deg 50% 50%), oklch(70% 0.2 150) (rgb, rgba, hsl, hsla, hwb, lab, lch, oklab, oklch). Named colours such as red are not accepted, as in EmVB’s own style colours.

    A list of tokens (70px 2, 1px solid red), var(), calc(), exponents (1e3) and any other keyword are dropped. The value must also pass the style value check: at most 200 characters, no { } ; < > \ " ', no backtick, no url(, no comments, no newlines. A value that fails is dropped and the rest stay. At most 16 variables stay on one node.

  • They are written into the node’s own rule, .emvb-e-<id>{--daisy-value:70}, never into a style attribute, so the public page, the editor canvas and the HTML export carry them the same way. The value is read when the page is rendered on the server.

  • To change a value in the browser, the extension’s own runtime calls el.style.setProperty("--daisy-value", n). The class allowlist limits only what the server writes, not what a script does on the page.

  • An extension with no classes declares no prefix, so it can set no variables. One that only sets variables and has no class of its own declares classes: { prefix, allow: [], vars: [...] }; a list of vars keeps the declaration even though allow is empty. With neither a class nor a valid variable, the declaration is dropped.

  • Like extClasses, extVars is render output, not stored data.

css and runtime

  • Paths are relative to module (./file, ../dir/file), must stay inside the extension’s package, and end in .css (stylesheet) or .js/.mjs (runtime). URLs (https:, data:, //host), absolute paths and \ are refused, with a build-time warning.
  • They are read once at build time by emvb(), and their text travels in the plugin’s serialized options. Nothing is fetched or import()ed at request time.
  • Limits: stylesheet 512 KB, runtime 256 KB. A stylesheet containing </style is refused; a runtime containing <script, </script or <!--, or a line starting with import or export, is refused.
  • The runtime must be a self-contained browser script (wrap it in an IIFE). Public pages write it inline as <script type="module"> (deferred, strict); the HTML export copies it to assets/ext/<id>.js and loads it with a classic <script defer>, like EmVB’s own export scripts, because browsers don’t load module scripts from pages opened as file: URLs. It must work in both: no imports or exports, no top-level await, no sloppy-mode-only syntax (with). The build compiles it (never runs it) as a strict classic script and refuses it with a warning when it doesn’t parse. Sites with a Content Security Policy must allow it, as for EmVB’s own inline page styles.
  • Scoping (extensionApi 1.5): the stylesheet is scoped to the extension’s own wrapper, .emvb-ext--<id>, so it cannot restyle the page around its elements.
    • A selector with no class (h1, button, *) becomes :where(.emvb-ext--<id>) h1, which adds no specificity.
    • @scope and @import are dropped: bundle imported CSS into the stylesheet. A rule ending in an emvb class is scoped like a rule with no class, since those classes are EmVB’s.
    • A leading html or body is the wrapper itself, and its qualifiers move onto it: body button is :where(.emvb-ext--<id>) button, body.dark button is :where(.emvb-ext--<id>).dark button, and html.dark body button is the same. The qualifier is a class or attribute on the wrapper, so the extension’s runtime sets it (dark on its wrapper when the host is dark). A page’s own dark class on <html> does not reach the kit.
    • The document’s variables (:root, :host, unprefixed daisyUI palette included) are declared on the wrapper, not the page root, so a kit’s --color-primary can’t overwrite the host’s. The kit’s elements read them as usual.
    • Theme. A kit rule keyed on [data-theme=X] applies when the host root carries it, when the host root carries EmVB’s data-emvb-theme=X (the Theme toggle and the pre-paint choice), when the wrapper carries data-theme=X (the extension’s own theme), or when an element inside it does. A kit’s default rule (:root:not([data-theme])) does not apply when data-emvb-theme is set, so the host’s choice beats the operating system’s.
    • A class selector is left as written, except for its leading root, which is still mapped. This assumes every class in the stylesheet is the extension’s own, so every class it names must start with the extension’s prefix, including a class that only the runtime adds. The build warns about each class that does not (.page-heading would style content outside the extension); it is a warning, not a refusal.
    • A rule that changes nothing keeps its exact bytes, so a stylesheet of the extension’s own classes is emitted exactly as written.
  • Emitted only where used: a public page gets the stylesheet and the runtime only when it, or one of its theme parts, renders at least one of the extension’s elements, and each once per response: a page with the element in its header, footer and body ships the stylesheet once (in the theme parts’ css when a part uses it, else in EmVBPage’s style) and the runtime once. An element that can’t render (missing extension, throwing render) emits neither. Pages without the extension’s elements get no extra CSS and no extra script.
  • Editor canvas: the canvas iframe gets the stylesheet when it shows one of the extension’s elements. The runtime never runs in the canvas (the canvas is sandboxed without scripts) or in the admin. Extension CSS never goes into the admin UI itself.
  • HTML export: the stylesheet is in each page’s style.css (once, however many of the page’s renders use it), the runtime in assets/ext/.
  • Hosts: render pages with <EmVBPage> and write the theme parts’ css, as the host guide does. The css that resolveEmVBPage() returns doesn’t hold extension stylesheets (its extensions lists the extensions the page rendered); <EmVBPage> writes them.

Canvas and export output

The admin bundle holds no extension code. The editor canvas and the HTML export ask the server (POST /_emdash/api/plugins/emvb/editor/extensions/render, editors and above) for each extension element’s output by node id, type, mode, child count and props (two elements with the same props each get their own), then check it again against the layout schema and the allowlist before rendering it. The route takes at most 100 elements per request and refuses props nested deeper than 24 levels or larger than 512 KB. The canvas asks once edits pause (300 ms), asks again after a failed request (up to three times, then on the next change), and forgets the output of elements no longer on the page.

The canvas shows what the public page would show: an extension without module renders on the canvas wherever it renders publicly (a Node dev server, where astro.config runs in the server’s process). On Cloudflare Workers and production builds its code isn’t in the server, so the canvas shows the placeholder (“Missing extension”) and visitors see nothing; set module to fix both.

What the package is responsible for

EmVB’s accessibility check can’t see inside extension markup or scripts. The package owns:

  • Reduced motion: a runtime or stylesheet that animates must honour prefers-reduced-motion (the sample only waves under no-preference).
  • Keyboard use: anything interactive must work with the keyboard and show focus.
  • ARIA: roles, names and states for anything the runtime makes interactive.

Slots and parts (extensionApi 1.5)

An element can hold stored children in a slot. Declare it in the element, and mark where the children go in the output with { type: "ext-slot", name }:

{
  name: "card",
  // ...schema, fields, render
  slots: [{ name: "body", label: "Body", accepts: ["heading", "text"] }],
  render: () => [{ type: "ext-slot", name: "body" }],
}
  • One slot per element in 1.5. An element declaring two is not registered.
  • accepts lists the child types the slot takes: built-in types as they are, or this extension’s element names. Without accepts, any child.
  • parts lists this extension’s element names that may only live inside this element (a tab panel inside tabs). A part is refused anywhere else in the editor.
  • A marker whose name the element does not declare renders nothing.
  • The children render directly where the marker is, with no wrapper element of their own, so a kit’s .card-body > h2 still matches. They are the author’s content: they keep their own ids and links exactly as outside an extension. Only the extension’s own output is id-scoped.
  • Don’t put a slot directly inside table or list markup (table, tbody, tr, ul, ol). The children are built-in elements and other extension elements, which render as divs, and a div is not valid there. Put the slot inside a td or an li.
  • Children are ordinary stored nodes, so the layout format is unchanged. The slot marker is output only; it is never stored.
  • If the extension is missing, its children stay saved and visitors see nothing, as for any extension element.

The drop rules apply wherever the editor places an element: drag and drop on the canvas and in Layers, the Add panel, paste, and proposals. Saving a page does not re-check them. A child that breaks them is kept, and the page draws it only if the slot’s accepts list allows it.

Panels

Panels are declarative. EmVB renders them in a Kumo slot with EmVB tokens. An extension must not inject global CSS into the admin. A panel may name one of its extension’s sources (source: "office"); the panel then lists that source’s values read-only, as label and value pairs in plain text.

The admin has no extension code, so the editor asks the server (GET /_emdash/api/plugins/emvb/editor/extensions/sources, editors and above) once it opens. The server resolves every source a panel names (async sources are awaited, for at most 5 seconds each) and cleans the values as for render(). A source that throws, doesn’t answer in time, or whose code isn’t in the server (an extension without module on Cloudflare Workers or a production build) shows “This source’s values couldn’t be read on this server.” Sources a panel doesn’t name are never resolved by this route.

Sources

Sources are read-only resolvers with declared capabilities (text, url, image). Results are cleaned like bound values before anything uses them: keys must match ^[a-zA-Z][a-zA-Z0-9_.-]{0,63}$, non-string values are dropped, at most 32 values are kept, and each value loses control characters and bidi overrides and is cut at 2,000 characters. render() gets synchronous sources only (ctx.sources); the editor’s panels also show async ones. They are not writable and are not a substitute for EmDash collections.

Missing extension

Removing an extension (or shipping a page to a site that never had it) leaves the page valid and keeps the element and its props. It renders again once the extension is back. Until then:

  • Public pages and the HTML export: nothing in its place, so visitors never see it. Its stylesheet and runtime are not written either.
  • Editor canvas: a placeholder, “Missing extension “ext:pkg/name””.
  • Admin previews of the public page (field mode’s Preview) and EmDash draft previews (_preview links): a placeholder, “This block needs an extension that is not installed.” (.emvb-ext-missing, role="note").

A throwing or illegal render is treated the same way. Siblings keep rendering. Hosts calling renderPage with mode: "public" can pass extensionPlaceholders: true to get the placeholder in their own previews.

Stability and deprecation (extensionApi 1.x)

Same freeze rules as the schema contract:

  • Within extensionApi 1.x: no key on the public extension surface is removed, renamed or changed in meaning.
  • New keys are additive and optional.
  • Unknown keys from a newer 1.x on a manifest EmVB does not know are ignored.
  • Limits (string length, depth, counts) may only loosen.
  • Breaking changes wait for extensionApi: 2, with a migration note and at least one 1.x release of deprecation warnings in the CHANGELOG.

The sample package emvb-ext-hello is illustrative; its element and source names are not part of the frozen surface.