Skip to content

perf(Toast): load the swipe's claim at idle or with the first toast - #9835

Closed
vjeux wants to merge 3 commits into
meta:mainfrom
vjeux:uikit/c25-toast-lazy-claim
Closed

vjeux wants to merge 3 commits into
meta:mainfrom
vjeux:uikit/c25-toast-lazy-claim

Conversation

@vjeux

@vjeux vjeux commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Primary intent

Load Toast's swipe claim, and the gesture host under it, when the page is idle after load, or with the first toast if sooner, not with every Toast or LayerProvider import. other.md: no template fits a size change with one behavior tradeoff. It carries #8047's two commits, rebased onto main, and stays a draft until maintainers pick it or eager loading.

User or maintainer impact

#8047 routes a toast's touch claim through the shared gesture host, and LayerProvider renders a ToastViewport, so every LayerProvider import, desktop-only apps included, gains about 24 KB gzip. Here no import carries it; idle time or the first toast loads it.

Tradeoff: a touch that starts on a toast before the code loads scrolls the page instead of swiping.

Problem and solution fit

  1. Why the cost? The claim needs the gesture host, its arena and a pan: 14 modules.
  2. Why in Layer? LayerProvider mounts the toast viewport, so Layer's import reaches Toast's.
  3. Why not drop it? The claim stays on the gesture host's one claim path; only its loading moves.
  • ToastSwipeClaim holds the pan; lazyToastSwipeClaim loads it like Popover's and DropdownMenu's sheets, with no new loader: through requestIdleCallback after the load event (the next task in Safari), or on the first toast's render.
  • A touch that starts before the pan attaches is ignored: the host holds only a touch it saw begin.

Authority

  • architecture:react-component-runtime INV13 and spec:AST-080 DEC-5 rule out a component lazy-loading touch code; the owners' recorded exception joins this draft once they decide.
  • architecture:component-style-authoring INV6: the size of a component's import.
  • component:Toast FR4 and spec:AST-094 FR19, as fix(Toast): a quick flick dismisses a toast of any height #8047 records them.

Change and evidence

Eager output, esbuild split build (packages external, gzip -9, entry plus static imports), on main 217fc4658d:

Import main Eager claim This PR
Toast 64,488 B 88,222 B 65,311 B
Layer 74,161 B 98,069 B 74,848 B

Claim chunk: about 23.5 KB gzip. Time until it arrives:

Start iOS 26.2 simulator, Safari Chromium, 4× CPU, Slow 4G
First toast's mount, before idle 13–15 ms 147–154 ms
Load event, no toast 171–208 ms about 5.5 s

Scope

  • This PR has one primary intent.
  • Independently removable deltas were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

Local gates at b1536f7bf1, three commits on main (217fc4658d). They ran at 74a60e78b7, whose code is the same; b1536f7bf1 rewords three comments.

  • pnpm install --frozen-lockfile, the core typecheck, the Storybook build, CI=true pnpm lint and the strict comment lint on the changed files (0 errors in them; pnpm lint also reports main's own WheelContrast.a11y.chromium.spec.ts import), Prettier, pnpm check:knowledge, pnpm check:changesets and pnpm review:pre (0 findings): clean.
  • pnpm test:ui: 790 files, 19,759 tests, all passing, useScrollLockImports.test.ts among them: no static import path from Drawer, Dialog, BottomSheet or Lightbox reaches the gesture host.
  • The whole node project: 879 of 882 files pass. The three that fail are CLI search files that time out under load on main too (_template-searcher, tree, global-local); they are tracked as known load-sensitive tests, and none reads Toast.
  • New: lazyToastSwipeClaim.idle.test.tsx. "loads once the page is idle, before any toast shows" fails without the idle load (load count 0). Three cases pin the scheduler: it waits for the load event, falls back to the next task without requestIdleCallback, and cancels.
  • lazyToastSwipeClaim.test.tsx: a finger that lands before the claim loads is not cancelled, does not drag and does not dismiss; once the module loads, the next touch swipes. lazyToastSwipeClaim.offline.test.tsx: a failed load keeps the toast and leaves the touch to the page.
  • scripts/touch-support-bundle.test.mjs (a split build's eager output of Toast and of Layer carries no gesture module) and scripts/core-single-bundle.test.mjs: 13 of 13.
  • Browser: the ToastEmptyViewport and ToastSwipeScroll interaction specs, Chromium 4 of 4 and WebKit 3 of 3 (the page-scroll case runs in Chromium only); ToastSafeArea with the toast outlet contracts, 11 of 11.
  • Timing, the table above. iOS 26.2 simulator over a local network, chunk timing from Resource Timing: 5 mount-only runs, 3 idle runs. Chromium headless with a 4× CPU throttle and Lighthouse's Slow 4G (150 ms, 1.6 Mbps): 3 runs each. With no toast, a build without the idle load never fetches the chunk, on both.
  • iOS Safari (iOS 26.2 simulator) on this branch's Storybook build: the chunk had arrived 1.1 s after navigation, before any toast. Three downward swipes dismissed three stacked bottom toasts with the page still at 600 px and each first move cancelled; an upward swipe scrolled the page to 730 px. After swipe, close-button and auto-hide exits, Safari's bottom toolbar keeps the page color.
  • Sizes: the table above.

Writing rubric results

  • R1 narration: in Toast.spec.md, FR4's first-move sentence, FR6's gutter sentences, the readiness count, the Current-state gap on Safari's toolbar, the three first-move tradeoffs and the verification rows, and spec:AST-094 FR19's toast exception and its FR18–FR22 row, state what the system does, in the present tense.
  • R2 research: none in the records.
  • R3 evidence: none in the records; this description carries it.
  • R4 rejected alternatives: none.
  • R5 change references: none added (no # numbers, /pull/ or /issues/ in the changed lines).
  • R6 open questions: none touched.

@vercel

vercel Bot commented Nov 3, 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 4, 2026 7:06am UTC

Request Review

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

astracat-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
❓ Needs maintainer input (for maintainers only) Nov 4, 2026, 7:26 AM UTC

@github-actions github-actions Bot added the needs:design-review Affects visuals — Design should review label Nov 3, 2026
@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

Current run: The current CI run concluded skipped. Current analysis is unavailable.


Generated by PR Enrichment workflow | View current CI run

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

Approval waits on an engineering owner's decision. spec:AST-080 DEC-5 and architecture:react-component-runtime INV13 rule out a component lazy-loading touch code, so keeping this approach needs an exception in those records and approval of this head's record changes (Toast FR4's first-move sentence and its three tradeoffs, the FR6 bottom gutter, and the fifth spec:AST-094 FR19 exception). An engineering owner records that decision by commenting /approve-spec <full head SHA> on this pull request, and the author may do so as an engineering owner. If the lazy load stays, these items also need fixing:

  • When the claim's chunk fails to load (offline, or after a deploy removes it), React.lazy throws while the first toast renders and unmounts everything up to the nearest error boundary, so resolve a failed load to a claim that renders nothing and add a test for it (lazyToastSwipeClaim.ts).
  • .changeset/toast-first-move-claim.md says importing Toast or LayerProvider "costs nothing more", but the eager output still grows by about 0.5 KB gzip and the single-bundle size grows by about 24 KB, so please correct the wording.
  • The first-move claim fixes a page scroll seen in iOS Safari, and this head changes when that claim attaches, so please add an iOS Safari run of this head (a downward swipe on a bottom toast over a scrolled page after the claim has loaded) because Chromium and Playwright WebKit runs do not stand in for Safari.

[Automated review]

Comment on lines +30 to +34
const LazyToastSwipeClaim = lazy(async () =>
loadToastSwipeClaim().then(module => ({
default: module.ToastSwipeClaim,
})),
);

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.

If this import() rejects (offline, or a deploy removed the chunk), React.lazy throws the rejection while the first toast renders, and everything up to the nearest error boundary unmounts. Toasts often report going offline, so this is the case most likely to hit it. Resolving a failed load to a claim that renders nothing keeps the toast working, with the page keeping the touch as it does before the load:

const LazyToastSwipeClaim = lazy(async () =>
  loadToastSwipeClaim().then(
    module => ({default: module.ToastSwipeClaim}),
    () => ({default: () => null}),
  ),
);

A test where the import rejects would pin it.

Comment thread .changeset/toast-first-move-claim.md Outdated
'@astryxdesign/core': patch
---

[fix] A touch on a toast is now decided by its first move. A first move toward the toast's edge belongs to the swipe and never scrolls the page; any other first move leaves the page its scroll. The shared gesture host makes that claim. Its code, about 24 KB gzip, loads when the first toast shows, so importing Toast or LayerProvider costs nothing more until then; a touch that starts before it loads stays the page's.

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.

"costs nothing more until then" overstates it: the description's own table shows the eager output growing by 620 B (Toast) and 497 B (Layer) gzip over main. Suggest "adds under 1 KB gzip until then".

vjeux added 3 commits November 3, 2026 22:23
A release toward the edge dismissed past max(40% of the card's height,
48 px), or as a flick only when it had also travelled 48 px. On any toast
up to 120 px tall the two floors are the same distance, so a flick could
never dismiss a one-line toast. The flick now needs no travel of its own
beyond the slop the swipe already passed.

The flick's speed was also timed with Date.now() in the handlers. Browsers
deliver touch and pointer moves on the next frame, so the handlers trail the
finger by up to two frames (measured in Chromium: a 33 ms flick handled over
55 ms), which halved the speed of a short flick. It is now timed by the input
events' own timeStamp.

A bottom toast keeps its 16 px gap above the bottom safe-area inset instead of
sitting on the inset's edge. Under a coarse pointer it clears at least 34 px
plus that gap: Safari reports the inset only with `viewport-fit=cover`, so a
page without it read 0 and left the toast in the home indicator's area. Toast's
record (FR6) states that gutter.

A touch on the card is decided by its first move: toward the edge, the swipe
owns the touch; any other first move leaves the page its scroll (Toast FR4).
The gesture host makes that claim: the card attaches a one-way pan whose new
`firstMoveDecides` option holds a touch from a first move its own way and
fails on any other. Toast's own listeners only draw the swipe, and end it once
the pan leaves the touch to the page.

Importing Toast or LayerProvider now brings in the shared gesture host:
about 24 KB gzip on its own (standalone estimate), and nothing more where
other touch components already load it.
iOS Safari colors its bottom toolbar from a fixed element at the bottom
edge. A swiped toast leaves through that edge inside the toast viewport,
and Safari kept the toast's color while the empty viewport still had a
box. The viewport now has no box while it holds no toast, and the
toolbar returns to the page color once the last toast leaves.

The Toast record lists the limit that remains: while other toasts
remain, a swiped toast's exit can tint the toolbar until the last toast
leaves.

The first-move changeset's size figures now read against current main.
The toast's touch claim runs through the shared gesture host. That put the
host in every Toast import, and LayerProvider renders a ToastViewport, so
every LayerProvider import carried it too: about 24 KB gzip.

The claim's pan now lives in ToastSwipeClaim, which lazyToastSwipeClaim
loads as Popover's and DropdownMenu's sheets do: when the page is idle after
its load event (requestIdleCallback, or the next task in Safari, which has
none), or on the first toast's render if that comes first. The pan attaches
once the card root is in the DOM. A touch that starts before it loads stays
the page's: the gesture host holds only a touch it saw begin. A module that
fails to load gives a claim that renders nothing, so the toast stays and the
page keeps the touch.

Eager output (esbuild split build, gzip -9), main / eager claim / this:
Toast 64,488 / 88,222 / 65,311 B; Layer 74,161 / 98,069 / 74,848 B. The
claim's chunk is about 23.5 KB gzip.

core-single-bundle.test.mjs now takes the app's own output by its entry
file: esbuild names an entry point on each chunk an import() starts, too, and
the new chunk changed their order.
@vjeux
vjeux force-pushed the uikit/c25-toast-lazy-claim branch from 9137383 to b1536f7 Compare November 4, 2026 06:59
@vjeux vjeux changed the title perf(Toast): load the swipe's claim with the first toast perf(Toast): load the swipe's claim at idle or with the first toast Nov 4, 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.

Approval waits on an engineering owner's decision. spec:AST-080 DEC-5 and architecture:react-component-runtime INV13 rule out a component lazy-loading its touch code, so keeping Toast's lazy swipe claim needs an exception in those records, and this head's record changes need approval: Toast FR4's first-move sentence and its three accepted tradeoffs, the FR6 bottom gutter, the toolbar gap and verification rows, and the fifth spec:AST-094 FR19 exception. An engineering owner records that decision by commenting /approve-spec <full head SHA> on this pull request, and the author may do so as an engineering owner.

  • No blocking defect remains: the failed-load fallback, the corrected changeset wording and the iOS Safari run address the earlier findings.
  • If the lazy load stays, the third tradeoff should also say that a custom-content toast with no control and no auto-hide cannot be dismissed by touch until the code loads.
  • Non-blocking: the lazyToastSwipeClaim.ts header can drop the sentences its JSDoc already says (see the inline note).

CI has not run on this head yet.

[Automated review]

* render, and `preloadToastSwipeClaimWhenIdle`, which loads it at idle
* @position Internal to Toast: the claim of its swipe
*
* The claim brings in the gesture host, so no import of Toast or Layer holds

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 (comment style): this header runs 15 lines, and its prose repeats the JSDoc of whenPageIdle and preloadToastSwipeClaimWhenIdle. Keeping only the constraint would read:

 * The claim brings in the gesture host, so no import of Toast or Layer holds
 * it. It renders nothing on the server, in the hydrating render, and when its
 * module fails to load. The toast then stays, and the page keeps the touch.

@vjeux

vjeux commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Closed: card 62 chose option C; see #10012.

This branch was successfully deployed

1 active deployment
Preview — b1536f7b Deployed Nov 4, 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. needs:design-review Affects visuals — Design should review needs:spec-owner-review Current knowledge records await owner approval

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant