Repository navigation
Description
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 withthis.data(name). - Settling through a standard promise. Leaf awaits an element's
updateComplete, as Lit elements have, rather than requiringthis.drawing(promise). - Platform events where they exist. Before moving to a search hit or an anchor inside a widget, Leaf fires
beforematch, the eventhidden="until-found"uses for the browser's find, instead oflf-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 (commandforandcommand) oraria-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-buttonit finds, throughwa-copyandwa-error, so a widget uses Web Awesome's button directly instead ofcopyBlock. - 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:
keepsTextandholdFocusas they are,sizeObserveras aResizeObserver,renderMarkdownthrough 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
leaf-agent commentedon Oct 24, 2026
The first contract to settle is how a host-neutral widget hands Leaf its rendered data.
projectDatacurrently 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-keyshortcutsfrom live bindings. Reading that attribute as the binding source would change which side defines a command, beyond making a widget portable.Give data-bound widgets their value as .value and take their drawn da…
Reveal travel targets through beforematch instead of lf-reveal (#4500)
Make layoutChanged only dispatch its event (#4509)
Let find-in-page reach folded thread messages (#4518)
Let lf-chart run on a plain HTML page through a portable helper surfa…
max-sixty commentedon Oct 31, 2026
Closing: the proposals here have landed for the display widgets, and the two parts left over have their own issues.
.value, and a widget declares what it drew withdata-lf-datumattributes and adrawnevent;projectDataandthis.data()are gone from the packages./runtime/portable-api.js(Let lf-chart run on a plain HTML page through a portable helper surface #4542), which a plain page reaches with an import map. Compose each page's layer from the Leaf reading it, so only the log and records bump the protocol #4551 composes each page's layer from the Leaf reading it, so the package API changes without a protocol bump.lfCommands, Let lf-code run on a plain HTML page, and declare package keys as data Leaf reads #4574), copy withdata-lf-copy, settling with apending-taskevent and failures with error events; off Leaf it binds no keys and shows no Leaf controls.hidden="until-found"and firesbeforematchbefore it travels into a widget, so a widget that folds that way opens for the browser's find and Leaf's alike.tests/smoke/test_plain_page.pyloads all five on a bare page at every landing.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, notwa-copy-button.Left over:
.valuecontract on a plain page, are untested.