Skip to content

fix(ui): srcdoc frame height with collapsed margins + fenceTheme host seam - #1677

Merged
backnotprop merged 2 commits into
mainfrom
fix/ui-frame-height-fence-theme-seam
Oct 4, 2026
Merged

backnotprop merged 2 commits into
mainfrom
fix/ui-frame-height-fence-theme-seam

Conversation

@backnotprop

Copy link
Copy Markdown
Owner

Two small @plannotator/ui changes requested by a host embedding the package. No release or version bump.

1. HtmlViewer frame height (bug)

Root cause (confirmed in Chromium): the bridge reported document.body.scrollHeight. When body has no top padding or border, a first child's top margin collapses through body. That moves body's box down without making it taller, so the frame was cut short by the margin, and the same happens at the bottom. Example: body{margin:0} with a 560px card that has margin-top:40px reported 560 instead of 600. Ordinary pages also lost their collapsed h1/last-p margins (1062 reported vs 1100 true).

Fix: measureContentHeight in bridge-script.ts places body's box in document coordinates (its top already includes a collapsed top margin). It then adds the bottom margins that can collapse through body's bottom edge, by walking the last in-flow child chain. The walk skips out-of-flow children (display:none, absolute/fixed, floats, so also a trailing script and the bridge's own overlay), and stops at flex/grid, padded or bordered blocks and scroll containers. The ResizeObserver now also observes documentElement, because a change to a collapsed margin moves body without resizing it.

Why not max(documentElement.scrollHeight, body.scrollHeight): documentElement.scrollHeight never drops below the frame's own viewport, and an html{height:100%} root is the frame's height. Either way the frame could grow but never shrink (the initial 600px frame could not get smaller than 600). The new measure reads nothing that the frame sizes.

Verified in headless Chromium (real BRIDGE_SCRIPT in sandboxed srcdoc frames; each value checked against the html box height):

Case Before After
Card, collapsed top margin (40) 560 600
Card, collapsed top (40) + bottom (60) margins 560 660
Ordinary page (h1, paragraphs) 1062 1100
Nested collapse chain + trailing script 220
Flex body 160
Scroll box with an overflowing child margin (must not leak) 300
html{height:100%}, content 200 → 1500 → 80 230 → 1530 → 110

The html{height:100%} page now grows and shrinks, with no runaway. Pages whose body is itself viewport-sized (html,body{height:100%}, min-height:100vh) behave exactly as before.

Protocol version: not bumped. The resize message shape is unchanged and only the value is more accurate, so a cached older asset keeps the old measurement and nothing is misread. The bridge asset files are gitignored and generated at prepack (bun run --cwd packages/ui smoke:package regenerates them and passes).

Regression test: components/html-viewer/bridgeResize.test.ts runs the shipped function against a hand-built box model. It covers collapsed top and bottom margins, independence from the frame height, the rule that scroll and flex containers stop the chain, and scroll/overflow.

2. fenceTheme seam

configurePlannotatorUI({ fenceTheme?: (colorTheme, mode) => string | undefined }), also exposed as setFenceThemeResolver / resetFenceThemeResolver on utils/syntaxTheme. A return of undefined, an empty string or a throw falls through to the default resolution. An unknown theme name renders the fence as plain text, the same as an unknown language does.

It lives in resolveFenceTheme, so it applies consistently to every fence-style surface: Viewer fences, CodeBlock, the code-file hover preview, the plan diff view, and code review's HighlightedCode. The code-review diff pane's dark/light pair (resolveSyntaxTheme) is deliberately not affected. Plannotator passes nothing, so its output is unchanged. Documented in README.md and HANDOFF.md (seam catalog row plus an "unreleased" section). Tests are in hooks/useFenceTheme.seam.test.tsx; its DOM half is added to the CI DOM_TESTS list.

Checks

  • bun run typecheck: clean
  • bun test: all pass (the one initial failure was the DOM-test allowlist, now fixed); DOM_TESTS=1 bun test packages/ui: 2096 pass
  • bun run --cwd packages/ui smoke:package: passes
  • bun run --cwd apps/review build && bun run build:hook: ok
  • Live: CLI built from this branch into /tmp with a temp PLANNOTATOR_DATA_DIR, raw-HTML annotate of an ordinary page, a margin card and an html{height:100%} page. All rendered correctly. Note that Plannotator's own HTML surfaces (annotate and PR artifacts) use fullViewport, so they ignore the reported height. The auto-height path only matters for hosts, and it was verified with the harness above.

…eTheme seam

HtmlViewer's bridge reported document.body.scrollHeight, which omits a
child's top/bottom margin that collapses through a margin-less body, so
auto-sized frames were cut short. measureContentHeight places body's box in
document coordinates and adds the bottom margins that can collapse through
it. It never reads documentElement.scrollHeight or anything sized by the
frame, so html{height:100%} pages still grow and shrink. Resize message
shape unchanged, so no BRIDGE_PROTOCOL_VERSION bump.

configurePlannotatorUI({ fenceTheme }) lets a host pick the Shiki theme for
every fence-style surface (everything reading resolveFenceTheme /
useFenceTheme); undefined, empty or a throw falls through to the default.
Plannotator passes nothing.
The new height measure adds collapsed/body margins on top of content that
is sized by the frame (html,body{height:100%}, min-height:100vh, a 100vh
child), so every grow asked for another (Chromium: 600 -> 2600+ in under a
second). Add a feedback guard: the first measure after a grow the parent
applied holds the frame when the page still overflows by as much as before.
Later content changes still resize. Unit test drives the guard.
@backnotprop

Copy link
Copy Markdown
Owner Author

Review fix pushed: the new measure made viewport-sized pages (html,body{height:100%}, min-height:100vh, a 100vh child, with default body margin or a collapsed child margin) grow forever in headless Chromium with the real bridge (600 → ~2600 in under a second, still climbing; main was stable at 600). Added a feedback guard (nextResizeHeight): the first measure after a grow the parent applied holds the frame if the page still overflows by at least as much; a window resize listener tells it the grow landed. Those pages now settle at 610–640 instead of looping; card/ordinary/html{height:100%} grow+shrink, details, late image, dynamic margin, zoom, floats, transforms unchanged. Unit test added (fails without the guard). HANDOFF corrected (it claimed viewport-sized pages behave exactly as before). Pre-existing, not addressed: absolutely positioned content whose containing block is the root is not measured (main didn't either).

@backnotprop
backnotprop merged commit af8defe into main Oct 4, 2026
28 checks passed
@backnotprop
backnotprop deleted the fix/ui-frame-height-fence-theme-seam branch October 4, 2026 04:09
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.

1 participant