Skip to content

Draw lf-code as an Effect UI component in a shadow root - #7484

Draft
max-sixty wants to merge 6 commits into
mainfrom
effect-ui-lf-code
Draft

max-sixty wants to merge 6 commits into
mainfrom
effect-ui-lf-code

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Requested by Maximilian · project thread

This ports lf-code from a LeafElement to an Effect UI component (defineWidget, shadow: true, slotAssignment: "manual"), following browser/AGENTS.md, "Renderers". It is slower than the Lit block on a large page: about 17% on load and 38% on resolving a comment at 256 blocks (numbers below). By the rule, that means it shouldn't land as it stands. It is open so the maintainer can decide.

It also grows the package interface. effect-widget.js is a public module, and this PR adds to it, which is the maintainer's call (root AGENTS.md, "Growing the package interface"):

  • a slotAssignment option on defineWidget ("manual" lets a Mount assign each authored child to a slot of its own);
  • lfElementsFor among the keys declare accepts.

What changed

  • Drawing. The numbered lines, elided rows, notes and toolbar are drawn in the element's shadow root. code.css styles them through shadow parts: frame, source, row/line/elided (with hi, at, folded, wrap/scroll), note, caption and bar.
  • Notes. Each lf-note stays where the author wrote it and shows under its line through a slot of its own, which the Assign mount fills with slot.assign. The widget no longer moves notes or writes styles on them.
  • Shiki runs in the Color mount. It holds Leaf's presentation (pending-task) until the coloured lines are drawn.
  • Model and messages. The Model holds the reader's wrap and notes choices. The w and n keys send messages, and each message's KeepInTab Command records the choice.
  • What the port made easier. The rows a block draws (excerpt numbering, elided rows, their captions, notes at the foot) come from one pure function, rowsOf. That function and the key Commands are covered by a Story test (tests/runtime/lf-code-story.test.mjs); before, only browser tests reached them.
  • Why a shadow root, not a light-DOM part. A note reads between two drawn lines. The light part stands after the authored children, so every note would come before every line in document order, which is the order that selection, copy and a passage's quote read. CSS order moves only the paint. Moving the note into the part would take it from where inputs and revisions look for it. The header says this.
  • verbatim. The registry doesn't need it for lf-note. lf-note has no module and isn't upgraded, and lf-code was already opaque. lf-option needed it only because it became an upgraded element.

Shared modules

  • effect-widget.ts: the two additions above, and a failed root with manual slots assigns all of its children to the fallback slot.
  • syntax-tokens.ts: synAttributes, which gives a token's colour as attributes.
  • stage-integration.ts: every stage now shares one adopted sheet of the layer's shadow rules, first among its adopted sheets. This replaces a <style> per stage, which cost about 0.5 s of profiled load on a page of 256 blocks. It applies to every shadow widget, lf-options included. The integration therefore retains no node, so installStageIntegration takes a function that returns nothing, and shadowStage (dom/shadow-stage.ts) no longer keeps style nodes beside the rendering.
  • bench.py: a code collection, a page of n lf-code blocks, each with a highlighted line and two notes.

Bench

Command: leaf-dev bench code --rounds 3 --base origin/main (Chromium). Each cell is the lowest of 3 rounds, in ms of main-thread CPU unless the row says otherwise. Each cell reads base → head. Other suites were running on the host at the same time.

reading 32 64 128 256
load cpu 957 → 994 1306 → 1421 1878 → 2144 3116 → 3659
resolve cpu 93 → 111 104 → 147 121 → 208 226 → 312
resolve shown ms 134 → 149 136 → 168 127 → 209 182 → 303
walk cpu 39 → 40 53 → 52 79 → 84 133 → 145
draw cpu 81 → 81 125 → 117 196 → 189 320 → 372
key / scroll / hover / aim cpu flat flat flat flat

Load grows at about the same exponent in both arms (1.04 base, 1.06 head), so the cost per block is higher rather than the growth worse. Before the shared sheet, load at 256 was 3817 → 4537 in a noisier run. The remaining cost, read from CPU profiles at 256:

  • Forced layout. nearWindow (Copy's anchorControl) and layoutWindow (the margin's windowHoldsRail) are the largest items.
  • Effect UI. effect-ui.js itself adds about 0.3 s.
  • Resolve. frame-pass.js pass forces layout for about 260 ms, against 60 ms on base.

I didn't find the cause of the higher forced-layout cost. Batching each stage's first arrivals into one microtask cut the 256 calls to Copy's prepareDeclaredBlocks to one, but load didn't move, so I reverted it.

Visible and behavioural differences

  • Page CSS. A page's CSS reaches the drawn lines only through ::part, for example #id::part(line). There is no lf-rendered class; :defined takes its place.
  • Wrapping. The w press reads whether lines wrap from the drawn line's computed style. A page's later CSS therefore counts as the default the first press turns the other way.
  • Copy always stands in the block's toolbar (lfBar), even with no notes. Copy beside the block hangs from an anchor named in the document, which a control inside the shadow tree doesn't find.
  • Selection. getSelection().toString() leaves out the slotted notes. A real copy (Ctrl+C) includes them, and the excerpt test now reads the clipboard.
  • Captions. The elided row's caption dash comes from a flex layout.
  • Plain pages. The plain-page frame is restated in ::part(source) rather than the reading face.
  • Height. The pre's ::after scroll clearance is gone, so a block with a note at its foot ends about 13 px sooner. This shows in walkthrough-code, walkthrough-copy-* and code-reader-feedback from leaf-dev stills --base. native-code, annotated-code and native-code-copy-hover show 0 px of change, only computed-style differences on the host.

Tests

  • Selectors move to the shadow tree in tests/browser/widgets/test_code_and_documents.py and tests/journeys/test_render_code_reading.py, and in the stills states in dev/leaf_dev/states.py.
  • The smoke test's MOVING probe now walks open shadow trees and skips authored children that a shadow tree shows nowhere. Those have no computed style to read.
  • Two failures are inherited: both also fail on the merge base (a894fd6), run locally by node id.
    • tests/journeys/widgets/test_code_and_documents.py::test_a_long_text_document_draws_the_lines_in_view_and_keeps_the_rest_whole
    • tests/browser/widgets/test_diffs.py::test_print_wraps_diff_lines_and_keeps_file_headers_in_place

🤖 Generated with Claude Code

https://claude.ai/code/session_01V22NWd3erndyoj2G9LEP7y

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


Generated by Claude Code

…r owned by a Mount

lf-code's numbered lines, elided rows, notes and toolbar are now an Effect UI
component (`defineWidget`, `shadow: true`, `slotAssignment: "manual"`), styled by
shadow parts from code.css. Each `lf-note` stays where the author wrote it and shows
under its line through a slot of its own, so the widget no longer moves the author's
notes or writes styles on them. Shiki runs in the `Color` mount.

What the port made easier: the rows a block draws (excerpt numbering, elided rows,
their captions, notes at the foot) are one pure function, `rowsOf`, and the wrap and
notes keys are messages whose `KeepInTab` Commands a Story test asserts
(tests/runtime/lf-code-story.test.mjs); before, each was reachable only through a
browser test. The reader's wrap and notes choices are the Model, so the hand-written
repaint of the notes toggle and its attribute are gone.

Why a shadow root rather than a light-DOM part: a note reads between two drawn lines,
and the light part stands after the authored children, so the note would come before
every line in document order, the order selection, copy and a passage's quote read.
Moving the note into the part takes it from where `inputs` and revisions find it.

Shared modules:
- effect-widget.ts: `slotAssignment` option, and `lfElementsFor` among the
  declarations.
- syntax-tokens.ts: `synAttributes`, a token's colour as attributes.
- stage-integration.ts: the layer's shadow rules are one adopted sheet every stage
  shares, in place of a `<style>` per stage, which cost about half a second of a
  256-block page's profiled load.
- bench.py: a `code` collection, a page of n lf-code blocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V22NWd3erndyoj2G9LEP7y

@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.

Two points to settle alongside the performance question the description already raises.

Package interface. effect-widget.js is a public module ($public-modules). This PR adds two things to it: a slotAssignment option on defineWidget, and lfElementsFor among the keys declare accepts. Root AGENTS.md ("The maintainer decides", "Growing the package interface") makes each of these the maintainer's call. leaf-dev interface --base doesn't list either one, because it lists names, not options. So the description should name both as additions for the maintainer to approve, next to the perf trade-off, not under "Shared modules" as implementation detail.

Dead stage-integration contract. The stage integration now returns [] in every case. shadowStage in dom/shadow-stage.ts still threads that return value through setChildren(root, [...styles, ...nodes]). Its header still says "The host returns its retained style nodes so imperative staging keeps them alongside the rendering". No integration retains a node anymore, so the Node[] return type, the styles spread and that header sentence can go, and installStageIntegration can take (root: ShadowRoot) => void.

Comment thread browser/runtime/stage-integration.ts Resolved
…urns now

The shadow rules are an adopted sheet, so the integration retains no node in a
stage and shadowStage has none to keep beside the rendering. Also hold that a
block without notes stands Copy alone on its toolbar.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V22NWd3erndyoj2G9LEP7y
# Conflicts:
#	dev/leaf_dev/commands/bench.py
max-sixty added a commit that referenced this pull request Nov 28, 2026
…7542)

`defineWidget` stops guessing. It no longer infers from DOM mutations
when a component has drawn, or which node in a new parent is the row the
user was on. Effect UI already keeps focus, typed words and the caret on
every node a draw keeps. Where a draw Leaf starts replaces the user's
row, the adapter's hold (`holdFocus`, keyed by `data-lf-datum` as on
main) puts the user on the row now carrying that key, or its nearest
neighbour, on the row itself rather than on a control picked by its
look. Where nothing it knows stands, as when a removal empties the list,
the user lands on the widget's element. The adapter's hold restores only
while focus is lost and tries where the user last stood first, so a
nested widget and its parent put the user in the same place; other holds
are as on main.

Also in this change:

- `declare` sets every member Leaf reads off a widget's element
(`DECLARED` in `effect-widget.ts`).
- `lf-call-diff` and `lf-toc` leave a fold's `open` to the browser, so
it stays where the user or a reveal left it.
- Root `AGENTS.md` names the one case a widget keeps itself: a row it
moves to another parent, written as a keep-alive child.

`effect-widget.ts`'s header lists the limits, among them a parent's draw
hiding a node that a nested widget's draw then restores, which lands the
user on the parent, one Tab away.

Max picked this rebuild on the Effect UI review page, with the
`AGENTS.md` words; Codex reviewed it over seven rounds. `pnpm run
test:runtime` (1240) and 13 focused browser tests pass. #7484
(`lf-code`'s port) adapts to `lfElementsFor` in `DECLARED` if it lands
second.

> _This was written by Claude Code on behalf of max-sixty_

This branch has not been deployed

No deployments
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.

3 participants