Skip to content

fix(BottomSheet, Dialog): returning focus after an overlay closes no longer scrolls the page - #10022

Merged
imdreamrunner merged 1 commit into
mainfrom
fix/overlay-focus-return-keeps-the-page
Nov 5, 2026
Merged

imdreamrunner merged 1 commit into
mainfrom
fix/overlay-focus-return-keeps-the-page

Conversation

@imdreamrunner

@imdreamrunner imdreamrunner commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

User impact

On Safari and iOS, a page that holds a BottomSheet or Dialog trigger inside a focusable region (a tabindex="-1" main landmark, as app routers and skip links set up) jumps when the sheet or dialog closes from a tap: Apply, Save, Done, the scrim or backdrop, or a swipe. The page scrolls so that the region's top edge meets the top of the viewport, 63 px on the docsite's component pages when the first example is open, 192 px when the page was not scrolled at all. Escape closes do not jump, and Chromium does not jump. The fix keeps the page where it was for every close path.

Problem and solution fit

  1. Why does the page scroll? WebKit does not focus a tapped button; it focuses the button's nearest focusable ancestor. BottomSheet and Dialog record document.activeElement as the opener when the caller passes no finalFocusRef, so the recorded opener is the tall region. On a pointer close, focusForLastInput(opener) blurs and refocuses the opener with focusVisible: false to drop the ring WebKit's own restore drew, and the refocus has no preventScroll, so WebKit schedules a scroll-to-focused-element for the next rendering update. The region's top edge is below the viewport's top edge, so the reveal scrolls the page to it.
  2. Why now for BottomSheet? The scroll lock used to pin <body> and scroll back to the saved offset on release. That window.scrollTo cancelled WebKit's scheduled reveal. The lock now holds the page with overflow: hidden and restores nothing, so the reveal runs. Dialog's return runs after its lock releases, so Dialog had the same jump before that change.
  3. Why does it matter? The person loses their place on the page every time they close a sheet or dialog on an iPhone, and the sheet presentation is now the default for Tokenizer, PowerSearch, Selector and Popover on a phone.

The fix is one rule at the one place that returns focus: returnFocusForLastInput(opener) in utils/focusReturn.ts focuses the opener with preventScroll: true and the ring the last input calls for. BottomSheet's close, the stack's "Close all" return, Dialog's close, DropdownMenu's close (which already passed preventScroll) and the return on removal use it. The opener was on screen when the overlay opened and the page has not moved since, so the return has nothing to reveal.

Expected behavior and authority

family:overlay-dismissal FR9 owns the focus return of every overlay member; this change adds to it that the return does not scroll the page or any scroller to the return target, and the reason WebKit needs the rule. component:Dialog FR4 / AR2 and component:BottomSheet FR9 / FR11 keep their return targets.

Smallest restoration

One new internal helper, five call sites, two browser specs, the unit expectations that pin the focus options, one record sentence, one changeset. No public API, no visual change, no new default. Lightbox and MobileNav call focus() once on an opener the native close already focused, which moves nothing, so they do not jump and are not changed.

Evidence

Per-frame measurement (viewport 390×844, a tabindex="-1" region whose top is at 192 px holds the trigger, page scrolled to 129; the value is the page's scroll offset after the close):

Build Engine Apply / Save Scrim Swipe Escape
before the scroll-lock change (body pinned) WebKit 129 129 129 129
main (lock holds overflow: hidden) WebKit 192 192 192 129
main Chromium 129 129 129 129
main, page not scrolled (offset 0) WebKit 192
this change WebKit 129 129 129 129
this change Chromium 129 129 129 129

Dialog (core-dialog--default, same page): main WebKit Cancel 192, backdrop 192, Escape 129, before and after the scroll-lock change alike; this change 129 on every path. The docsite's BottomSheet page at phone width in WebKit: main 129 → 192 on Apply, scrim and swipe; with this change 129 on all four paths.

  • Reproduction before the change: BottomSheetCloseScroll.a11y.browser.spec.ts and DialogCloseScroll.a11y.browser.spec.ts focus the region with preventScroll after the scroll and open with a programmatic click, so the region is the recorded opener in every engine, as a tap leaves it in Safari. Against a Storybook built from main, the four button and backdrop cases fail in WebKit ("Expected: 129, Received: 192") and the two Escape cases pass; all six pass in Chromium, whose reveal does not move the page in this geometry.
  • Result after the change: all six cases pass in WebKit and in Chromium; the sheet and the dialog still return focus to the region in both engines.
  • Representative unchanged path: Escape closes, Chromium closes, the Tokenizer and PowerSearch sheets (they return focus to their own trigger through finalFocusRef), and the focus ring policy: a pointer close still asks for no ring, a keyboard close leaves the ring to the browser.
  • Regression test or other mutation-sensitive proof: the two browser specs above; focusReturn.test.ts pins returnFocusForLastInput to preventScroll: true for both modalities; the BottomSheet and Dialog unit tests pin the focus options of every return.

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/BottomSheet packages/core/src/Dialog packages/core/src/DropdownMenu packages/core/src/utils/focusReturn.test.ts — 1008 passed.
  • pnpm exec playwright test BottomSheetCloseScroll DialogCloseScroll against a built Storybook, Chromium and WebKit projects — 12 passed with the change; against a main build, the 4 WebKit button and backdrop cases fail with the 192 px offset and the 6 Chromium cases pass.
  • pnpm -F @astryxdesign/core typecheck, pnpm check:knowledge, pnpm check:changesets, pnpm review:pre — clean.
  • Real-browser states: Storybook core-bottomsheet--showcase and core-dialog--default inside a tall tabindex="-1" region on a scrolled page; the docsite's BottomSheet page at 390 px with touch emulation.

Writing rubric results

R1 narration: the FR9 sentences state what the return does; no proposal or history.
R2 research: none in the record; the measurements live in this pull request.
R3 evidence: the new evidence row names the specs and the states, not a change.
R4 rejected alternatives: none added.
R5 change references: none.
R6 open questions: none.

@vercel

vercel Bot commented Nov 5, 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 5, 2026 1:33am UTC

Request Review

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

Copy link
Copy Markdown
Contributor Author

/approve-spec 647f3c5

@astracat-bot

astracat-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 5, 2026, 1:45 AM UTC

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 9e76574

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
CommandPalette 58.3 kB (+34 B) · 12.3 kB CSS (0) — — · — · —
DateRangeInput 75.7 kB (+33 B) · 11.2 kB CSS (0) — — · — · —
AlertDialog 89.9 kB (+31 B) · 13.1 kB CSS (0) — — · — · —
64 more components
Component Size LoC Visual · A11y · RTL
lab/Comments 269.8 kB (+30 B) · 2.5 kB CSS (0) — — · — · —
MoreMenu 77.6 kB (+29 B) · 16.1 kB CSS (0) — — · — · —
Pagination 92.4 kB (+27 B) · 16.5 kB CSS (0) — — · — · —
Markdown 152.3 kB (+26 B) · 18.9 kB CSS (0) — — · — · —
ContextMenuItem 23.8 kB (+25 B) · 6.0 kB CSS (0) — — · — · —
lab/TourStep 61.1 kB (+25 B) · 590 B CSS (0) — — · — · —
Selector 82.3 kB (+24 B) · 16.1 kB CSS (0) — — · — · —
Tokenizer 73.8 kB (+24 B) · 17.1 kB CSS (0) — — · — · —
lab/CommentComposer 108.6 kB (+24 B) · 1.9 kB CSS (0) — — · — · —
lab/Schedule 63.5 kB (+24 B) · 3.9 kB CSS (0) — — · — · —
Table 91.2 kB (+22 B) · 16.7 kB CSS (0) — — · — · —
DateTimeInput 176.8 kB (+21 B) · 17.2 kB CSS (0) — — · — · —
DateInput 154.9 kB (+20 B) · 15.5 kB CSS (0) — — · — · —
Drawer 41.0 kB (+20 B) · 8.2 kB CSS (0) — — · — · —
MultiSelector 79.5 kB (+20 B) · 15.5 kB CSS (0) — — · — · —
lab/ChatReactionBar 69.4 kB (+20 B) · 1.1 kB CSS (0) — — · — · —
TableCell 83.0 kB (+19 B) · 16.5 kB CSS (0) — — · — · —
lab/ChatEmojiPicker 68.9 kB (+19 B) · 773 B CSS (0) — — · — · —
Lightbox 54.8 kB (+18 B) · 9.1 kB CSS (0) — — · — · —
richtext/RichTextEditorToolbar 232.9 kB (+17 B) · 454 B CSS (0) — — · — · —
DropdownMenu 77.4 kB (+16 B) · 16.1 kB CSS (0) — ✓ · ✓ · ✓
TableHeaderCell 82.6 kB (+16 B) · 16.2 kB CSS (0) — — · — · —
TimeInput 140.4 kB (+16 B) · 14.5 kB CSS (0) — — · — · —
BottomSheet 56.3 kB (+15 B) · 7.5 kB CSS (0) — ✓ · ✓ · —
ContextMenu 81.8 kB (+15 B) · 16.0 kB CSS (0) — — · — · —
Dialog 22.0 kB (+14 B) · 6.7 kB CSS (0) — ✓ · ✓ · —
PowerSearch 249.2 kB (+14 B) · 22.3 kB CSS (0) — — · — · —
Popover 55.1 kB (+13 B) · 12.8 kB CSS (0) — — · — · —
lab/TransferListSelector 86.4 kB (+12 B) · 1.9 kB CSS (0) — — · — · —
BottomSheetSwitcher 17.4 kB (+11 B) · 3.4 kB CSS (0) — ✓ · ✓ · —
lab/CommentThread 264.6 kB (+9 B) · 2.4 kB CSS (0) — — · — · —
ChatComposerTokenElement 61.3 kB (+2 B) · 10.0 kB CSS (0) — — · — · —
ComplexSelector 57.6 kB (+1 B) · 10.4 kB CSS (0) — — · — · —
TopNavMenu 60.9 kB (+1 B) · 9.9 kB CSS (0) — — · — · —
AvatarGroup 37.7 kB (0) · 5.5 kB CSS (0) — — · — · —
BaseTypeahead 57.1 kB (0) · 9.6 kB CSS (0) — — · — · —
BreadcrumbItem 71.1 kB (0) · 11.2 kB CSS (0) — — · — · —
ButtonGroup 9.1 kB (0) · 3.4 kB CSS (0) — — · — · —
ChatComposer 63.3 kB (0) · 10.5 kB CSS (0) — — · — · —
ChatComposerInput 61.3 kB (0) · 10.0 kB CSS (0) — — · — · —
Collapsible 24.6 kB (0) · 4.5 kB CSS (0) — — · — · —
DialogHeader 49.6 kB (0) · 10.2 kB CSS (0) — ✓ · ✓ · ✓
DropdownMenuCheckboxItem 20.7 kB (0) · 6.3 kB CSS (0) — ✓ · ✓ · ✓
DropdownMenuDivider 5.2 kB (0) · 548 B CSS (0) — ✓ · ✓ · —
DropdownMenuGroup 6.3 kB (0) · 3.4 kB CSS (0) — ✓ · ✓ · —
DropdownMenuItem 19.9 kB (0) · 5.9 kB CSS (0) — ✓ · ✓ · —
DropdownMenuRadioGroup 6.3 kB (0) · 3.3 kB CSS (0) — ✓ · ✓ · —
DropdownMenuRadioItem 20.7 kB (0) · 6.3 kB CSS (0) — ✓ · ✓ · —
DropdownMenuSubMenu 59.4 kB (0) · 9.2 kB CSS (0) — ✓ · ✓ · ✓
NavHeadingMenu 15.5 kB (0) · 3.5 kB CSS (0) — — · — · —
SegmentedControl 83.8 kB (0) · 6.7 kB CSS (0) — — · — · —
SideNav 75.7 kB (0) · 10.1 kB CSS (0) — — · — · —
SideNavHeading 61.5 kB (0) · 10.0 kB CSS (0) — — · — · —
SideNavItem 56.6 kB (0) · 9.8 kB CSS (0) — — · — · —
TableSelectionToolbar 56.3 kB (0) · 11.8 kB CSS (0) — — · — · —
TabList 35.8 kB (0) · 6.3 kB CSS (0) — — · — · —
TabMenu 58.2 kB (0) · 9.8 kB CSS (0) — — · — · —
Toolbar 35.2 kB (0) · 7.9 kB CSS (0) — — · — · —
TopNavHeading 60.9 kB (0) · 9.8 kB CSS (0) — — · — · —
TopNavMegaMenu 56.1 kB (0) · 10.6 kB CSS (0) — — · — · —
TreeList 40.8 kB (0) · 5.3 kB CSS (0) — — · — · —
Typeahead 65.0 kB (0) · 11.2 kB CSS (0) — — · — · —
lab/CommentPin 48.4 kB (0) · 1.2 kB CSS (0) — — · — · —
LayerProvider 64.4 kB (−1 B) · 9.4 kB CSS (0) — — · — · —

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

API changes: none in this pull request

Committed API report behind this build: cli 9 rows, core 3 rows; run pnpm api:report.

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.

Changes requested. The fix itself works: in WebKit, with focus on the main region when the trigger is activated, the page stays at 129 px on this branch where main jumps to 192 px, and focus still returns to the opener. Three small corrections are needed before it lands.

  • The new BottomSheetCloseScroll and DialogCloseScroll specs pass unchanged against main in Chromium and Playwright's Linux WebKit, because those engines focus the clicked button. After the scroll, focus the region with main.focus({preventScroll: true}), and open with await opener.evaluate(node => (node as HTMLElement).click()) instead of opener.click(); the button and backdrop cases then fail on main (Expected 129, Received 192) and pass here.
  • The changeset lists DropdownMenu as fixed, but its close already passed preventScroll: true before this change and its sheet returns focus to its own trigger, so the entry should name only BottomSheet and Dialog.
  • In docs/families/overlay-dismissal.md, Open questions still says "FR9 fixes only the document focus returns in" and AV1 says the same, which now contradicts FR9's new no-scroll sentence, so both lines need the addition.

One non-blocking suggestion on the returnFocusForLastInput comment is inline.

[Automated review]

}

async function openSheet(page: Page, opener: Locator): Promise<Locator> {
await opener.click();

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.

In Chromium and Playwright's Linux WebKit this click focuses the trigger itself, so the sheet records the trigger as its opener and nothing scrolls on main: unchanged, all three cases pass against a main Storybook. Put focus where Safari puts it, then activate the trigger without moving focus:

// in mountScrolled, after window.scrollTo(0, scrollOffset):
main.focus({preventScroll: true});

// here:
await opener.evaluate(node => (node as HTMLElement).click());

With that, the Apply and scrim cases fail on main in WebKit (Expected: 129, Received: 192) and pass on this branch.

}

async function openDialog(page: Page, opener: Locator): Promise<Locator> {
await opener.click();

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.

Same as the BottomSheet spec: this click focuses the trigger in Chromium and Linux WebKit, so the spec passes on main. Focus main with preventScroll: true after the scroll and open with await opener.evaluate(node => (node as HTMLElement).click());; the Cancel and backdrop cases then fail on main (Expected: 129, Received: 192) and pass here.

'@astryxdesign/core': patch
---

[fix] BottomSheet, Dialog and DropdownMenu: the page no longer jumps after a sheet, a dialog or a menu closes in Safari and on iOS. Returning focus to the control that opened the overlay asks the browser not to scroll. WebKit focuses the nearest focusable ancestor of a tapped button, such as an app's `tabindex="-1"` main region, and records it as the opener; the focus return then scrolled the page to that region's top edge. The focus ring still follows the last input.

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.

DropdownMenu's close already passed preventScroll: true before this change, and its sheet returns focus to its own trigger through finalFocusRef, so nothing changes for DropdownMenu users. Suggest starting the entry with: "[fix] BottomSheet and Dialog: the page no longer jumps after a sheet or a dialog closes in Safari and on iOS."

Comment thread docs/families/overlay-dismissal.md Outdated
target, in the opener's document, with the ring the last input in that
document calls for (`architecture:interaction-modality` INV1); a BottomSheet,
BottomSheetSwitcher, or Lightbox does the same in a Picture-in-Picture window.
The return does not scroll the page or any scroller to the return target,

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 this sentence FR9 also owns scrolling during the return, but Open questions (line 344) still says "FR9 fixes only the document focus returns in" and AV1 (line 236) says the same. Please add "and that the return does not scroll" to both.

Comment thread packages/core/src/utils/focusReturn.ts Outdated
/**
* Returns focus to `opener` after an overlay closes, with the ring the last
* input calls for. The opener was on screen when the overlay opened, and the
* page has not scrolled since, so the move reveals nothing. `preventScroll`

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: "the page has not scrolled since" does not hold for a non-modal sheet, which has no scroll lock, so the page can scroll while it is open. Suggest keeping only the reason: "Return focus without a scroll. WebKit records the nearest focusable ancestor of a tapped button as the opener, such as a tabindex="-1" main region. A reveal of that region scrolls the page to its top edge."

…longer scrolls the page

On Safari and iOS the page jumped after a sheet or dialog closed from a
tap when its trigger sat inside a focusable region (a `tabindex="-1"`
main landmark). WebKit focuses the nearest focusable ancestor of a tapped
button, so the recorded opener was that region, and the pointer close's
blur-and-refocus of the opener, with no `preventScroll`, made WebKit
reveal the region: the page scrolled to its top edge. The scroll lock's
old `window.scrollTo` on release cancelled that reveal for BottomSheet;
Dialog returns focus after its lock releases, so it jumped before too.

`returnFocusForLastInput(opener)` returns focus with `preventScroll:
true` and the ring the last input calls for. BottomSheet's close, the
stack's "Close all" return, Dialog's close, DropdownMenu's close and the
return on removal use it. The opener was on screen when the overlay
opened and the page has not moved since, so the return reveals nothing.
Lightbox and MobileNav focus an opener the native close already focused,
which moves nothing, and are unchanged.

`family:overlay-dismissal` FR9 states that the return does not scroll
the page or any scroller to the return target. Two browser specs, in
Chromium and WebKit, close a BottomSheet and a Dialog whose trigger sits
in a tall focusable region of a scrolled page by a control inside, the
backdrop and Escape, and assert the page's offset and the trigger's
place on screen; the unit tests pin the focus options of every return.
@imdreamrunner
imdreamrunner force-pushed the fix/overlay-focus-return-keeps-the-page branch from 647f3c5 to 9e76574 Compare November 5, 2026 01:24
@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval label Nov 5, 2026
@imdreamrunner

Copy link
Copy Markdown
Contributor Author

/approve-spec 9e76574

@github-actions github-actions Bot removed the needs:spec-owner-review Current knowledge records await owner approval label Nov 5, 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; CI passed on this head.

In WebKit, with the trigger inside a focusable tabindex="-1" region of a scrolled page, closing a BottomSheet with Apply or the scrim, or a Dialog with Cancel or the backdrop, moved the page from 129 to 192 px on main and now leaves it at 129, with focus still returning to the opener. The new BottomSheetCloseScroll and DialogCloseScroll specs fail on main in WebKit and pass here, Chromium is unchanged, and the earlier comments on the specs, the changeset and the record are all addressed.

[Automated review]

@imdreamrunner
imdreamrunner merged commit 5db61cc into main Nov 5, 2026
66 checks passed
@github-actions
github-actions Bot deleted the fix/overlay-focus-return-keeps-the-page branch November 5, 2026 06:03

This branch was successfully deployed

1 active deployment
Preview — 9e76574b Deployed Nov 5, 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