Skip to content

fix(BottomSheet): sync its return target at commit - #10918

Merged
cixzhang merged 6 commits into
meta:mainfrom
vjeux:fix/bottom-sheet-return-target-at-commit
Nov 12, 2026
Merged

cixzhang merged 6 commits into
meta:mainfrom
vjeux:fix/bottom-sheet-return-target-at-commit

Conversation

@vjeux

@vjeux vjeux commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Render-time ref writes fixed, each read in a callback: finalFocusRefRef and stackViewRef in core/BottomSheet/BottomSheet.tsx. Red-first: yes, "returns focus to the committed finalFocusRef when it unmounts after React discards a render with another" fails on main.

User impact

People using a BottomSheet with a finalFocusRef, in an app that renders with transitions. The sheet kept its finalFocusRef and its stack view in refs written while it rendered. A render React discards, as a suspended transition does, left its values there. A sheet the page removed after a discarded render with another finalFocusRef returned focus to that render's target.

Problem and solution fit

A write during render runs for every render, including the ones React throws away. The stack reads the return target when it closes or removes the sheet, which can happen in a layout effect or an unmount cleanup.

The return target now comes from useCommittedRef, Core's shared hook for latest-value refs. It writes at commit, before every layout effect, where the stack can close the sheet. The stack's callback and the removal repair read it through one function when they run. The stack view is no longer copied during render. The registry's onView already writes it with every view it reports, together with the state, so the copy only ever repeated that write.

Expected behavior and authority

  • React's useRef contract: do not write ref.current during rendering, except to initialize it.
  • architecture:react-component-runtime INV3 and INV17: a latest-value ref goes through useCommittedRef, and render writes no ref. INV4: no rendered value comes from a ref mutation.

Smallest restoration

Two refs in BottomSheet.tsx. No API, visual or default changes.

Evidence

  • Reproduction before the change: the new test fails on main badd698, with the whole test file run.
  • Result after the change, at e8ac261: it passes.
  • Representative unchanged path: the existing focus-return, stack and scrim tests still pass, with the sheets built on this component.
  • Regression test or other mutation-sensitive proof: the test fails while the return target is written during render.

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

All runs below are at this PR's head, e8ac261.

The test renders an open sheet with focus inside it. A transition renders it with another finalFocusRef while a sibling suspends for good, so React never commits that render. The page then removes the sheet, and its removal returns focus.

Ref Now Test
finalFocusRefRef Written at commit by useCommittedRef Red-first: focus returns to the committed target, not the discarded one.
stackViewRef Written only by the registry's onView No new test. The registry writes the ref with each view it reports, so a render never had a newer value to copy.
  • pnpm exec vitest run packages/core/src/BottomSheet packages/core/src/Tokenizer packages/core/src/DropdownMenu packages/core/src/Toast --project ui: 41 files, 1431 tests pass.
  • pnpm exec vitest run --project ui: 1,002 files, 24,751 tests pass.
  • BottomSheet.test.tsx run whole against main's sheet: the new test fails, and the other 158 pass.
  • @astryxdesign/core typecheck: 0 errors. ESLint and Prettier on the changed files: clean.
  • History: main merged at 7e7fbae on Nov 11, with no conflicts, so CI runs on a current base. The change's own diff is unchanged.
  • History: the merge of main 1740654 on Nov 12 has no conflicts and brings useCommittedRef. The next commit moves the return target onto it in place of the sheet's own insertion effect.
  • Browser specs at this head, on a Storybook built at it: BottomSheet interaction specs, Chromium 22 passed (1 skipped) and WebKit (CI-profile container) 12 passed (11 skipped). BottomSheet a11y specs, Chromium 92 passed; their .browser specs in the WebKit container, 55 passed (1 skipped).
  • History: the merge of main 06b7e61 on Nov 12 has no conflicts. It brings main's changes to four files that BottomSheet.tsx reaches through its imports: BottomSheetPanel.tsx, motion/animator.ts, useMobileKeyboard.ts and keyboardHeight.ts. The change's own diff is unchanged.
  • History: the merge of main badd698 on Nov 12 has no conflicts. It brings main's change to motion/animator.ts, which BottomSheet.tsx reaches through its imports. The change's own diff is unchanged.

The sheet kept its finalFocusRef and its stack view in refs written
while it rendered. A render React discards, as a suspended transition
does, left its values there. A sheet the page removed after a
discarded render with another finalFocusRef then returned focus to
that render's target.

The return target now syncs in an insertion effect, ahead of the
layout effects where the stack can close the sheet. The stack view is
no longer copied during render: the registry's onView already writes
it with each view it reports.
@vercel

vercel Bot commented Nov 9, 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 Nov 12, 2026 5:02pm UTC

Request Review

@astracat-bot

astracat-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 12, 2026, 5:09 PM UTC

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · e8ac261

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
BottomSheetSwitcher 18.7 kB (0) · 3.4 kB CSS (0) — ✗ · ✓ · —
BottomSheet 69.9 kB (−5 B) · 7.7 kB CSS (0) — ✗ · ✓ · —
richtext/RichTextEditorToolbar 284.9 kB (+19 B) · 454 B CSS (0) — — · — · —
22 more components
Component Size LoC Visual · A11y · RTL
Markdown 193.6 kB (+7 B) · 21.0 kB CSS (0) — — · — · —
Table 130.6 kB (+5 B) · 18.7 kB CSS (0) — — · — · —
lab/CommentComposer 157.0 kB (+3 B) · 1.9 kB CSS (0) — — · — · —
DropdownMenu 102.6 kB (+1 B) · 17.9 kB CSS (0) — — · — · —
Tokenizer 102.2 kB (+1 B) · 19.5 kB CSS (0) — — · — · —
TableCell 114.6 kB (0) · 18.2 kB CSS (0) — — · — · —
MoreMenu 102.8 kB (−1 B) · 17.9 kB CSS (0) — — · — · —
PowerSearch 301.0 kB (−1 B) · 24.7 kB CSS (0) — — · — · —
lab/TourStep 102.3 kB (−2 B) · 590 B CSS (0) — — · — · —
lab/ChatReactionBar 112.5 kB (−3 B) · 1.1 kB CSS (0) — — · — · —
lab/Comments 360.4 kB (−4 B) · 2.5 kB CSS (0) — — · — · —
lab/CommentThread 353.7 kB (−7 B) · 2.4 kB CSS (0) — — · — · —
ContextMenu 113.4 kB (−9 B) · 17.8 kB CSS (0) — — · — · —
Pagination 115.7 kB (−11 B) · 18.2 kB CSS (0) — — · — · —
TableHeaderCell 114.2 kB (−14 B) · 18.0 kB CSS (0) — — · — · —
lab/ChatEmojiPicker 111.3 kB (−15 B) · 773 B CSS (0) — — · — · —
Popover 96.0 kB (−16 B) · 14.6 kB CSS (0) — — · — · —
DateInput 194.5 kB (−17 B) · 17.4 kB CSS (0) — — · — · —
MultiSelector 99.8 kB (−17 B) · 17.2 kB CSS (0) — — · — · —
Selector 105.5 kB (−18 B) · 17.8 kB CSS (0) — — · — · —
DateTimeInput 217.0 kB (−25 B) · 19.1 kB CSS (0) — — · — · —
TimeInput 168.5 kB (−44 B) · 16.4 kB CSS (0) — — · — · —
Visual changes: 2 snapshots in 1 component

A change is a question for the reviewer, not a failure.

Component · story Before After Diff
BottomSheet · Text Only (neutral, dark) before frame after frame diff frame
BottomSheet · Text Only (neutral, light) before frame after frame diff frame

Package size: @astryxdesign/core 7.40 MB packed (+227 B); no new dependencies.

Touched components checked; add ci:run-visual, ci:run-a11y or ci:run-rtl to run the full suites.


Generated by PR Enrichment workflow | 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.

Approved pending CI: CI has not run on this head yet.

Moving the return-target write into an insertion effect keeps a discarded transition render out of it, so a sheet removed after such a render returns focus to its committed finalFocusRef; the new test fails without the change and passes with it, and a Chromium check shows the same.

Next step: this is a draft from a fork, so a maintainer adds the ci:run label, and marking the pull request ready for review then starts CI.

  • Optional: the new test sits in the hasScrim={false} (non-modal) block but renders a modal sheet; move it to the "BottomSheet removed while open" suite in BottomSheetCloseFocus.test.tsx, or add hasScrim={false}.

[Automated review]

}
});

it('returns focus to the committed finalFocusRef when it unmounts after React discards a render with another', async () => {

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.

Non-blocking: this test sits in describe('hasScrim={false} (non-modal)'), but the sheet below passes no hasScrim={false}, so it runs the modal path while a failure reports a non-modal case. Move it to the "BottomSheet removed while open" suite in BottomSheetCloseFocus.test.tsx, or add hasScrim={false}; with that prop it still fails without the fix and passes with it.

},
[],
);
// The stack's view for the handlers. The registry's `onView` writes it with

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.

Non-blocking (INV29): "The stack's view for the handlers." restates the ref's name. Keep only the invariant, for example:

// Only the registry's `onView` writes it, in the same call that sets the state.

@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec fd28a7c

@cixzhang cixzhang added the ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) label Nov 10, 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.

Approved pending CI: CI has not run on this head yet.

Moving the return-target write into an insertion effect keeps a discarded transition render out of it, so a sheet removed after such a render returns focus to its committed finalFocusRef. The new test fails without the change and passes with it, for modal and non-modal sheets, and a Chromium check shows the same.

Next step: the ci:run label is already on this pull request, so CI runs once it is marked ready for review.

  • Optional: the two earlier notes still apply: the new test sits in the hasScrim={false} (non-modal) block but renders a modal sheet, and the comment above stackViewRef restates its name.

Automated review (maintainer link)

The sheet copied the committed finalFocusRef into a ref in its own
insertion effect. The shared internal hook makes the same write, so
the sheet calls it. The stack's callback and the removal repair read
the target through one function, so focus returns where it did.
@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec fcdf454

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

Approved pending CI: CI has not finished on this head yet.

Reading the return target through useCommittedRef keeps a discarded transition render out of it, so a sheet removed after such a render returns focus to its committed finalFocusRef, as the sheet's focus-return rule requires. CI is running on this head now, and merging waits for it.

  • Optional: the new test sits in the hasScrim={false} (non-modal) block but renders a modal sheet; move it to the "BottomSheet removed while open" suite in BottomSheetCloseFocus.test.tsx, or add hasScrim={false}.
  • Optional: three comments in BottomSheet.tsx restate names or misplace where the target is read; the inline suggestions give shorter wording.
Claims and files checked
  • Changeset: the sheet writes its focus return target at commit and no longer copies its stack view during render: verified (BottomSheet.tsx:464, :567-569).
  • Changeset and description: a sheet removed after a discarded render returns focus to its committed finalFocusRef: verified (the commit-time write at BottomSheet.tsx:464 and the test sequence at BottomSheet.test.tsx:980).
  • The new test fails on the base branch and passes at this head: verified from the code (the base writes the discarded target during render; the head writes only at commit).
  • The registry's callback and the removal repair read the target through one function: verified (BottomSheet.tsx:622-624, :649, :687).
  • The stack view changes only in the registry's onView, so the render-time copy only repeated that write: verified (BottomSheet.tsx:651-652).
  • No API, visual or default change: verified (only the unexported standalone sheet changes).
  • The governing rules (no ref writes during render, latest-value refs through the shared hook, no rendered value from a ref): verified against the current runtime architecture record.
  • Existing tests, typecheck, lint and formatting pass: consistent with the code (types and hook dependencies line up); CI confirms.
  • Both merges of the base branch are conflict-free, and the last brings the shared hook that the next commit adopts: verified from the history.
  • .changeset/bottom-sheet-return-target-at-commit.md: clear.
  • packages/core/src/BottomSheet/BottomSheet.test.tsx: test placement note.
  • packages/core/src/BottomSheet/BottomSheet.tsx: comment wording notes.

Automated review (maintainer link)

Comment on lines +462 to +463
// The committed `finalFocusRef`. The stack reads it when it closes the
// sheet, which can happen in a layout effect.

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.

Non-blocking (INV29): the registry reads this target in its passive-effect seal (bottomSheetStack.ts:429), not in a layout effect; the layout-effect read is this sheet's own removal cleanup (line 687). The first sentence restates the hook's name. Keep only the reason, for example:

// A render React discards must not change where focus returns.

dialog == null
? null
: prepareFocusRepairOnRemoval(dialog, {keepRestingFocus: true});
// Where focus returns when the sheet leaves, read when it leaves.

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.

Non-blocking (INV29): "Where focus returns when the sheet leaves" restates getReturnTarget. Keep the constraint, for example:

// Read it when the sheet leaves. This effect does not run again for a new `finalFocusRef`.

@vjeux
vjeux marked this pull request as ready for review November 12, 2026 10:32
@vjeux
vjeux marked this pull request as draft November 12, 2026 14:05
@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec 980236a

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

Approved pending CI: CI skipped every job on this head because the pull request is a draft, and it runs once the pull request is marked ready for review.

Reading the return target through useCommittedRef keeps a discarded transition render out of it, so a sheet removed after such a render returns focus to its committed finalFocusRef, as the sheet's focus-return rule requires.

  • Optional: the new test sits in the hasScrim={false} (non-modal) block but renders a modal sheet; move it to the "BottomSheet removed while open" suite in BottomSheetCloseFocus.test.tsx, or add hasScrim={false}.
  • Optional: three comments in BottomSheet.tsx (lines 462, 567 and 622) restate names or misplace where the target is read; the open inline suggestions give shorter wording.
Claims and files checked
  • Changeset: the sheet writes its focus return target at commit and no longer copies its stack view during render: verified (BottomSheet.tsx:464, :567-569).
  • Changeset and description: a sheet removed after a discarded render returns focus to its committed finalFocusRef: verified (the shared hook's commit-time write at BottomSheet.tsx:464, and the test sequence at BottomSheet.test.tsx:980).
  • Both refs were written during render and are read only in callbacks and effects: verified (base BottomSheet.tsx:462, :566; head reads at :581, :594, :623-624, :643, :759, :780, :898-899).
  • The new test fails on the base branch and passes at this head: verified from the code (the base writes the discarded target during render; the head writes only at commit); not run here.
  • The target is written at commit, before every layout effect: verified (useCommittedRef.ts:35-38 writes in an insertion effect).
  • The registry's callback and the removal repair read the target through one function: verified (BottomSheet.tsx:623-624, :649, :687).
  • The stack view changes only in the registry's onView, so the render-time copy only repeated that write: verified (BottomSheet.tsx:651-652).
  • The governing rules (no ref writes during render, latest-value refs through the shared hook, no rendered value from a ref): verified against the current runtime architecture record.
  • No API, visual or default change: verified from the code (only the unexported standalone sheet's body changes, and no render reads either ref).
  • Existing tests, typecheck, lint, formatting and browser specs pass: consistent with the code (types and hook dependencies line up); not rerun here, and CI has not run on this head.
  • Each merge of the base branch is conflict-free, the second brings the shared hook, and the last brings changes to four files the sheet imports while the change's own diff stays the same: verified from the history.
  • One defect, no tagalong, no hidden API or interaction change, no internal context: verified.
  • .changeset/bottom-sheet-return-target-at-commit.md: clear.
  • packages/core/src/BottomSheet/BottomSheet.test.tsx: test placement note.
  • packages/core/src/BottomSheet/BottomSheet.tsx: comment wording notes.

Automated review (maintainer link)

@vjeux
vjeux marked this pull request as ready for review November 12, 2026 15:07
github-actions Bot added a commit that referenced this pull request Nov 12, 2026
@vjeux
vjeux marked this pull request as draft November 12, 2026 16:01
@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec e8ac261

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

Approved pending CI: CI skipped every job on this head because the pull request is a draft, and it runs once the pull request is marked ready for review.

Reading the return target through useCommittedRef keeps a discarded transition render out of it, so a sheet removed after such a render returns focus to its committed finalFocusRef, as the sheet's focus-return rule requires.

  • Optional: the new test sits in the hasScrim={false} (non-modal) block but renders a modal sheet; move it to the "BottomSheet removed while open" suite in BottomSheetCloseFocus.test.tsx, or add hasScrim={false}.
  • Optional: three comments in BottomSheet.tsx (lines 462, 567 and 622) restate names or misplace where the target is read; the open inline suggestions give shorter wording.
Claims and files checked
  • Changeset: the sheet writes its focus return target at commit and no longer copies its stack view during render: verified (BottomSheet.tsx:464, :567-569).
  • Changeset and description: a sheet removed after a discarded render returns focus to its committed finalFocusRef: verified (the shared hook's commit-time write at BottomSheet.tsx:464, and the test sequence at BottomSheet.test.tsx:980).
  • Both refs were written during render and are read only in callbacks and effects: verified (base BottomSheet.tsx:461-462, :566; head reads at :581, :594, :623-624, :643, :759, :780, :898-899).
  • The new test fails on the base branch and passes at this head: verified from the code (the base writes the discarded target during render; the head writes only at commit); not run here.
  • The target is written at commit, before every layout effect: verified (useCommittedRef.ts:35-38 writes in an insertion effect).
  • The stack reads the target when it closes or removes the sheet, in a layout-effect cleanup or later: verified (the removal cleanup at BottomSheet.tsx:687; the registry's seal at bottomSheetStack.ts:429 runs from a passive effect or a microtask).
  • The registry's callback and the removal repair read the target through one function: verified (BottomSheet.tsx:623-624, :649, :687).
  • The stack view changes only in the registry's onView, so the render-time copy only repeated that write: verified (BottomSheet.tsx:651-652).
  • The governing rules (no ref writes during render, latest-value refs through the shared hook, no rendered value from a ref): verified against the current runtime architecture record.
  • No API, visual or default change: verified from the code (only the unexported standalone sheet's body changes, and no render reads either ref).
  • Existing tests, typecheck, lint, formatting and browser specs pass: consistent with the code; not rerun here, and CI has not run on this head.
  • Each merge of the base branch is conflict-free, the second brings the shared hook, the third brings changes to four files the sheet reaches, the last brings the animator change, and the change's own diff stays the same: verified from the history.
  • One defect, no tagalong, no hidden API or interaction change, no internal context: verified.
  • .changeset/bottom-sheet-return-target-at-commit.md: clear.
  • packages/core/src/BottomSheet/BottomSheet.test.tsx: test placement note.
  • packages/core/src/BottomSheet/BottomSheet.tsx: comment wording notes.

Automated review (maintainer link)

@vjeux
vjeux marked this pull request as ready for review November 12, 2026 17:11
github-actions Bot added a commit that referenced this pull request Nov 12, 2026
@cixzhang
cixzhang merged commit 1bb7828 into meta:main Nov 12, 2026
130 checks passed
@vjeux
vjeux deleted the fix/bottom-sheet-return-target-at-commit branch November 12, 2026 17:52

This branch was successfully deployed

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

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) 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