Skip to content

Port lf-toc to an Effect UI component, in TypeScript - #7457

Merged
max-sixty merged 5 commits into
mainfrom
effect-ui-lf-toc
Nov 28, 2026
Merged

max-sixty merged 5 commits into
mainfrom
effect-ui-lf-toc

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Requested by Maximilian · project thread

Ports lf-toc from LeafElement and Lit to an Effect UI component, written as lf-toc.ts and compiled to the served lf-toc.js. toc-view.js, the Lit view, is deleted; toc-marks.js stays as it was.

inputs reads the outline (headings, destinations, lent ids), each row's marks from the threads, On Me and comparison readings, and the open attribute. The Model holds the reader's disclosure choice, whether the form stands the outline open, the row the reader is in, and the rows the map leaves off. Three Mounts do the post-commit work:

  • FollowReading (on the nav) tracks the reading position with an IntersectionObserver on the headings crossing the window's top half, plus a scroll-end check. It replaces measuring every destination against the landing band on every scroll. The observer's root is the page's own document (or nested scroller), so a framed page reads its own window, not the host's.
  • FitMap (on the rows) measures the reading map when the document, its destinations or the track change size, or on lf-layout, toggle and lf-presentation. It writes each row's place and the lens's scroll-driven keyframes, and reports the rows it leaves off the map. The fitting algorithm and the lens are unchanged.
  • FollowPosture (on the summary) uses a ResizeObserver to detect when the summary's box appears or disappears. That tells it whether the form stands the outline open. The disclosure's open is now controlled by the Model, which follows toggle. When the form hides the summary, the Mount reads the reader's choice from the disclosure itself, because the browser queues toggle as a task it may fold into the form's opening.

The registry schema (tag, attributes) is unchanged.

What the port made easier

This follows the port rule in browser/AGENTS.md, "Renderers":

  • The disclosure's rules are now one update covered by Story tests (lf-toc-story.test.mjs). The rules are the reader's choice against the forms that stand the outline open, and the focused link that keeps it open on return. Before, they were split between the Lit view, the element's fields and a measure.
  • A row keeps its nodes, and focus on its link, by key. That retires holdFocus, which restored focus after a redraw.
  • The reading position is an IntersectionObserver in a Mount, rather than a measure of every destination on each scroll.
  • The module is typed. lf-toc.ts is 1121 lines, against 1024 for lf-toc.js and toc-view.js together.

Bench

No generated page held lf-toc, so this adds contents to COLLECTIONS: prose_sections(n) beside a sidebar track holding the outline. The figures come from uv run leaf-dev bench contents --base (Chromium, base 0ca1326 against head 4f68bd4) on a shared host at load 7 to 16. Scroll, key and resolve were read over 3 rounds, the rest over 1. These rows are the lowest reading:

reading 64 base 64 head 256 base 256 head
load cpu ms 750 741 1055 972
scroll (wheel step) cpu ms 8.0 9.3 8.8 9.2
key cpu ms 12.5 14.0 12.9 12.3
resolve cpu ms 118 115 110 139
resolve shown ms 152 184 186 193
  • Growth. No reading gains a growth mark on head that base lacks. Scroll and key stay level with page size. Load's fitted exponent is 0.79 on head against 0.76 on base.
  • Budgets. Key sits at the 12 ms budget, and resolve's shown time is over 100 ms, at 256 on both arms alike.
  • Resolve at 256. A profiled rerun gave 152 ms on base and 160 ms on head. Head's resolve profile has no lf-toc function among its growing costs. Load's largest growing costs are the map's measuring (sizeOf) on both arms.

Visible and behavioral differences

leaf-dev stills --base origin/main on contents-map-keyboard, contents-marks, contents-marks-changes and topic-contents shows no visible difference. The only style difference is the adapter's display: contents box around the nav. The differences are in behavior:

  • Current section threshold. The current section is judged against the middle of the scroller, not the middle of the landing band (the band excludes the top chrome and the panel's row). A section counts from its heading's top, not its destination's (for example, an eyebrow's). The two lines differ by about half the top and bottom insets.
  • All destinations hidden. While every destination is hidden, the start row stays aria-current instead of none. The outline is hidden then too, so nothing is perceivable. The hidden-destinations test no longer asserts "no aria-current while hidden". It still asserts the first revealed frame: no row off the map, start current.
  • Row identity. A row keeps its nodes, and focus on its link, through its heading's authored id, or failing that its destination's. The heading keeps that key for as long as it stands. The old code restored focus after the fact (holdFocus), also matching by a lent id. Now a heading replaced by a new element, where neither it nor its destination has an authored id, gets a new row, and focus on its link is lost.
  • Marks on folded rows. Marks for a section left off the map are folded into the row above it. A change that spans both sections now counts twice in that row's preview, where it counted once.
  • Map attributes. A label or marker off the map now carries data-lf-toc-off, written by the Mount. Before, the view drew data-lf-toc-shown and the widget removed it. toc.css and the tests follow. The empty outline is hidden via lf-toc:has(.lf-toc-nav[data-lf-toc-empty]), since the view can't set an attribute on the host.

Checks

  • The type check (browser/types), the TypeScript build, pre-commit and the build tests of pnpm run check
  • node --test: toc-marks, authored-capture and the new lf-toc-story
  • The lf-toc browser tests in test_contents_and_glosses.py (now with a framed page whose current section is read against its own window; it fails on the old root) and test_render_toc_disclosure.py, plus the lf-toc tests in go-to, aim, native layout, dial, initial delivery and render pages. All pass locally in Chromium and Firefox. WebKit isn't installed on this host.

🤖 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

lf-toc is now an Effect UI component (lf-toc.ts, compiled to lf-toc.js) and
toc-view.js, its Lit view, is gone. `inputs` reads the outline, each row's marks
and the `open` attribute; the Model holds the reader's disclosure choice, whether
the form stands the outline open, the row the reader is in and the rows the map
leaves off. Three Mounts do the post-commit work:

- FollowReading hears the headings cross the window's top half with an
  IntersectionObserver, and the scroller reach its end, in place of measuring
  every destination against the landing band on each scroll.
- FitMap measures the reading map as the document, its destinations or the track
  change size, writes each row's place and the lens's scroll-driven keyframes, and
  reports the rows it leaves off the map.
- FollowPosture hears the summary's box come and go, which says whether the form
  stands the outline open, in place of reading it at each measure.

Behaviour that changed:

- The window's half the current section is read against is the scroller's, not
  the landing band clear of the top chrome and the panel's row, and a section
  crosses it at its heading rather than its destination's top.
- While every destination is hidden the start row stays current, where none was;
  the outline is hidden then, so the reader sees no difference.
  test_contents_with_every_destination_hidden_returns_when_the_disclosure_opens
  no longer asserts no aria-current while hidden.
- A row keeps its nodes, and focus on its link, by its heading's authored id or
  else its destination's, which the heading keeps while it stands. A heading
  replaced in a place with neither authored id takes a new row.
- A change that spans a section the map leaves off and the row above it counts
  twice in that row's preview.
- The map's labels off the map wear `data-lf-toc-off`, which the Mount writes,
  in place of `data-lf-toc-shown`, which the view drew and the widget took off.

The Lit drawing's node test in authored-capture.test.mjs goes, the browser suite
covering what it held; toc-marks.test.mjs reads the outline with `readOutline`,
and lf-toc-story.test.mjs covers the disclosure's update.

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.

In a framed page, FollowReading measures the top half against the wrong window. With root: null, the IntersectionObserver's implicit root is the top-level browsing context's viewport, not the document the outline covers. So a Leaf page inside an lf-sample frame, such as a window sample, which scrolls inside itself, reads "the window's top half" as the parent's. If the frame sits in the parent's lower half, no heading ever intersects, and the current row falls back to the "above" rule whatever the reader is looking at. On top of that, entry.rootBounds is in the parent's coordinates while boundingClientRect is in the frame's, so above and top compare two different coordinate spaces. The code this replaces measured against the frame's own scroller band, so this is a regression for framed pages. margin-projection.js (watchNear) already passes root: document for the same reason ("rather than the top window a framed page stands in"). The inline suggestion makes that change. The served lf-toc.js needs the same one-line edit, or a rebuild.

The rest holds up. No package-interface name is added (leaf-dev interface --base shows none), and holdFocus, frameFor, the Lit view and the per-scroll measurement go away. check:browser reports the compiled module in sync, and pnpm run test:runtime passes (1225 tests).

Comment thread packages/default/widgets/lf-toc.ts Resolved
…oice as the form opens the outline

FollowReading's IntersectionObserver took the top window's viewport as its implicit
root, so a page framed low in its host, as a window sample stands, read "the window's
top half" as the host's, and compared heading boxes in the frame's coordinates with
the host's bounds. Its root is now this document, as margin-projection's watchNear's
is. test_a_framed_page_s_current_section_is_read_against_its_own_window fails on the
old root.

FollowPosture now reports the disclosure's own open state as the form hides the
summary (Exposed{open}), and focus as it returns (Returned{keep}). A press's toggle is
a queued task the browser folds into the next toggle, so the form's opening could
overtake it and the reader's close was never heard, leaving the outline open on
return to flow; test_contents_disclosure_keeps_its_links_focus_and_open_state failed
there twice locally under load. A story covers the overtaken press.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V22NWd3erndyoj2G9LEP7y
`leaf-dev bench contents` is `prose_sections(n)` in a sidebar whose track holds an
lf-toc, so its rows, markers, fitting and reading position grow with the page;
`page` takes the track a sidebar holds. No other generated page held lf-toc.

What the port made easier (browser/AGENTS.md, "Renderers"): the disclosure's rules,
the reader's choice against the forms that stand the outline open and the focused
link that keeps it open on return, are now one `update` under Story tests
(lf-toc-story.test.mjs), where they were split between the Lit view, the element's
fields and a measure. A row keeps its nodes and focus by key, which retires
holdFocus's restoring focus after a redraw. The reading position is an
IntersectionObserver in a Mount rather than a measure of every destination on each
scroll, and the module is typed. lf-toc.ts is 1121 lines against 1024 for lf-toc.js
and toc-view.js, now with types.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V22NWd3erndyoj2G9LEP7y
@max-sixty
max-sixty merged commit 66b2192 into main Nov 28, 2026
7 checks passed
@max-sixty
max-sixty deleted the effect-ui-lf-toc branch November 28, 2026 01:00
leaf-agent added a commit that referenced this pull request Nov 28, 2026
…o page link (#7505)

Focus entering `main` and Go-to arming each restyled every link on the
page again, so
`test_entering_the_column_or_go_to_restyles_no_cell_or_link_it_passes`
has failed on every push to `main` since #7457 (`{'20 rows':
{'entering': 434, 'Go-to': 549}, '120 rows': {'entering': 534, 'Go-to':
649}}`: exactly one more restyle per row's link).

The cause is the selector, not the port's `display: contents` wrappers.
#7457 replaced `a[data-lf-toc-shown]` with `a:not([data-lf-toc-off])` in
the two `toc.css` rules that let the map's labels take the pointer (one
under `lf-toc:is(:hover, [data-lf-focus-within])`, one in the
`body[data-lf-go-to-active]` scope). A `:not()` gives Chromium nothing
to narrow its invalidation by, so the subject came down to the type `a`.
When `main` gains `data-lf-focus-within` or `body` gains
`data-lf-go-to-active`, Chromium restyles every `a` beneath it, whether
or not the rule matches there. `browser/AGENTS.md`, "What a style rule
costs", names this case and asks for a class only the targets carry. The
subjects now read `.lf-toc-link:not([data-lf-toc-off])`. That class is
on every label link the view draws (`lf-toc.ts`'s `link`) and on nothing
else in the labels layer, so the rules match the same links and nothing
on screen changes.

Verification on the Tend runner:
- The restyle test fails on `main` with the counts above and passes with
this change.
- The `lf-toc` label and reveal tests pass:
`test_a_route_too_long_for_its_labels_leaves_its_deepest_level_off_the_map`,
`test_the_outline_beside_the_body_marks_its_introduction_on_its_label`,
and both directions of
`test_a_contents_spine_rests_beside_the_prose_and_reveals_its_labels_over_it`.

No other rule under `browser/` or `packages/` reads either mark before a
type-only subject.

Closes #7499 — automated triage

---------

Co-authored-by: leaf-agent <[email protected]>
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