Skip to content

Let lf-chart run on a plain HTML page through a portable helper surface - #4542

Merged
max-sixty merged 7 commits into
mainfrom
agent-aeab1289051cc5546
Oct 28, 2026
Merged

max-sixty merged 7 commits into
mainfrom
agent-aeab1289051cc5546

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

lf-chart now loads and draws on a plain HTML page that maps Leaf's /runtime/ and /vendor/ paths with its own import map and runs none of Leaf's runtime. On a Leaf page it behaves as before. This is the first slice of #3833's portable display widgets.

Before this, a bare page couldn't define any package widget. widget-api.js pulls in 178 runtime modules, and even framework.js throws at load, because its imports reach modules that read what Leaf composes into a page: control-selectors.js reads prepaint's lfInputs, stylesheets.js the delivered sheet JSON, and storage.js lfStorage. lf-chart's import graph is now 11 runtime modules and 68 KB of source, down from 179 and 2,251 KB.

Portable entry. A new /runtime/portable-api.js re-exports the helpers whose modules load in any document. widget-api.js re-exports it with export *, so each name has one home and other widgets are unaffected.

  • widget-upgrade.js no longer imports layer-client, shadow-stage or Lit. It holds once, bodyText, dataBody, failSoft, reportFailure and placesFailure.
  • Two new homes: shown-box.js holds measure, skipped and elementBox, which geometry.js's shownBox now builds on. base-layer.js holds inBaseLayer.
  • Importing each home by its own /runtime/<module>.js path was rejected. The public boundary would become a list of internal files that grows with every widget, and moving a home would break vendored widgets.
  • portable-api.js is now named beside widget-api.js in root AGENTS.md, "Pages across Leaf updates", as part of the public package API a protocol bump guards.

Leaf reads the widget instead of the widget calling Leaf.

  • Settling. The widget dispatches pending-task (PendingTaskEvent, the Web Components Community Group's draft protocol: bubbling, composed, carrying a complete promise). A document listener in widget-controller.js holds the nearest widget's presentation on that promise, as widgetController(owner).present() does. updateComplete was rejected because a plain element starts drawing when it first gets a box, such as a shut panel opening, which only the element knows.
  • Failures. failSoft shows the error box and dispatches a bubbling, composed ErrorEvent("error") on the element. A failure on a detached element goes to window, carrying the element. layer-client.js's window error listener reports it as before, named by failureOf: the element the failure carries, else composedPath()[0]. A reason that isn't an Error is wrapped so the event always carries one, and the report text is unchanged.
  • Failure box. It no longer goes through Lit's render, since Leaf's Lit build imports dom-move.js → focus.js, which don't load off Leaf. Where the element holds a Lit part, the box becomes that part's value through Lit's internal setter (_$AI), so the widget's next template render still replaces it. A Lit upgrade that renames the setter fails dom-children.test.mjs.
  • Placers. placesFailure keeps one placer per element again. A shadow stage registers through its own slot, asked after the owner's.

Kernel. The portable modules compile into one chunk, bundle-portable.js, through a codeSplitting group read from portable-api.js. A Leaf page boots with 18 kernel files instead of 16, at the same bytes. The portable chunk has to be in the kernel because Leaf pages and portable widgets must share one instance of these modules. PUBLIC_MODULES gains the facade, and the layer's private-module refusal subtracts it.

Behavior changes, no protocol bump. Every name widget-api.js exported is still exported, plus PendingTaskEvent. A failure in a widget nested inside a shadow stage is now named for that widget, not its host. A dispatcher of pending-task with no widget descriptor, which threw before, now hands its promise to the nearest ancestor widget, or to nobody.

Known gap. lf-chart reads data-lf-height, which only Leaf's delivery paints, so a plain host has to write it or the chart is 1px tall. Drawing to the chart's own box fixes that, and comes with the next widget.

Tests.

  • New tests/smoke/test_plain_page.py runs at every landing. It serves a page with only layer_import_map and the runtime and layer Leaf serves, and asserts the chart is defined, draws an svg, a refused chart shows its box and fires one error event, and nothing reaches the console. Adding a widget is one Portable(...) entry. With lf-chart importing widget-api.js again, it fails at control-selectors.js:14.
  • New tests/browser/test_public_api.py::test_a_widget_s_pending_task_holds_the_page_until_its_drawing_is_in. It fails when the widget-controller.js listener is removed.
  • New runtime tests for a detached failure's report, one placer per element, and a non-Error reason. The template-recovery assertions in dom-children.test.mjs are restored.
  • Also run: pnpm run test:runtime (687), pnpm run check:browser, and 25 focused browser tests: the chart tests, failure naming and refusal, export, readiness, reading place, diff recovery, smoke, private-import refusal, owner index, and a thread-card test for the moved measure import.
  • An independent review found no regression on Leaf pages for any bundled widget. Its five smaller findings are fixed above.

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

…ce (#3833)

A display widget can now load and draw on a page that runs none of Leaf's runtime:
the host maps `/runtime/` and `/vendor/` with its own import map, as Leaf's
`layer_import_map` does. lf-chart is the first: its import graph is 11 runtime
modules (68 KB of source) where it was 179 (2.2 MB), none of which reads what
Leaf's delivery or prepaint puts in a page.

The portable surface is `runtime/portable-api.js`, a facade over the homes any
document can load; `widget-api.js` re-exports it whole, so other widgets are
unaffected and each helper keeps one implementation and one public name set.
A facade rather than imports of each home by path, because the homes stay free to
move and the boundary is one module that eslint, the layer's private-module
refusal, `PUBLIC_MODULES` and the kernel's entries name.

Homes made portable:
- `widget-upgrade.js` (once, bodyText, failSoft, reportFailure, placesFailure)
  no longer imports layer-client, shadow-stage or Lit.
- `measure`, `skipped` and the element part of `shownBox` move to `shown-box.js`;
  geometry's `shownBox` builds on it. Off Leaf, where no prepaint ran, `skipped`
  answers false and readings ask the browser directly.
- `inBaseLayer` moves to `base-layer.js` from `stylesheets.js`, which reads the
  delivered sheets at import.

Leaf now reads a widget from outside rather than the widget calling in:
- Settling: a widget that does not extend LeafElement dispatches the Web Components
  Community Group's `pending-task` event (`PendingTaskEvent`) on itself; Leaf's
  document listener passes its promise to the nearest widget's controller
  `present`, exactly where lf-chart's `widgetController(this).present` went.
  `updateComplete` stays LeafElement's: Leaf reads it only on updates it drives, and
  a plain element starts drawing when it gets a box, which only it knows.
- Failures: `failSoft` and `reportFailure` dispatch a bubbling, composed `error`
  ErrorEvent on the element; Leaf's window error listener reports it as the page's
  error naming the widget (`failureContext`). Off Leaf the box shows and nothing is
  logged.

Behavior changes:
- A failure of an element that is not in a document when it fails is no longer
  reported to the agent, since its event reaches no listener.
- A Lit template drawn into an element after failSoft replaced its children no
  longer replaces the box; it draws after it. No shipped widget draws Lit into its
  failed host: LeafElement places its box itself. Directives are still released.
  The runtime test for template recovery is narrowed to directive release and native
  recovery.
- A shadow stage now places its host's failure box through a placer it registers,
  and `placesFailure` keeps every owner's placer in order rather than the last.
- `widget-api.js` also exports `PendingTaskEvent`.

The kernel gains `runtime/portable-api.js` as an entry. Its homes compile into one
chunk (`bundle-portable.js`); a Leaf page's boot loads 18 kernel files where it
loaded 16, at the same bytes, and a page with a chart 21 where it loaded 18.

`tests/smoke/test_plain_page.py` serves a plain page with the runtime and layer Leaf
serves (kernel where current) and checks each portable widget is defined and draws
for its room, and that a refused chart shows its box and an `error` event with no
console error; adding a widget is one entry in `PORTABLE`. It fails with lf-chart
importing widget-api.js.
# Conflicts:
#	browser/runtime/widget-elements.js
#	dist/kernel/kernel.json
#	dist/kernel/leaf.js
#	dist/kernel/runtime/bundle-annotation-overlay.js
#	dist/kernel/runtime/bundle-data.js
#	dist/kernel/runtime/bundle-disclosure.js
#	dist/kernel/runtime/bundle-geometry.js
#	dist/kernel/runtime/bundle-layer-client.js
#	dist/kernel/runtime/check-api.js
#	dist/kernel/runtime/geometry.js
#	skills/leaf/references/module-authoring.md
…3833)

Fixes from review of the portable helper surface. Two behavior changes the last
commit made are taken back, and failure reports are more exact.

- A template rendered into an element after failSoft replaces the error box again,
  as before the portable move. The box becomes the value of the Lit part the element
  holds, set through the part as lit/directive-helpers.js's `setChildPartValue` does
  (`_$AI`, the setter name every production build of Lit 3 shares; `_$setValue` in a
  development build), so no import of Lit is needed and the module still loads off
  Leaf. Setting it disconnects the template's directives, and a part whose marker a
  native draw removed gets the marker back. The runtime test's template-recovery
  assertions are restored.
- A failure of an element out of the document, such as a cached thread-message body
  whose Plot load fails while its thread is shut, is reported again: reportFailure
  dispatches on the window when the element isn't connected, carrying the element and
  the reason as thrown. Off Leaf it reaches only the host's own window listeners, as a
  connected failure does.
- Leaf names an error event by the element it began at (`failureOf`), not the shadow
  host it reached the window by.
- Every failure event carries an Error, so the render check no longer reads one with
  no reason as an exceptionless window error. A reason thrown as something else is
  wrapped without a stack, and Leaf reports the reason as thrown, so its text is
  unchanged.
- `placesFailure` is one placer per element again, a re-registration replacing it, so
  an owner may register on every connection. A shadow stage registers through its own
  slot (`stagesFailure`), asked after the owner's placer.

The merge of main before this commit points thread-card.js's `measure` import at
shown-box.js and drops widget-elements.js's re-export, since the transcript change
it waited for has landed.

Tests: runtime tests for a detached failure's report, an Error on every failure event,
and a re-registered placer, each failing on the old behavior; a browser test that a
Leaf page holds the widget's presentation on a `pending-task` promise, which fails with
the listener removed; the plain-page test now asserts an empty console.
# Conflicts:
#	dist/kernel/kernel.json
#	dist/kernel/runtime/bundle-page-inspection.js
# Conflicts:
#	dist/kernel/kernel.json
#	dist/kernel/leaf.js
#	dist/kernel/runtime/bundle-data.js
#	dist/kernel/runtime/bundle-geometry.js
#	dist/kernel/runtime/geometry.js

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new plain-page smoke test failed locally on a 5 px layout shift during chart initialization. Containing the source margin as suggested inline made the same test pass.

Comment thread tests/smoke/test_plain_page.py Resolved
@max-sixty
max-sixty merged commit 6c6ce9b into main Oct 28, 2026
7 checks passed
@max-sixty
max-sixty deleted the agent-aeab1289051cc5546 branch October 28, 2026 03:10
max-sixty added a commit that referenced this pull request Oct 28, 2026
…a Leaf reads (#4574)

`lf-code` now loads and draws on a plain HTML page that maps `/runtime/`
and
`/vendor/` with its own import map, as `lf-chart` does since #4542. On a
Leaf
page it behaves as before. Its import graph is 14 runtime modules and 80
KB of
source, down from 182 and 2,271 KB. `lf-chart` draws to its own box's
height,
so a plain host no longer has to write `data-lf-height`.

Off Leaf a widget binds no keys, shows no Copy control and writes no
quiet
word. Leaf adds each of those by reading what the widget declares on its
elements, rather than the widget calling into Leaf.

**Keys are data on the element.** A widget sets an element's
`lfCommands` to the
scope (`{title, rows, when, ready, escape, claims}`, the rows unchanged,
or a
`commandScope()` value) and calls `commandsChanged(element)`, a portable
helper
that dispatches a bubbling, composed `lf-commands` event. Leaf's
register
(`keyboard/scopes.js`) reads the declaration from a window listener: a
new one
stands in place of the last, `null` withdraws it, and the same one
standing
means something its rows read changed, so the register repaints, as
`paintKeys()` did. Leaf stays the only writer of `aria-keyshortcuts`,
and a
plain page, which hears nothing, advertises no key.
- One public route. `commands` and `paintKeys` leave `widget-api.js`,
and
every caller moves to the declaration: all 16 package widgets,
`lf-suggestion`,
the feature gallery's page script, the docs and the tests.
`commandScope` stays,
as the value form a margin entry and several elements share, and so does
the
template directive `commandsOn`. The internal `keys` remains core's own
entry
to the register, which the declaration's reader calls.
- A malformed declaration still throws where the widget made it. A
listener's
exception never reaches the dispatcher, so the reader hands the refusal
back on
the event and `commandsChanged` throws it.
- An element out of the document dispatches on the window, carrying
itself, as
`reportFailure` does, so a control declared before it is placed keeps
its keys.
- Rejected: reading `lfCommands` at upgrade or connection without a
signal. It
would need a second way in, an arrival watch over every widget tag plus
a sweep
after each module import, and would still miss elements a render creates
and
declarations made after asynchronous drawing, such as lf-code's. Also
rejected:
keeping `commands` public as a portable writer of the declaration, which
leaves
two public routes to one capability.

**Copy.** A widget block declares its Copy control with `data-lf-copy`,
naming
what it holds, and an `lfCopy()` method returning the text to copy.
Leaf's code
block watch (`code-copy.js`) stands the control right after the block
while
both are in the page. `copyBlock` stays for a widget that seats the
control
among its own, as `lf-draft` does in its toolbar; declaring that
placement would
add an option for one caller.

**Quiet words.** A module declares a part's quiet word with
`data-lf-quiet`, and
Leaf writes it there (`watchQuietWords`, `declared-words.js`). Off Leaf
no
stylesheet would clip the word, so it would show in the code line.
`quietWord`
leaves `widget-api.js`; `lf-suggestion` declares its slots' words the
same way,
so `data-lf-quiet` joins `PAGE_PAINT_ATTRIBUTE`, the paint an authored
element's reading looks past.

**Portable homes.**
- `tab-store.js` holds `tabStore`: Leaf's tab store where the prepaint
opened
one, and off Leaf a store that keeps nothing (reads answer null, writes
are
refused). Off Leaf the wrap can't be toggled anyway, since the key is
Leaf's.
- `syntax-tokens.js` holds `syntax`, `synNodes`, `tokenLines` and
`ensureSyntaxLanguage`. `syntax.js` keeps the page's code blocks and
`langForPath`, which read Leaf's registry. A tokenizer failure reaches
Leaf
through `failSoft`'s error event.
- `layoutChanged` and `LAYOUT` move to `widget-upgrade.js`.
- lf-code announces its drawing with `PendingTaskEvent` instead of
`widgetController(this).present`.

**lf-chart height.** It reads `clientHeight` instead of
`data-lf-height`, once,
at its first drawing, while the theme (on a Leaf page) or the host (off
Leaf)
still holds the box, and keeps that height through redraws (`#height`).
Reading
it on every redraw would let a chart that a grid or flex row stretches,
such as
a short chart beside a taller one in `.layout-tiles`, grow to the row's
height
after a width change. The legend subtraction is unchanged.

**No protocol bump.** Since #4551 each page's layer is composed by the
Leaf
that reads it, so a change to the package API leaves the protocol at 36.

**Tests.**
- `tests/smoke/test_plain_page.py` adds lf-code, drawing coloured lines
with the
highlighted one among them, and asserts the page holds no
`aria-keyshortcuts`,
Copy control or quiet word. The chart's drawing now fills its host's
height,
with no `data-lf-height`. Each assertion failed when undone: lf-code
importing
`widget-api.js` (fails at load), lf-code writing its own quiet word,
lf-code
setting `aria-keyshortcuts`, and lf-chart reading `data-lf-height`
again.
- `test_code_wrapping_keeps_source_copy_and_reader_choice` asserts that
on a
Leaf page lf-code advertises `w`, its Copy control stands after its
block and
its highlighted line says "highlighted". Each failed with its
declaration
withheld.
- A runtime test covers a declaration made out of the document, its
withdrawal
and its refusal; it failed with the window dispatch or the withdrawal
removed.
- `test_a_chart_keeps_its_stated_height_beside_a_taller_one` puts charts
of
data-height 200 and 420 in one tiles row and resizes it 640 then 1280
wide;
reading the height on each redraw, the short chart ends at 420.
- `module-input.md` lists every declaration field and says that a new
`lfCommands` object replaces the scope and is checked, while
`commandsChanged`
on the same object only repaints. `module-authoring.md` states the
limit: Leaf
finds `data-lf-copy` and `data-lf-quiet` in the page and its own shadow
stages,
not in a shadow root a page script attaches. The
`widget-list-keeps-focus` eval
rubric asks for the declaration.
- Also run: `pnpm run test:runtime` (694 after merging main), `pnpm run
check:browser`,
`pnpm run test:browser`, and 66 focused browser tests: every converted
register
test, a keyboard test for each converted widget, the chart tests, code
copy,
export quiet words, composition and the pending-task test.

> _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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants