Skip to content

fix(DropdownMenu): load the touch bottom sheet only when a menu shows it - #7709

Merged
josephfarina merged 3 commits into
mainfrom
josephfarina/dropdown-lazy-touch-sheet
Oct 20, 2026
Merged

josephfarina merged 3 commits into
mainfrom
josephfarina/dropdown-lazy-touch-sheet

Conversation

@josephfarina

@josephfarina josephfarina commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

User impact

Every page that renders a DropdownMenu downloads the touch bottom sheet (BottomSheet, its drag gestures, mobile-keyboard handling, and the touch action list), even when the menu only ever opens as an anchored popover. On desktop that code never runs. In a bundle of DropdownMenu alone, it is about a third of the component's code.

Problem and solution fit

DropdownMenu.tsx imported MenuBottomSheet and MenuBottomSheetActionList statically, so bundlers had to include them wherever the dropdown is imported. The sheet only renders when the resolved presentation is bottom-sheet (explicit, or adaptive on a compact touch screen). Loading both with React.lazy inside a Suspense boundary keeps them out of popover-only pages. ContextMenu already loads the same MenuBottomSheet this way.

Expected behavior and authority

No visible behavior change: a server-rendered bottom-sheet menu adds its closed (invisible) sheet markup right after hydration instead of in the server HTML. The DropdownMenu contract (popover and bottom-sheet presentations, adaptive switching, focus rules) is unchanged. The trigger still renders eagerly. The sheet starts loading when the trigger mounts; a keyboard open that arrives before it loads still focuses the first action once the rows mount.

Smallest restoration

  • New internal lazyMenuBottomSheet.ts: MenuBottomSheet and MenuBottomSheetActionList wrappers with the same props, each a React.lazy load inside <Suspense fallback={null}>.
  • The wrappers render nothing on the server and in the hydrating render (useIsHydrating()), so server markup never holds a half-loaded sheet (AST-013 FR9); the closed sheet has no visible markup.
  • DropdownMenu.tsx: only its import changes, to the wrappers. Its JSX is untouched.
  • Tests: a behavioral test asserts a popover menu never requests the sheet modules. A small test helper opens one sheet in beforeAll so later tests render it synchronously (the same pattern the Item swipe tests use).

Evidence

  • Before: esbuild with code splitting, DropdownMenu entry from the built dist, React and StyleX external: popover-only code is 246 KB minified / 78 KB gzipped.
  • After: popover-only code is 166 KB minified / 54 KB gzipped. The sheet is split into separate chunks that load only for the bottom-sheet presentation.
  • Unchanged path: all bottom-sheet, adaptive, drill-in, RTL, and keyboard-focus tests pass unchanged apart from the warm-up.
  • Regression proof: the source test fails if either module is imported statically again.
  • Keyboard open before the sheet loads: DropdownMenuLazySheet.test.tsx holds both sheet modules until after Enter, with no warm-up; the first action gets focus once they load (fails on a255dd3, passes now).
  • Server rendering: DropdownMenu.ssr.test.tsx renders and hydrates a bottom-sheet menu three times, each in a fresh module graph, with no recoverable error (fails without the hydration guard).
  • On-demand proof: DropdownMenuLazySheet.test.tsx asserts a popover menu never requests either sheet module, and its import times out if any module the dropdown loads imports the sheet eagerly (verified by adding an eager import).

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

  • vitest run --project ui on DropdownMenu, ContextMenu, MoreMenu, and the menu a11y suites: 360 passed.
  • pnpm -F @astryxdesign/core build and typecheck: clean. eslint and prettier: clean.

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

vercel Bot commented Oct 20, 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 20, 2026 3:09am UTC

Request Review

@astracat-bot

astracat-bot Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 20, 2026, 3:40 AM UTC

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR

🧪 Sandbox Preview

View Sandbox for this PR

Modified Components

MoreMenu (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 87 -
Complexity N/A Low (4) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.9KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

40 of 482 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 754,077
Selector Indicator Space Bottom Sheet Wide Start neutral light 740,188
Chat Full AI Chat probe light 230,823
Chat Full AI Chat probe dark 229,805
RichTextEditor Markdown parity: side by side probe dark 94,866
RichTextEditor Markdown parity: side by side probe light 89,762
Collapsible Dividers probe dark 66,445
Collapsible Dividers probe light 66,445
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
ProgressBar With Target Mark probe dark 7,895
ProgressBar With Target Mark probe light 7,895
Markdown Default neutral dark 7,573
Markdown Default neutral light 7,566

and 20 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.

Requesting changes: the bundle saving holds up (a popover-only DropdownMenu loads about 24 KB less gzipped code) and loaded bottom sheets behave as before, but opening a sheet from the keyboard before its on-demand code arrives now leaves focus on the sheet panel instead of the first action.

  • Run the first-row focus when the rows mount instead of once when isOpen flips, load the sheet and its action list in one request, and add a test that opens the sheet before the lazy modules resolve (the beforeAll warm-up hides this path today).
  • Add your handle on its own line to .changeset/dropdown-menu-lazy-touch-sheet.md, since check:changesets fails the Lint workflow.
  • Replace the import-string test with an assertion on the bundle output, or drop it, because the testing architecture asks tests not to rely solely on import structure.

[Automated review]

import {
MenuBottomSheet,
MenuBottomSheetActionList,
} from './lazyMenuBottomSheet';

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.

Loading the sheet lazily leaves the keyboard-open focus effect (lines 761-771) racing the import: if Enter lands before MenuBottomSheet resolves, the effect's single animation frame finds no rows, nothing re-runs it when they mount, and focus stays on the sheet panel. Running the first-row focus when the rows mount (and loading both modules in one request) keeps the shipped behavior.

})),
);

const LazyMenuBottomSheetActionList = lazy(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.

This boundary renders inside the lazily loaded sheet, so its import starts only after the MenuBottomSheet chunk has loaded and rendered: two sequential requests before the rows exist. Starting both imports together, or loading the whole sheet subtree from one module, removes the second wait.


// The touch sheet is loaded lazily; resolve it once so sheets below render
// synchronously.
beforeAll(warmMenuBottomSheet, 20000);

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 both lazy components resolved here, no test opens the sheet before they load; the steps of 'focuses the first action when a bottom sheet opens from the keyboard' fail on this head without this warm-up. A cold-path case in its own file would cover that partition.

expect(source).not.toContain("import {Section} from '../Section'");
});

it('loads the touch sheet only on demand', () => {

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 asserts import strings, which component-test-sufficiency INV5 asks tests not to rely on when the outcome can be observed directly; a static import of BottomSheet internals from another eager DropdownMenu module would restore the bytes while this still passes. An assertion on the bundle graph of the DropdownMenu entry would prove the promise.


[fix] DropdownMenu: load the touch bottom sheet only when a menu shows it

`DropdownMenu` imported its touch sheet (BottomSheet, its gestures, and the touch action list) up front, so every page with a dropdown shipped it, even when the menu only ever opens as an anchored popover. The sheet now loads on demand, the way `ContextMenu` already loads it. A popover-only dropdown's own code drops by about a third (about 23 KB gzipped). Bottom-sheet and adaptive menus behave as before; the sheet mounts with the trigger, so it has loaded before the first tap.

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.

check:changesets fails the Lint workflow here: the body needs a contributor handle on its own line, for example @josephfarina.

…beats the sheet load

The touch sheet loads on demand, so a keyboard open can commit before its
rows exist. Wait for the rows, then focus the first action. Add a test that
holds the sheet modules until after Enter, with no warm-up, and credit the
changeset.

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

Requesting changes: the bundle saving holds (a popover-only DropdownMenu loads about 21 KB less gzipped code), loaded menus look and behave as before, and a keyboard open that beats the sheet load now focuses the first action. Two things still need a change:

  • With renderToString, the first two server renders in each server process send the closed sheet incompletely and hydration reports a recoverable error, which AST-013 FR9 rules out; skipping the lazy sheet while useIsHydrating() returns true keeps it out of the server and hydrating renders.
  • The import-string test in DropdownMenu.test.tsx relies on source structure, which component-test-sufficiency INV5 rules out, and it still passes when another eager module imports the sheet again, so please drop it or assert on the bundled entry instead.

[Automated review]

})),
);

export function MenuBottomSheet(

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 wrapper suspends during a server render and during the hydrating render, because DropdownMenuBottomSheet renders the closed sheet beside the trigger. With renderToString, the first two renders in each server process send no sheet and then no rows, and each hydrates with "Switched to client rendering because the server rendering aborted…", which AST-013 FR9 rules out. Skipping the lazy sheet while useIsHydrating() returns true keeps it out of both renders, and the sheet still starts loading right after hydration.

Comment on lines +379 to +395
it('loads the touch sheet only on demand', () => {
const source = readFileSync(
'packages/core/src/DropdownMenu/DropdownMenu.tsx',
'utf8',
);
const loader = readFileSync(
'packages/core/src/DropdownMenu/lazyMenuBottomSheet.ts',
'utf8',
);

// Pointer-only pages must not pull the touch sheet into their bundle.
expect(source).toContain("from './lazyMenuBottomSheet'");
expect(source).not.toMatch(/from '\.\/MenuBottomSheet(ActionList)?'/);
expect(loader).toContain("import('./MenuBottomSheet')");
expect(loader).toContain("import('./MenuBottomSheetActionList')");
expect(loader).not.toMatch(/^import \{[^}]*\} from '\.\/MenuBottomSheet/m);
});

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 checks import strings, which component-test-sufficiency INV5 rules out when the outcome can be observed directly, and it misses the regression it is meant to catch: with import './MenuBottomSheet' added to renderDropdownItems.tsx, the sheet's modules return to the eager bundle while this test still passes. Dropping it (INV11 asks for no new test for a private wrapper) or asserting on the bundled entry's static graph would fix that.

…ng renders

A lazy sheet that has not loaded yet left server markup incomplete in the
first renders of a server process, and hydration then fell back to client
rendering. The sheet wrappers render nothing while useIsHydrating() is true;
the closed sheet has no visible markup, so it first renders right after
hydration. Add an SSR test over fresh module graphs.

Replace the source-structure import test with a behavioral one: opening a
popover menu never requests either sheet module, and importing the dropdown
times out if any module it loads imports the sheet eagerly.

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

This looks ready to merge: popover-only DropdownMenu pages now load about 22 KB less gzipped code, menus look and behave the same once the sheet has loaded, and server-rendered bottom-sheet menus hydrate cleanly. The hydration guard and the new cold-path, SSR and module-load tests resolve the earlier blocking feedback, and each of those tests fails when its fix is reverted. Nothing else is needed before merging; as an optional follow-up, starting both sheet imports together would halve the wait when someone opens a sheet before its code has arrived, since the action list's chunk is requested only after the sheet's.

[Automated review]

@josephfarina
josephfarina merged commit e868278 into main Oct 20, 2026
56 checks passed
@github-actions
github-actions Bot deleted the josephfarina/dropdown-lazy-touch-sheet branch October 20, 2026 06:22
imdreamrunner pushed a commit that referenced this pull request Oct 20, 2026
… cases (#7758)

ContextMenu loads its touch sheet with React.lazy. Until #7709 its import resolved at once, because DropdownMenu imported the sheet statically; now nothing in this suite loads it first, so the first case that opens a ContextMenu sheet pays the sheet's cold import inside a 1 s waitFor and fails on a slow machine. The suite now warms the sheet up front with the helper #7709 added for the DropdownMenu and MoreMenu suites.
cixzhang pushed a commit that referenced this pull request Oct 30, 2026
…er an unhandled rejection (#8478)

## User impact

A bottom-sheet DropdownMenu preloads its action list at rest. When that load fails (a dropped or flaky connection), the page gets an unhandled promise rejection in the console. The rejected load also stays cached, so every later open of the menu fails to show its rows until a reload. In this repository's CI, the same unhandled rejection fails the UI test shard that runs `DropdownMenu.ssr.test.tsx` while every test passes, as on #8353, #8229 and #8211.

## Problem and solution fit

- Why does it occur? `preloadMenuBottomSheetActionList` starts the load with `void loadActionList()`: nothing handles a rejection, and `actionListLoad ??=` keeps the rejected promise.
- Why does it harm the task? An unhandled rejection is reported as an error, and a cached rejection turns one network blip into a menu whose rows never appear.
- Why does that matter? The preload only exists to make the first open instant; it must never break the open. In tests, the SSR file can finish while its preload is still importing; the environment tears down mid-import and the rejection fails the run.

The fix handles the preload's failure and drops the failed load, so the next preload or open asks the loader again. The SSR test waits for the sheet modules before it ends.

## Expected behavior and authority

The sheet loads on demand (#7709) and preloads at rest so the first open shows its actions at once (#8392). A preload is an optimization: its failure surfaces no error and blocks no retry.

## Smallest restoration

- `loadActionList` clears its cached load when the import rejects.
- `preloadMenuBottomSheetActionList` handles the rejection.
- `DropdownMenu.ssr.test.tsx` awaits the two sheet modules after unmounting.

No API, rendering or timing change.

## Evidence

- Reproduction before the change: on main e9b3f3e, `DropdownMenu.ssr.test.tsx` run alone fails 5 of 6 runs with three `EnvironmentTeardownError`s each, and the new unit test records the unhandled rejection.
- Result after the change: the SSR file passes 8 of 8 runs alone, and the unit test passes.
- Representative unchanged path: all 10 DropdownMenu test files (312 tests) pass.
- Regression test or other mutation-sensitive proof: `lazyMenuBottomSheet.test.ts` fails the list's import once, then checks there is no unhandled rejection and that the next call imports it again. Keeping the rejected load cached fails it ("expected 1 to be 2").

## Scope

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

## Testing

- At 3962530: the changeset and comments promise only what holds everywhere (no unhandled rejection, no kept failure); the unit and SSR tests: 2 passed; changeset coverage: passes.
- At 8877d94 (adds the Changeset): `node scripts/release/changeset-coverage.mjs pr --base e9b3f3e` fails at 4d8e178 and passes here (`[fix]`, `@astryxdesign/core`); `pnpm check:changesets`: valid.
- `pnpm exec vitest run --project ui packages/core/src/DropdownMenu/lazyMenuBottomSheet.test.ts`: fails on main, passes here.
- `pnpm exec vitest run --project ui packages/core/src/DropdownMenu/DropdownMenu.ssr.test.tsx`, alone, 8 times: 8 of 8 (main: 1 of 6).
- `pnpm exec vitest run packages/core/src/DropdownMenu`: 10 files, 312 tests.
- `tsc -p packages/core/tsconfig.json --noEmit`, eslint and prettier on the changed files: clean.

This branch was successfully deployed

1 active deployment
Preview — d3a5a274 Deployed Oct 20, 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