Skip to content

Let a widget work off a Leaf page by taking Leaf's features through standard properties, attributes and events #3833

Closed

Description

@max-sixty

What happens

A package widget runs only on a Leaf page. It extends LeafElement and imports its helpers from /runtime/widget-api.js, which only a Leaf page serves. So a widget built for Leaf can't be reused in another app, such as a Svelte app showing the same data, though most of its code is plain DOM.

For example, a conversation viewer widget is about 970 lines. About 770 of them, the drawing, folding and filtering, would run off Leaf given a Markdown renderer and three small helpers. The rest reaches Leaf through calls: projectData for comments on drawn items, commands, rowWalk and paintKeys for keys, copyBlock for copy, this.data() for the bound source, this.drawing() for settling, and loadMarkdown, renderMarkdown and highlightBlocks. Reusing it means splitting it into a plain core and a Leaf wrapper, and every widget we want to share needs the same split.

Proposal

Leaf finds and augments standard elements, properties and events, and a widget imports from Leaf only what no standard form expresses. Off a Leaf page, the same widget works without those features. Some ways to get there:

  • Values in as properties. Leaf sets a bound source on a property the registry names, such as .value, and the widget redraws when it is set. Elsewhere the host sets it. Today a widget pulls it with this.data(name).
  • Settling through a standard promise. Leaf awaits an element's updateComplete, as Lit elements have, rather than requiring this.drawing(promise).
  • Platform events where they exist. Before moving to a search hit or an anchor inside a widget, Leaf fires beforematch, the event hidden="until-found" uses for the browser's find, instead of lf-reveal. A widget that opens for the browser's find then opens for Leaf's search too.
  • Declarations as attributes. A widget marks drawn items with attributes for key, identity and label, and announces a redraw with an event carrying its snapshot, instead of calling projectData. Buttons declare commands through invoker commands (commandfor and command) or aria-keyshortcuts, which Leaf's key handler, key help and hints read. Outside Leaf the attributes do nothing and the buttons still work.
  • Augment elements rather than wrap them. Leaf already adds copy to document code blocks. It could also add its status line and key to any wa-copy-button it finds, through wa-copy and wa-error, so a widget uses Web Awesome's button directly instead of copyBlock.
  • An importable API with plain fallbacks. For what stays a call, a widget imports the API by a bare specifier. A Leaf page maps it to the runtime with an import map. Elsewhere it resolves to a small package of plain versions: keepsText and holdFocus as they are, sizeObserver as a ResizeObserver, renderMarkdown through markdown-it, and no-ops for comments and keys.
  • Tokens with defaults. Leaf publishes its design tokens as a stylesheet another app can load, so a widget's var(--…) values resolve the same way off Leaf.
  • A portability check. A check loads each package widget on a bare HTML page without Leaf's runtime and reports what fails.

Why

With these, a team can build a widget once and use it on Leaf pages, with comments, keys and search, and in its own apps without them, without keeping a second copy of the widget.

Seen on leaf f97ce55, installed 2026-10-24T13:56:05-07:00.

This was written by Claude Code on behalf of max-sixty

Activity

  1. added
    enhancement
    on Oct 24, 2026
  2. leaf-agent commented on Oct 24, 2026

    @leaf-agent
    Collaborator

    The first contract to settle is how a host-neutral widget hands Leaf its rendered data. projectData currently stamps a source revision and each datum’s identity and origin onto the nodes shown together. An attribute-and-event replacement needs to preserve that single snapshot, including when an asynchronous draw finishes after a newer source arrives. A useful first slice would specify that handoff for one data-backed widget and exercise the same widget module on a Leaf page and a plain page.

    I’m leaving this as an open design issue rather than opening a PR for the full proposal. The keyboard portion needs its own ownership choice: Leaf’s command register currently projects aria-keyshortcuts from live bindings. Reading that attribute as the binding source would change which side defines a command, beyond making a widget portable.

  3. max-sixty commented on Oct 31, 2026

    @max-sixty
    OwnerAuthor

    Closing: the proposals here have landed for the display widgets, and the two parts left over have their own issues.

    Where this landed differently from the proposal: keys are a declaration on the element that Leaf reads, not invoker commands or aria-keyshortcuts; and Copy is an attribute Leaf augments, not wa-copy-button.

    Left over:

    This was written by Claude Code on behalf of max-sixty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions