Skip to content

fix(useScrollableArea): a viewport is a tab stop from the first frame its content overflows - #8388

Merged
cixzhang merged 4 commits into
mainfrom
fix/scrollable-area-first-frame
Oct 25, 2026
Merged

cixzhang merged 4 commits into
mainfrom
fix/scrollable-area-first-frame

Conversation

@cixzhang

@cixzhang cixzhang commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

User impact

A keyboard user can't Tab into a scroll viewport for a moment after its content arrives already overflowing. Example: loaded data replacing a loading state in Schedule's week view. The viewport scrolls, but it isn't in the tab order until an animation frame later, and longer on a busy page. On main, an accessibility audit of the lab Schedule "Async Loader" story catches that window and reports scrollable-region-focusable (serious). It isn't in the a11y baseline, so the next PR that touches Schedule would fail pr-a11y on it.

After this change, the viewport is a tab stop in the same commit that lets its overflowing axis scroll, so it is reachable from its first scrolling frame.

Problem and solution fit

  • Why it happens:
    • useScrollableArea keeps a fitting viewport at overflow: clip.
    • Its first measure, in the mount layout effect, finds geometric overflow. That switches the axis to auto on the next render.
    • The same measure reads the axis as not scrollable, because it is still clip at that moment. So isScrollable, which grants the tab stop, stays false.
    • It only turns true when the style change triggers the MutationObserver's animation-frame remeasure.
  • Fix: a layout effect remeasures in the same commit when an axis has just gained overflow, or the axis mapping changed. The first scrolling frame then already carries tabindex="0". A loss of overflow still waits for the next frame, as on main, so content that fits only once a scrollbar takes space alternates at most once per frame and never loops inside a commit. No API, markup or style change.
  • Why the hook and not the story: the window exists in every adopter whose content mounts already overflowing: ScrollableArea, Table, CodeBlock, BottomSheet, DiffViewer and Schedule. The story is only where the audit happened to land in it.

Expected behavior and authority

  • spec:AST-025 FR12 (current): a viewport that owns keyboard access is in the tab order while an axis overflows.
  • component:Schedule AR1 (current): the time-grid viewport is that owner, a named region in the tab order while an axis overflows.

Smallest restoration

  • hooks/useScrollableArea.ts: keep a ref to the active measure, and remeasure in a layout effect when an axis gains overflow or the mapping changes (43 product lines). The same-commit remeasure calls the measurement body directly, so the frame already scheduled stays the only pending one.
  • hooks/useScrollableArea.test.tsx: a race test. Computed overflow follows the hook's own style, as in a browser, and the content is taller than the viewport at mount. With no animation frame run, the viewport is auto, tabindex="0", and isScrollable. It fails on main.
  • More hook tests: content that fits only under auto (a scrollbar that takes space) renders and flushes frames without looping; an overflowing mount leaves exactly one animation frame pending and unmount leaves none; a writing-mode change that moves the overflowing axis keeps the tab stop after one frame.
  • A patch changeset.

Evidence

Real Chromium (headless) with axe-core 4.12, Storybook built from main and from this branch. The story is lab-schedule--async-loader at 1280×720 with reduced motion. axe runs scrollable-region-focusable at 25 ms steps from 0 to 775 ms after the story renders, with CPU throttling ×1 and ×4 (64 scans each):

Build Scans with the violation
main 11 / 64 (the loaded viewport: role="region" "… time grid", data-scroll-axis="both", overflow set, no tabindex)
this branch 0 / 64

A full axe run on the settled story at this branch reports only landmark-one-main and page-has-heading-one, which the audit disables for story isolation.

Tests: useScrollableArea.test.tsx 18/18, each new case failing without the code it covers. The first-frame case fails on main, and the fits-only-under-auto case fails on this PR's first round. The ScrollableArea, BottomSheet, CodeBlock, Table, Schedule and DiffViewer suites pass. The Table perf budgets pass when run alone; they time out only on a loaded local machine, on main too.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

pnpm vitest run --project ui packages/core/src/hooks/useScrollableArea.test.tsx packages/core/src/ScrollableArea packages/lab/src/Schedule

… its content overflows

The first measure reads the viewport while this hook still has it at
overflow: clip, so it finds overflow but no scrollable axis; the axis became
scrollable on the next render, but the tab stop waited for an animation-frame
remeasure. Remeasure in the same commit once the overflow style changes.
Fixes the scrollable-region-focusable violation on the lab Schedule Async
Loader story, where loaded data replaces the loading fallback's viewport.
@vercel

vercel Bot commented Oct 25, 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 11:40am UTC

Request Review

@astracat-bot

astracat-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

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

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

Previews ready: Storybook and Sandbox were built with this PR's docsite. CI evidence will be added when available.

View Storybook for this PR · View Sandbox for this PR

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

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

The same-commit re-measure does make the viewport a tab stop from its first scrolling frame (the new test fails on main and passes here, and in Chromium the Schedule async loader's time grid now carries tabindex="0" in the commit that first lets it scroll), but one change is needed before this can land. The new layout effect in useScrollableArea.ts re-measures on every overflow change with no bound, so when a space-taking scrollbar decides whether the content fits (for example a 16:9 child at full width in a 540 × 300 block ScrollableArea with 15px scrollbars), each flip re-measures and flips back inside one commit until React throws "Maximum update depth exceeded" and unmounts the tree, where main only flickers once per frame. Bounding the follow-up (for example, re-measuring synchronously only when an axis has just gained overflow or the mapping changed) and adding a hook test whose content fits only under auto would resolve it.

[Automated review]

Comment on lines +354 to +356
useIsomorphicLayoutEffect(() => {
measureRef.current?.();
}, [overflow, mapping]);

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.

This follow-up re-measures on every overflow change, including the ones it causes. When a space-taking scrollbar decides whether the content fits (a 16:9 child at full width in a 540 × 300 block ScrollableArea with 15px scrollbars), the measure under auto sets overflow back to fitting, the clip commit re-runs this effect, and React throws "Maximum update depth exceeded" after about 50 nested commits; main alternates once per frame instead (AST-025 FR7, FR9). Bounding it, for example to commits where an axis has just gained overflow or the mapping changed, keeps the first-frame tab stop (the 15 hook tests still pass) and stops the abort.

…s gains overflow

Review round 1: content that fits only once a space-taking scrollbar appears
flipped overflow back and forth inside one commit until React aborted. The
same-commit remeasure now runs only when an axis has just gained overflow or
the axis mapping changed; a loss waits for the next frame, as on main. Adds a
hook test whose content fits only under auto.

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

The same-commit re-measure makes an overflowing viewport a tab stop from its first scrolling frame (the new test fails on main and passes here, and in Chromium the Schedule async loader's time grid now carries tabindex="0" in the commit that first lets it scroll), and the gain-only bound fixes the earlier in-commit loop, but two changes are needed before this can land.

  • measure starts with frame = null, so calling it from the new layout effect forgets the frame that observeResize scheduled at registration: every overflowing mount then runs two measurements in the next frame and unmount cancels only one of them (AST-025 FR7), so leave frame to the frame callback, for example with a measureNow the effect calls, and add a test that an overflowing mount leaves one frame pending and unmount leaves none.
  • The hasRemapped path keeps the tab stop through a writing-mode change, but the hook suite still passes with it removed, so add a test that switches an overflowing viewport to vertical-rl and expects tabindex="0" after one frame.

[Automated review]

const hasGainedOverflow =
(overflow.inline && !previous.inline) ||
(overflow.block && !previous.block);
const hasRemapped = mapping !== previousMappingRef.current;

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.

This keeps the tab stop through a writing-mode change that moves the overflowing axis (without it the stop disappears for one frame), but with || hasRemapped removed from the condition below all 16 hook tests still pass. A test that settles an overflowing block viewport, sets writing-mode: vertical-rl, runs one frame and expects tabindex="0" would protect it.

previousOverflowRef.current = overflow;
previousMappingRef.current = mapping;
if (hasGainedOverflow || hasRemapped) {
measureRef.current?.();

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.

measure begins with frame = null (line 223) because it was written as the frame callback. Called here, it runs while the frame that observeResize scheduled at registration is still pending (the shared observer calls its callback synchronously on registration), so that frame is forgotten: the MutationObserver then schedules a second one, two measurements run in the next frame (AST-025 FR7), and cleanup can cancel only the newer frame, so the older one outlives unmount. Calling a body that leaves frame alone, for example a measureNow that measure wraps with frame = null, keeps one pending frame.

…ng frame

Review round 2: the remeasure called the frame callback, which clears the
pending frame, so an overflowing mount ran two measurements in the next frame
and unmount could cancel only one. It now calls a measureNow body that leaves
the frame alone. Adds tests that an overflowing mount leaves one frame pending
and unmount none, and that a writing-mode change moving the overflowing axis
keeps the tab stop after one frame.
…comments

The set-state-in-effect suppressions became unused once the setters moved
into measureNow; lint-staged dropped them with their explanations. Restore
the explanations as comments.

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

No changes are needed: the same-commit re-measure makes an overflowing scroll viewport a tab stop from its first scrolling frame, the new first-frame test fails on main and passes here, and in Chromium and WebKit the Schedule async loader's loading time grid no longer scrolls without tabindex. The gain-only bound, the single pending frame and the writing-mode remap are each pinned by a test that fails without them. One optional touch-up: the comment at useScrollableArea.ts:363 and the test note at useScrollableArea.test.tsx:263 say a loss waits for the next frame, but when content fits only once a scrollbar takes space the same-commit re-measure itself records the loss, so rewording them would keep that case clear for the next reader.

[Automated review]

// a tab stop from the first frame it scrolls, not an animation frame later
// (`spec:AST-025` FR12). Only on a gain: content that fits only once a
// scrollbar takes space would otherwise flip overflow back and forth inside
// one commit; a loss waits for the next frame, as before.

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: the gate bounds what triggers a re-measure, not when a loss lands. When content fits only once a scrollbar takes space, the re-measure under auto records the loss itself, so the viewport writes auto and then clip within one frame (and the test note at useScrollableArea.test.tsx:263 has the same wording). Something like "a loss never triggers this re-measure; the next measurement after it waits for a frame, as before" would match what the code does.

@cixzhang
cixzhang merged commit 3353ab5 into main Oct 25, 2026
66 checks passed
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 26, 2026
useScrollableArea.ts: main's meta#8388 same-commit remeasure (measureNow, measureRef, the gained-overflow layout effect) with this branch's scroll path (publish, measuredMapping/measuredOverflow, onScroll); main's comment style replaces the set-state-in-effect disables. ci.yml: main's, which discovers browser accessibility specs by file name (meta#8367), so this branch's explicit AppShell keyboard step is dropped and the spec runs by its AppShell folder.
@github-actions
github-actions Bot deleted the fix/scrollable-area-first-frame branch October 26, 2026 06:31

This branch was successfully deployed

1 active deployment
Preview — 8703b89c 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.

1 participant