Repository navigation
perf(useScrollableArea): a scroll updates the edges from geometry, without reading computed style - #8041
Conversation
…thout reading computed style
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report📚 Storybook Preview🧪 Sandbox PreviewComponent Size
Sizes are gzip. Import counts the JavaScript the import loads eagerly; CSS counts the package stylesheet rules referenced by the modules it can load from its own package, lazily loaded ones included. Bundle Size SummaryGzip figures measure each package's entry file, not the code an import pulls in.
Accessibility AuditStatus: No accessibility violations detected. Visual Regression48 of 504 shot(s) changed. A change here is a question, not a failure: check whether the after is the
and 28 more. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
There was a problem hiding this comment.
Changes requested: the scroll path does what it says (in Chromium, Firefox and WebKit, scrolling no longer reads the viewport's computed style or requests an animation frame, and the reported edges match the current behavior in LTR, RTL and vertical writing modes), but two small fixes are needed first:
- In the changeset and the PR's scope line, delete the clause saying
stateand thedata-scroll-*attributes now update in the frame that scrolled; their timing is unchanged, because they still arrive through React's next commit. - In "keeps the last valid edges when a scroll arrives while the viewport has no box", scroll to
0instead of200in the zero-size step, because as written the test still passes with the zero-size guard inmeasureLogicalScrollPositionremoved.
[Automated review]
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [perf] Scrolling a `ScrollableArea`, or any viewport that uses `useScrollableArea` (Table, CodeBlock, BottomSheet's body), no longer reads computed style on each frame. Before, every scroll event triggered a full measurement in the next animation frame: four computed-style reads plus geometry. When something else on the page had changed style earlier in that frame, this forced a style recalculation from script. Now the scroll event works out the logical edges from box geometry alone, before animation-frame callbacks run, so `state` and the `data-scroll-*-start` / `data-scroll-*-end` attributes update in the frame that scrolled rather than the next one. Size, style and writing-mode changes still trigger a full measurement, at most once per frame. |
There was a problem hiding this comment.
The last clause does not match the shipped behavior. Measured in Chromium, Firefox and WebKit, data-scroll-* flips land at the same frame offset from their scroll event before and after this change, because publish still goes through setMeasured and React's next commit. Please delete "so state and the data-scroll-*-start / data-scroll-*-end attributes update in the frame that scrolled rather than the next one"; the computed-style and frame-request statements are accurate.
There was a problem hiding this comment.
Fixed in f6935a6: the clause is gone. The changeset now says the scroll event works out the edges from box geometry with no computed style and no frame request, and the description no longer claims a timing change.
| void act(() => viewport.dispatchEvent(new Event('scroll'))); | ||
| flushFrame(); | ||
|
|
||
| setGeometry(viewport, {clientWidth: 0, clientHeight: 0, scrollLeft: 200}); |
There was a problem hiding this comment.
With scrollLeft: 200, the edges recomputed against a zero-width box (start false, end false) equal the kept state, so this test still passes when the zero-size guard in measureLogicalScrollPosition is removed. With scrollLeft: 0 it still passes with the guard and fails without it, because atStart turns true.
There was a problem hiding this comment.
Fixed in f6935a6: the zero-size step now scrolls to 0. Checked both ways: it fails with the zero-size guard in measureLogicalScrollPosition removed and passes with it.
…drop the timing claim The zero-size step scrolled to 200, where edges recomputed against a box with no size equal the kept state, so the test passed with the guard in measureLogicalScrollPosition removed. At offset 0 the start edge would flip, so the step now fails without the guard. The changeset no longer says that state and the data-scroll attributes update in the frame that scrolled: both paths publish through the same state update and React's next commit, so that timing is unchanged.
There was a problem hiding this comment.
Changes requested: the new scroll path works as described (in Chromium and Firefox, scrolling no longer reads the viewport's computed style, 228 reads to 0 over 60 scroll events, and the edges match the base in LTR, RTL and vertical-rl), and both earlier requests are fixed. One test is still needed: nothing exercises vertical writing modes on the scroll path, so reading mapping.inlineReversed for the block axis in measureLogicalScrollPosition leaves vertical-rl block edges at start while scrolled to the end, and all 19 hook tests still pass. Please add a vertical-rl block-axis case that scrolls to the end and asserts the end edge with no frame requested.
- Optional: a
directionchange that reaches the viewport only through a stylesheet, with nodir,classorstyleattribute change, now keeps the old mapping while scrolling; scheduling a full measurement when an offset's sign contradicts the measured direction would close that gap.
[Automated review]
| const isX = mapping[logicalAxis] === 'x'; | ||
| const maxOffset = isX | ||
| ? element.scrollWidth - element.clientWidth | ||
| : element.scrollHeight - element.clientHeight; | ||
| return { | ||
| overflows: maxOffset > SCROLL_OVERFLOW_TOLERANCE, | ||
| ...logicalEdges( | ||
| isX ? element.scrollLeft : element.scrollTop, | ||
| maxOffset, | ||
| mapping[`${logicalAxis}Reversed`], |
There was a problem hiding this comment.
Nothing tests this axis and reversal choice in a vertical writing mode. With mapping.inlineReversed in place of mapping[`${logicalAxis}Reversed`] here, a vertical-rl block axis reports the start edge while scrolled to its end, and all 19 tests in useScrollableArea.test.tsx still pass; reading x for every inline axis at line 202 also passes, silently sending vertical scrolls back to the full measurement. A vertical-rl axis="block" case that measures through transitionend, sets scrollLeft: -120, dispatches a scroll and expects no frame and {isScrollable: true, atStart: false, atEnd: true} fails under both.
There was a problem hiding this comment.
Fixed in e900045: a vertical-rl axis="block" case measures through transitionend, sets scrollLeft -120, dispatches a scroll, and expects {isScrollable: true, atStart: false, atEnd: true} with no frame requested. Checked against both mutations: it fails with the block axis reading inlineReversed, and with every inline axis read from x.
| const position = measureLogicalScrollPosition( | ||
| viewport, | ||
| logicalAxis, | ||
| measuredMapping, |
There was a problem hiding this comment.
Optional: only a full measurement refreshes measuredMapping, so a direction change made by a stylesheet alone (no dir, class or style attribute change the observer sees) keeps the old mapping while the user scrolls; in Chromium and Firefox an area turned RTL that way reports the start edge at its inline end, where the base re-measured on the scroll. Treating an offset whose sign contradicts the stored mapping (negative scrollLeft on a non-reversed axis, positive on a reversed one) as unexplained and calling scheduleMeasure() would close it.
There was a problem hiding this comment.
Fixed in e900045: measureLogicalScrollPosition also reports an offset whose sign the stored mapping rules out (negative on a forward axis, positive on a reversed one), and the scroll path then schedules the full measurement. A test flips the computed direction to rtl with no attribute change, scrolls to -200, and expects a frame request and the end edge after it. It fails on f6935a6.
…out re-measures; cover vertical-rl on the scroll path - measureLogicalScrollPosition also reports when an offset's sign contradicts the stored mapping (negative on a forward axis, positive on a reversed one). A direction changed by a stylesheet alone reaches no observer, so the scroll path now schedules the full measurement then, as the base did on every scroll. - A vertical-rl block-axis test scrolls to the end on the scroll path and expects the end edge with no frame requested; it fails if the block axis reads the inline reversal or if every inline axis is read from x.
There was a problem hiding this comment.
Ready to approve: scrolling a useScrollableArea viewport now derives its edges from box geometry without reading computed style or requesting an animation frame (240 reads to 0 over 60 scroll events in Chromium and Firefox), and the reported edges match the current behavior in LTR, RTL and vertical writing modes. Both points from the last review are in: the vertical-rl block-axis scroll test fails when that mapping breaks, and a direction changed by a stylesheet alone now re-measures on the next scroll and reports the right edge. Nothing else is needed from you.
[Automated review]
…0-scroll-edges # Conflicts: # packages/core/src/hooks/useScrollableArea.ts
There was a problem hiding this comment.
Ready to approve: a scroll on a useScrollableArea viewport now works out its edges from box geometry, with no computed-style reads or animation-frame requests (72 reads to 0 over 18 scroll events in Chromium, Firefox and WebKit), and the edges callers see, and when they see them, match the current behavior in LTR, RTL and vertical-rl. The new tests pin each fallback (a measurement already pending, lost overflow, a viewport with no box, a direction changed by a stylesheet alone) and fail when those guards are removed. Nothing else is needed from you.
[Automated review]
useScrollableArea.ts conflicted: meta#8041 merged in a later form than the one this branch carried. That file takes this PR's version, which is meta#8041's merged code plus this PR's own changes (the shared mutation observers and the watch on a viewport's direct children). AppShell.tsx merged textually but declared mainRef twice: meta#8723 added an object ref for the content glide, and this PR's ref is the scroll measurement under fill. This PR's becomes mainScrollRef, and main's node goes to both through useMergedRefs.
Maintainer impact
Scrolling a viewport built on
useScrollableArea(ScrollableArea, Table, CodeBlock, BottomSheet's body) ran a full measurement per scroll event: an animation-frame callback read the viewport's computed style four times, plus its geometry, to tell whether the offset reached an edge. When anything had changed style earlier in that frame, those reads forced a style recalculation, and the edge state lagged the content by a frame.Evidence
iOS 26.2 Safari, full screen (Safari's URL text filled with its pill colour), finger scrolls (dots) on the ScrollableArea Playground to the end and back; the overlay counts computed-style reads of the viewport and shows both edges (waits between swipes shortened):
Playwright Chromium, phone profile, real touch drags over the Playground, identical over 3 runs:
Intended invariant
What the hook reports doesn't change: effective axes, logical edges in every writing mode and direction, the 1 px tolerance, keeping the last valid state while there is no box, and stable state objects (
module:ScrollableArea/useScrollableAreaFR1–FR5, PR1–PR2;component:ScrollableAreaPR1). Size, style, writing-mode and subtree signals still coalesce into one full measurement per frame.Change
A scroll only moves the offset. The scroll event now derives the edges from box geometry with the writing-mode mapping of the last full measurement: no computed style, no frame request. Scroll events fire at most once per frame, before animation-frame callbacks, when layout is normally clean. If overflow appears or disappears on a requested axis, or the offset's sign contradicts that mapping (a direction changed by a stylesheet alone), the full measurement is scheduled; one already pending in that frame covers the scroll.
Scope
Testing
vitest run packages/core/src/hooks/useScrollableArea.test.tsx(25 pass, with main's fix(useScrollableArea): a viewport is a tab stop from the first frame its content overflows #8388 same-commit remeasure merged in; on the base hook 2 fail; the zero-size step fails with its guard removed; the vertical-rl block-axis case fails if the block axis reads the inline reversal or every inline axis reads x; the direction-contradiction case fails on the previous head)vitest run packages/core/src/Table packages/core/src/CodeBlock packages/core/src/BottomSheet packages/core/src/hooks packages/core/src/ScrollableArea packages/lab/src/DiffViewer packages/lab/src/Schedule(88 files, 1,677 tests pass)pnpm -F @astryxdesign/core typecheckandpnpm -F @astryxdesign/lab typecheck, stricteslint(CI=true) andprettier --checkon changed files,node scripts/check-changesets.mjs