Skip to content

perf(useScrollableArea): a scroll updates the edges from geometry, without reading computed style - #8041

Merged
cixzhang merged 4 commits into
meta:mainfrom
vjeux:uikit/c10-scroll-edges
Oct 28, 2026
Merged

cixzhang merged 4 commits into
meta:mainfrom
vjeux:uikit/c10-scroll-edges

Conversation

@vjeux

@vjeux vjeux commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

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):

main this PR
Four reads per scroll event No reads while scrolling

Playwright Chromium, phone profile, real touch drags over the Playground, identical over 3 runs:

Before After
Computed-style reads of the viewport, 44 scroll events 176 0
Restyles/layouts forced from script in a 150 px drag, page restyling every frame 22 0 (the lifecycle does them)
Frame callbacks requested per scroll event 1 0
  • Two new unit tests fail on the base hook.
  • Existing hook, ScrollableArea, Table, CodeBlock, BottomSheet and lab DiffViewer/Schedule suites pass. One test used a scroll to signal a style-only change; it now flushes the attribute observer, which carries that change in a browser.
  • fix(ScrollableArea): complete hardening audit #6314 merges cleanly on top, both suites green, in either order.

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/useScrollableArea FR1–FR5, PR1–PR2; component:ScrollableArea PR1). 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

  • No intended public API, product behavior, visual direction, or policy change.
  • Product changes discovered during the work were removed or split.
  • Public text and artifacts contain no internal Meta context.

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 typecheck and pnpm -F @astryxdesign/lab typecheck, strict eslint (CI=true) and prettier --check on changed files, node scripts/check-changesets.mjs

@vercel

vercel Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Oct 25, 2026 3:57pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 24, 2026
@astracat-bot

astracat-bot Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 25, 2026, 4:21 PM UTC

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR

🧪 Sandbox Preview

View Sandbox for this PR

Component Size

Component Import before Import after Delta CSS before CSS after Delta
core/BottomSheet 25.9KB 26.2KB +302B 7.5KB 7.5KB 0B
core/CodeBlock 55.6KB 55.9KB +267B 10.4KB 10.4KB 0B
core/ContextMenu 74.0KB 74.3KB +290B 14.2KB 14.2KB 0B
core/DateInput 89.7KB 90.0KB +300B 14.3KB 14.3KB 0B
core/DateTimeInput 99.0KB 99.2KB +295B 14.7KB 14.7KB 0B
core/DropdownMenu 69.0KB 69.0KB -1B 14.4KB 14.4KB 0B
core/Markdown 133.3KB 133.6KB +271B 16.9KB 16.9KB 0B
core/MoreMenu 69.2KB 69.2KB +1B 14.4KB 14.4KB 0B
core/MultiSelector 72.0KB 72.3KB +290B 14.1KB 14.1KB 0B
core/Pagination 83.7KB 84.0KB +313B 14.8KB 14.8KB 0B
core/PowerSearch 144.1KB 144.3KB +282B 18.1KB 18.1KB 0B
core/ScrollableArea 10.4KB 10.7KB +290B 4.2KB 4.2KB 0B
core/Selector 75.2KB 75.4KB +268B 14.5KB 14.5KB 0B
core/Table 80.1KB 80.4KB +299B 14.6KB 14.6KB 0B
core/TableCell 75.0KB 75.3KB +285B 14.4KB 14.4KB 0B
core/TableHeaderCell 74.8KB 75.1KB +285B 14.3KB 14.3KB 0B
core/TimeInput 76.1KB 76.4KB +287B 13.2KB 13.2KB 0B

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 Summary

Gzip figures measure each package's entry file, not the code an import pulls in.

Package Entry (ESM) Entry gzip before Entry gzip after Delta
@astryxdesign/core 4.9KB 1.2KB 1.2KB 0B
Package stylesheet Gzip before Gzip after Delta
@astryxdesign/core 33.7KB 33.7KB 0B

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

48 of 504 shot(s) changed.

A change here is a question, not a failure: check whether the after is the
picture you intended. Record that review on the PR. Baseline maintenance is an
explicit dispatch of CI; this report never rewrites the baseline or adds a release gate.

component story theme mode pixels
Selector Indicator Space Bottom Sheet Narrow End RTL neutral light 753,933
Selector Indicator Space Bottom Sheet Wide Start neutral light 740,072
RichTextEditor Markdown parity: side by side probe dark 556,900
RichTextEditor Markdown parity: side by side probe light 538,816
Chat Full AI Chat probe light 230,823
Chat Full AI Chat probe dark 229,805
AppShell With Mobile Nav probe light 217,010
AppShell With Mobile Nav probe dark 216,960
Collapsible Dividers probe dark 66,445
Collapsible Dividers probe light 66,445
AppShell Playground neutral dark 65,198
AppShell Playground neutral light 63,788
Chat Default neutral dark 36,304
Chat Default neutral light 35,767
Chat Empty State probe light 23,531
Chat Empty State probe dark 22,622
ChatAutoScroll Scroll Behavior Comparison neutral dark 15,378
ChatAutoScroll Scroll Behavior Comparison neutral light 14,107
Chat With Attachments probe light 13,406
Chat With Attachments probe dark 11,232

and 28 more.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 state and the data-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 0 instead of 200 in the zero-size step, because as written the test still passes with the zero-size guard in measureLogicalScrollPosition removed.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 direction change that reaches the viewport only through a stylesheet, with no dir, class or style attribute 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]

Comment on lines +202 to +211
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`],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

@cixzhang
cixzhang merged commit 72cd1b1 into meta:main Oct 28, 2026
65 checks passed
@vjeux
vjeux deleted the uikit/c10-scroll-edges branch October 28, 2026 17:25
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 28, 2026
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.

This branch was successfully deployed

1 active deployment
Preview — bb50c2ae Deployed Oct 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants