Skip to content

fix: measure only on-screen block toolbars (0.4.3) - #1

Merged
olibaron merged 3 commits into
mainfrom
fix/sticky-header-scroll-cost
Sep 10, 2026
Merged

olibaron merged 3 commits into
mainfrom
fix/sticky-header-scroll-cost

Conversation

@olibaron

Copy link
Copy Markdown
Contributor

Problem

Every code, diagram, and table toolbar registered its own capturing window scroll listener and requested an animation frame per scroll event. Each frame measured its block with getBoundingClientRect and called classList.add/remove — for every block on the page, on screen or not, whether or not the state changed. DOMTokenList add/remove rewrite the class attribute even when the token is already in the requested state, so every block produced a mutation record and a layout read per scroll frame.

Measured in a host app scrolling a 64k px transcript: ~600 mutation records/s and ~2,000 rect reads/s from toolbars alone. The chat-test example page showed ~5,000 mutation records/s (code, diagram, and table headers).

Change

runtime.watchStickyHeader(wrapper, header, fullscreen) replaces the per-block listeners:

  • one shared IntersectionObserver tracks which blocks are on screen; an off-screen block cannot span the toolbar band, so it is cleared and never measured;
  • one capturing scroll/resize listener is attached only while some block is on screen;
  • each pass reads every on-screen block before writing any class;
  • the scroll class is toggled only when the stuck state flips;
  • without IntersectionObserver every block counts as on screen (previous behaviour, minus redundant writes).

Code, mermaid, and table headers use it; onViewportScroll stays exported.

After the change, the same host scroll measured 9 mutation records/s.

Tests

  • New tests/coverage/sticky-header.test.ts: off-screen blocks are never measured, repeated scrolls in one state write nothing, leaving the screen clears the class and detaches the listener, registration waits for the wrapper ref, stop() clears the class.
  • final-branches.test.tsx: an unmounted header's update schedules nothing (the per-instance rAF path no longer exists).
  • npm test (888 passed), npm run typecheck, eslint on changed files, npm run build.

Release

Includes the chore: release 0.4.3 bump (patch: no API change).

🤖 Generated with Claude Code

olibaron and others added 2 commits September 10, 2026 20:57
…hange

Every code, diagram, and table toolbar registered its own capturing window
scroll listener and requested one animation frame per scroll event. Each
frame measured its block with getBoundingClientRect and called
classList.add or classList.remove, whether or not the block was on screen
and whether or not the state changed. DOMTokenList add and remove rewrite
the class attribute even when the token is already in the requested state,
so a long transcript produced one attribute mutation and one layout read
per block per scroll frame (measured: ~600 mutation records and ~2,000
rect reads per second while scrolling a 64k px transcript).

watchStickyHeader replaces the per-block listeners: one IntersectionObserver
tracks which blocks are on screen, one capturing scroll/resize listener is
attached only while some block is, each pass reads every on-screen block
before writing any class, and the class is toggled only when the stuck
state flips. Without IntersectionObserver every block counts as on screen.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Block toolbars stop costing work per scroll frame: only on-screen blocks
are measured, through one shared listener, and the "scroll" class is
written only when it flips. No API change, so this is a patch.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Coverage Report for Unit

Status Category Percentage Covered / Total
🟢 Lines 100% (🎯 100%) 780 / 780
🟢 Statements 100% (🎯 100%) 790 / 790
🟢 Functions 100% (🎯 100%) 80 / 80
🟢 Branches 100% (🎯 100%) 571 / 571
File CoverageNo changed files found.
Generated in workflow #47 for commit 5d285e3 by the Vitest Coverage Report Action

@github-actions

github-actions Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Coverage Report for All

Status Category Percentage Covered / Total
🟢 Lines 100% (🎯 100%) 3110 / 3110
🟢 Statements 100% (🎯 100%) 3145 / 3145
🟢 Functions 100% (🎯 100%) 485 / 485
🟢 Branches 100% (🎯 100%) 1938 / 1938
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
lib/runtime.ts 100% 100% 100% 100%
lib/code/header.tsx 100% 100% 100% 100%
lib/mermaid/header.tsx 100% 100% 100% 100%
lib/table/header.tsx 100% 100% 100% 100%
Generated in workflow #47 for commit 5d285e3 by the Vitest Coverage Report Action

Covers the server no-op watch, an intersection delivered after stop, a
stop before the wrapper attaches, and an update on or off screen. Header
wrapper getters read the required ref directly, and update() recomputes
through isStuck, which already clears a fullscreen block.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@olibaron
olibaron merged commit 2ccb8bb into main Sep 10, 2026
6 checks passed
@olibaron
olibaron deleted the fix/sticky-header-scroll-cost branch September 10, 2026 19:05
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