extending

Extending Leaf

Packages add reusable widgets, themes and behavior to Leaf. Their contracts let each contribution work alongside other packages and the page's own content.

This guide covers the interfaces for implementing widgets, themes and packages, with a package tutorial and CSS contracts. How it works introduces Leaf's core concepts and how they fit together. UI covers composing and operating a page with existing capabilities.

In this topic

Who is responsible for what

The user sets the goal and preferences. The author composes the page; packages supply its visual language and widget behavior. Leaf core keeps the shared record of what the user does.

flowchart TD
  accTitle: Ownership boundaries in a Leaf page
  accDescr: The user gives the author a goal and selects a theme. Within the authored page, the author owns content and layout, and the widget package owns controls and action meaning. Theme inputs style both. Widgets send actions to Leaf core, which owns the record, current state and Undo.
  User([User])
  Theme["Theme package<br/>Color, type and spacing"]
  subgraph Page["Authored page"]
    Content["Page author<br/>Content and layout"]
    Widget["Widget package<br/>Controls<br/>Action meaning"]
    Content -->|places and configures| Widget
  end
  Core["Leaf core<br/>Record, state and Undo"]
  User -->|goal| Page
  User -->|theme preference| Theme
  Theme -.->|visual inputs| Page
  Widget -->|actions| Core
  Core -->|current state| Widget
Ownership architecture. The author places widgets inside the page; each package owns its widget’s behavior. Theme inputs style the page and its controls. Leaf core owns the record behind them.
Each owner’s authoring interface
Owner Responsibility Interface
User Goals, preferences, decisions, and edits. Instructions to the agent, actions on the page, and named personal themes with a default. Theme colors can follow the browser's color-scheme preference.
Page author Content, initial values, package selection, and composition. Semantic HTML, declared widget attributes, data bindings, page CSS, and optional page modules.
Widget package Domain meaning, controls, and rendering within the allotted box. Registry declarations, browser modules, scoped styles, and author instructions. The package connects its controls to Leaf's state, commands, and annotation APIs.
Theme package Reusable visual choices and presentation idioms. Theme inputs and explicit page-wide styles, independently of whether the package declares widgets.
Leaf kernel Shared state, event admission, Undo, and interaction services. The registry contract and public module APIs. Core interprets declarations; packages supply each widget's domain behavior.

These are contribution boundaries, not a sandbox: package and page scripts run in the same document. Each package's selectors decide where its rules match, and inheritance connects nested content's presentation. The CSS contract describes selector reach and cascade precedence. The package checks validate declarations and rendered behavior.

Choose the extension boundary

  1. One page. Semantic HTML, existing Leaf elements, a page-local <style>, and inline SVG shape one document. Keep presentation here when no other page needs it.
  2. A package. A reusable element, theme, browser behavior, or author instructions belong in a package. A project-local package, an installed package, and a package bundled with Leaf all use the same composition format.
  3. Leaf's kernel. Runtime, chrome, and protocol behavior shared by every page belongs in Leaf itself. A package consumes those capabilities; it does not add a parallel runtime path.

Package interfaces

Packages connect through declared inputs, outputs and actions. A release review can use one package to read a patch and another to navigate its files, without either package knowing the other’s implementation.

Data connects widgets

flowchart TD
  accTitle: A typed output connects independent widgets
  accDescr: The author supplies a patch. The Diff widget publishes its changed files using the file-list contract. The page binds a File tree widget to that output, without either widget knowing the other's implementation.
  Patch["Author supplies a patch"] --> Diff["Diff widget"]
  Diff -->|files: file-list| Tree["File tree widget"]
The page connects these widgets with for="release-patch". The producer and consumer agree on the file-list value’s shape. The file tree can also read a stored source with that contract. A published output is a browser-local derivation, not a decision in the page’s log.

Widgets delegate recorded state to Leaf

flowchart TD
  accTitle: A choice is drawn immediately and recorded by Leaf
  accDescr: The user's choice starts two paths. The browser draws it before the next frame, while Leaf validates the action and appends it to the log. Shared state then reconciles the browser's selection.
  Choose["Choose Canary clients"]
  Choose --> Draw["Browser<br/>Draw now"]
  Choose --> Admit["Leaf<br/>Admit action"]
  Admit --> State["Shared state"]
  Draw --> Reconcile["Browser<br/>Reconcile choice"]
  State --> Reconcile
The package declares choose and the schema for its selected IDs. The browser shows what the gesture decides while admission completes; a refused action restores the server’s state. Undo withdraws the admitted action, and the widget draws the resulting selection. Packages share these guarantees. How it works explains how Leaf derives state from the record.
Independent capabilities, validation and package selection

Leaf's core, called the kernel, supplies the browser runtime, shared controls, and collaboration protocol. Packages supply reusable elements, JavaScript modules, styles, and authoring instructions. The default package provides common widgets and structural elements; optional packages add capabilities such as diagrams and diffs. Project packages use the same format and can replace or extend the page's selected layer. The package catalog shows the bundled packages; the tutorial builds a small custom package.

A package declares capabilities independently: what an action records, whether it decides a proposed change, which typed data it consumes or publishes, and where its styles apply. Ordinary data fields do not acquire decision meaning from their names. Two occurrences of the same widget can select different input contracts without replacing the widget's declaration. Leaf validates lasting input where it enters the event log; the render check validates the outputs a widget actually publishes during the check. The state and data contract defines those interfaces.

page init records the page's package and theme selection. The Leaf serving, checking or exporting the page composes its layer from that selection and supplies the browser runtime. Each revision captures the authored content, page-owned declarations and modules, and the package selection. Historical revisions run with the current Leaf's runtime and composed layer while retaining their authored content and recorded decisions. The package contract defines how packages are combined and replaced.

In this topic

Build a package with two files

Create a package in the project, then check the empty composition:

$ leaf package init packages/callout
$ leaf package check packages/callout

The widget in this tutorial needs only these files:

packages/callout/
  registry.json
  defaults.css

Every package beyond the bundled default joins a page with --package. In the commands below, PAGE is the page directory to create or update:

$ leaf page init --package packages/callout PAGE

The complete package contract covers installed and bundled packages, contribution directories, composition order, and replacement rules.

package init also creates empty contribution directories for instructions, browser modules, widgets, and vendor files. The CSS-only widget here leaves them untouched.

To start with behavior instead, name the first element while creating the package:

$ leaf package init packages/risk-notes --widget lf-risk-note

Leaf writes a valid markup-widget element declaration and its one-shot browser module, then checks the complete candidate before changing the package. The tutorial below stays with the smaller CSS-only form so each contract remains visible. A behavior module imports Leaf's package API from /runtime/widget-api.js; it declares each interactive capability once, as the lfCommands of the element it applies in. The same live command rows feed keyboard dispatch, help, accessibility shortcuts, and—when decision names the action—the controls shown inline for an obligation.

Style the element

A package's defaults.css joins Leaf's existing cascade as written, so its rules name the elements they style, starting with lf-callout itself:

lf-callout {
  display: block;
  padding: var(--sp-3);
  border: 1px solid var(--rule);
  border-radius: var(--r);
  background: var(--field);
  --lf-block-frame: 1;
}

--lf-block-frame: 1 tells the shared layout that this rule draws the widget's inset. The style contract distinguishes theme inputs such as --accent from layout declarations, runtime outputs, and vendor bridges. The CSS contract explains how widget, theme and control sheets compose.

Declare the element

The page will author the callout as ordinary readable markup:

<lf-callout id="migration-warning">
  <strong>Migration window</strong>
  Writes pause for about two minutes.
</lf-callout>

Its complete registry.json entry validates the attributes and tells Leaf that the body is prose. x-example is the sample an author queries with the entry:

{
  "lf-callout": {
    "description": "A short warning or constraint that belongs in the reading flow.",
    "type": "object",
    "properties": {
      "id": { "type": "string" }
    },
    "required": ["id"],
    "additionalProperties": false,
    "x-content": "markup",
    "x-upgrade": false,
    "x-example": "<lf-callout id=\"migration-warning\"><strong>Migration window</strong> Writes pause for about two minutes.</lf-callout>"
  }
}

x-content and x-upgrade are the two Leaf declarations every widget requires. The ordinary JSON Schema fields validate its attributes, and Leaf adds the grammar an id follows; description and x-example make the merged registry useful to an author.

The registry-key reference lists every x-* declaration. A CSS-only widget stops here.

Compose and check the page

$ leaf package check packages/callout
$ leaf page init --package packages/callout PAGE
$ leaf page vocabulary PAGE | jq '.["lf-callout"], .["$keys"]'
$ leaf page check PAGE --render

A page keeps its own copy of a package it selects by path, so re-running page init --package is the explicit update for an existing page.

leaf package install packages/callout copies the checked package into ~/.local/state/leaf/packages/, after which --package callout reaches it by name from any project on this machine.

Registry keys
The generated reference for declarations such as x-data and x-state.
Event log
How a widget's x-state verbs become logged events and folded state.
Package contract
Composition, instructions, browser modules, external data, and validation.
Bundled default package
A complete package containing the widgets used across Leaf pages.
In this topic

CSS contracts

Widget packages style their own elements; theme packages supply presentation across the page. Stylesheet roles set where rules reach, and the cascade sets which rule wins. Themes in the UI guide covers selecting a theme and making local page adjustments.

Which rule wins

Leaf sorts CSS into tiers by who wrote it. The tiers are CSS cascade layers (@layer), and a rule in a higher tier wins. Each switch below puts the rule beside it into the tier its page or package writes into, for the same card, so the card shows what the browser decides.

Rules in tier order, highest first. The card takes its padding from the highest rule that is on, which is outlined.

A lower tier's rule loses however specific its selector is, as the widget package's lf-card.featured shows. Within a tier, the more specific selector wins, and between equally specific selectors the later rule wins. !important reverses the order of the tiers, so a page's !important loses to Leaf's own; an ordinary rule is all an override needs.

The tiers

The page’s document

flowchart TD
  accTitle: CSS precedence in the page document
  accDescr: For normal declarations, unlayered page rules have the highest precedence, then the page's own layers, lf-state, lf-overrides, lf-layouts, lf-base and lf-reset. Each arrow points from a higher tier to a lower tier.
  Page["Page rules · unlayered"] --> Layers["Page's own layers"]
  Layers --> State["lf-state · Leaf"]
  State --> Theme["lf-overrides · packages"]
  Theme --> Layout["lf-layouts · Leaf"]
  Layout --> Base["lf-base · Leaf and widgets"]
  Base --> Reset["lf-reset · Leaf"]

A widget’s shadow tree

flowchart TD
  accTitle: CSS precedence inside a widget's shadow tree
  accDescr: For normal declarations, Leaf's marks have the highest precedence, then lf-state, lf-overrides, lf-shadow and lf-base. Page rules and Layouts do not enter the shadow tree.
  Marks["Marks · Leaf"] --> State["lf-state · Leaf"]
  State --> Theme["lf-overrides · theme packages"]
  Theme --> Shadow["lf-shadow · Leaf and widgets"]
  Shadow --> Base["lf-base · widget defaults"]
Normal declarations, highest precedence first. Leaf’s lf-state hides what a decision removed; lf-reset clears the browser’s form-control styles; marks are the highlights Leaf draws. !important reverses the layer order.
Which styles contribute to each tier?

A page’s unlayered rules come first. Leaf’s panel, cards and marks are also unlayered and appear later, winning a tie. The page’s own @layer blocks follow Leaf’s layers, so they outrank them.

Tier Contributions
lf-overrides Each package's overrides.css, in package order.
lf-layouts Leaf’s Layout classes, such as layout-column.
lf-shadow Leaf’s shared shadow rules and each package’s widget-shadow.css.
lf-base In the document: Leaf’s base.css, then packages’ defaults.css and widget-shadow.css. In a shadow tree: defaults the widget adopts with inBaseLayer().

Page rules and Layouts stop at the shadow root. Inherited inputs such as --accent cross it. A widget can also expose a part for styling with ::part.

A nested widget owns its interior

Put a choice inside a release review: the review package arranges the choice's box, and the default package styles its Options widget inside. Leaf adds no boundary between them, so the review package keeps its rules off the choice's interior by naming its own structure: a child combinator such as lf-release-review > p, or its own classes. Shared theme inputs, such as color and type, flow through both.

flowchart TD
  accTitle: A nested widget keeps its own styling when the outer package names its own structure
  accDescr: Shared theme inputs inherit through the review widget and its nested Options widget. The review package styles its own heading and evidence with selectors that name them, and arranges the nested widget's host box. The default package styles the Options interior.
  Theme["Shared theme inputs<br/>Color, type and spacing"]
  subgraph Review["Review widget"]
    Content["Review package<br/>Heading and evidence"]
    subgraph Host["Nested widget host"]
      Options["Default package<br/>Options interior"]
    end
    Content -->|arranges| Host
  end
  Theme -.->|inherit through both| Review
Each package names what it styles. The review package's rules reach the nested host and stop short of its interior because they name the review's own children and classes; a bare descendant rule such as lf-release-review p would reach inside. The theme's colors and type reach both widgets.

These are styling conventions, not a security sandbox. Page and theme rules deliberately have wider reach; packages run in the same document. A widget's presentation depends on its allotted box and supported theme inputs, so it can move between page layouts.

Choose a stylesheet by where its rules should reach

A package can contribute widget, theme and control styles independently. Declaring a widget does not change the reach of its other sheets.

Stylesheet Reaches
defaults.css The page document, below Leaf's Layouts.
widget-shadow.css The page document and every declared shadow tree; :host(lf-tag) keeps a rule to its widget's own tree.
overrides.css The page document, above Leaf's Layouts.

Leaf adds no scope to a sheet, so each rule names the widget it styles: lf-callout .title in the document, and :host(lf-callout) .title in a widget's shadow tree. Shadow trees form a browser boundary: document selectors do not enter them, but inherited inputs and public ::part selectors can.

The package reference defines the stylesheet roles and supported inputs.

Choose a bundled package

The core product gallery shows Leaf’s default widgets and controls. The pages below demonstrate optional packages; the Code review package provides authoring instructions.

Playground also powers the structured data explorer and the built-output A/B comparison. Those pages keep their task-specific state, schema, and behavior while reusing the same package mechanics.