Skip to content

[drawer] Drive swipe gestures natively to stop per-frame re-rasterization - #4980

Merged
atomiks merged 3 commits into
mui:masterfrom
atomiks:codex/drawer-swipe-native-touchmove
Jun 10, 2026
Merged

atomiks merged 3 commits into
mui:masterfrom
atomiks:codex/drawer-swipe-native-touchmove

Conversation

@atomiks

@atomiks atomiks commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4863

Problem

While a Drawer was open, every swipe touchmove re-rasterized the sheet's content instead of staying compositor-only. The touchmove bubbled into React's delegated touch system, and processing it there made Chrome invalidate and repaint the document and full-screen popup once per move (~2 paints per move). The translate was already a cheap composited transform — the repaints came from the event-dispatch path.

Fix

Drive the swipe from a native, capture-phase touchmove listener. Once the Drawer claims the gesture it calls stopPropagation() so the move never reaches React's delegated handlers, forwarding it via a typed moveFromNativeTouch hook method. Propagation is preserved for cases the Drawer must not hijack (native scroll, range inputs, pinch-zoom, text selection). getDragTransform also moves to translate3d. No consumer CSS or workarounds required.

Results

Heavy drawer stress benchmark (800-node payload, includes Vaul), headless Chromium, 20× CPU slowdown, paired same-session runs, medians:

Lib Total busy Paint Paint ev Avg move task
Base UI (before) 981 ms 341 ms 78 17.1 ms
Base UI (after) 631 ms 70 ms 6 8.9 ms
Material UI 723 ms 328 ms 79 14.4 ms
Silk 586 ms 80 ms 0 4.2 ms
Vaul 1121 ms 86 ms 6 10.2 ms

Base UI before → after: paint 341 → 70 ms (−79%), paint events 78 → 6, avg move task 17.1 → 8.9 ms (−48%), total busy −36%. The per-move content re-rasterization is gone; paint no longer scales with the heavy content.

Notes: numbers reflect main-thread work during the gesture. Total busy = all main-thread time in the swipe window; Avg move task = per-move input-handler latency — not comparable across libraries (Silk travels via native scroll, so it offloads the drag to the compositor and delivers fewer JS moves). Material UI's SwipeableDrawer keeps an 800-node paper mounted while closed, which inflates absolute numbers for every row equally, so treat cross-library absolutes loosely; the Base UI before/after delta is unaffected.

Before

Screenshot 2026-06-10 at 9 51 44 am

After

Screenshot 2026-06-10 at 9 52 25 am

Changes

  • Handle swipe moves in a native capture-phase touchmove listener and call stopPropagation() so they don't reach React's delegated handler. Native scroll, range inputs, pinch-zoom, and text selection still propagate.
  • Add moveFromNativeTouch to useSwipeDismiss so the native listener feeds the gesture without a synthetic React event.
  • Switch the swipe transform to translate3d.

@atomiks atomiks added component: drawer Changes related to the drawer component. performance mobile Targets mobile platform. labels Jun 8, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4980
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4980
    

commit: eb6dec5

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+77B(+0.02%) 🔺+22B(+0.01%)

Details of bundle changes

Performance

Total duration: 1,106.74 ms ▼-522.70 ms(-32.1%) | Renders: 50 (+0) | Paint: 1,689.40 ms ▼-739.28 ms(-30.4%)

Test Duration Renders
Tabs mount (200 instances) 213.81 ms ▼-111.17 ms(-34.2%) 4 (+0)
Slider mount (300 instances) 139.00 ms ▼-90.55 ms(-39.4%) 3 (+0)
Menu mount (300 instances) 116.25 ms ▼-67.45 ms(-36.7%) 2 (+0)
Scroll Area mount (300 instances) 84.81 ms ▼-45.00 ms(-34.7%) 3 (+0)
Select mount (200 instances) 129.28 ms ▼-44.87 ms(-25.8%) 3 (+0)

…and 6 more (+1 within noise) — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jun 8, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit eb6dec5
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a28a75443ceba00099b2c91
😎 Deploy Preview https://deploy-preview-4980--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the codex/drawer-swipe-native-touchmove branch 2 times, most recently from 979a995 to 0740b7c Compare June 8, 2026 03:00
@atomiks
atomiks force-pushed the codex/drawer-swipe-native-touchmove branch from 0740b7c to 53c5e5a Compare June 8, 2026 03:08
@atomiks
atomiks marked this pull request as ready for review June 8, 2026 03:25
@atomiks

atomiks commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Review (Claude Fable 5): base-ui#4980 — [drawer] Drive swipe gestures natively

This routes drawer swipe moves through the existing document-level capture-phase touchmove listener: once the drawer claims the gesture it calls stopPropagation() and forwards the event into useSwipeDismiss via the new moveNative method, so the move never reaches React's delegated touch system — which was what triggered ~2 paints per move. The drag transform also moves to translate3d, and the old React onTouchMove plumbing in DrawerViewport is removed.

Correctness — traced the forwarding path end to end:

  • The synthetic object passed to handleMove supplies exactly the five fields the move pipeline reads (touches, currentTarget, nativeEvent, defaultPrevented, timeStamp); getPrimaryPointerPosition and isTouchLikeEvent branch on 'touches' in event, so the native shape flows through the touch path identically to a React event. The defaultPrevented snapshot (vs. React's live getter) is only read on the non-touch branch, so freezing it is harmless.
  • currentTarget is the same element the React touch props were spread onto (viewportElement ?? popupElement), so the scrollable-boundary check in handleMoveCore is unchanged.
  • Propagation is preserved everywhere the drawer must not hijack the gesture: range inputs, two-finger pinch, text selection, cross-axis native scroll, and in-progress scrolling of inner containers. The old React-side guard (allowSwipe === false || (scrollTarget != null && !allowSwipe)) maps cleanly onto the new branches — forward only when scrollTarget is null/doc/body, or when allowSwipe === true.
  • One theoretical gap: the doc/body branch forwards without re-checking allowSwipe === false, where the old guard would have skipped. In practice it's unreachable — native touch events keep targeting the touchstart element, so a gesture that began on a range input never reaches that branch (and a touchstart on a range input nulls out touchState entirely).

Notes:

  1. Observable behavior change. User-attached React onTouchMove handlers inside the drawer no longer fire while the drawer owns the gesture (the new test asserts this). That's the point of the fix, but it's API-observable — worth a line in the changelog for anyone reading swipe moves themselves. onTouchStart/onTouchEnd still fire, so consumers see start → end with no moves in between.
  2. The "non-overflowing scroll container" branch stops propagation without forwarding to the swipe — consistent, since preventDefault already made the gesture inert there; just noting it's the one spot where propagation stops without the swipe consuming the move.
  3. The hook method is named moveNative and documented in terms of the mechanism (capture-phase consumers claiming the gesture) rather than the Drawer specifically, keeping the shared util component-agnostic — Toast also consumes it. Since the property key survives minification, the short name also saves a few bundle bytes.

Tests cover both the claimed path (consumer handler not called) and the preserved native-scroll path (handler called once), and the translate3d assertion tolerates browser serialization differences. Benchmark methodology in the description is appropriately honest about cross-library caveats.

LGTM — the notes above are observations, not blockers.

@atomiks
atomiks merged commit 5b51e39 into mui:master Jun 10, 2026
6 checks passed
@atomiks
atomiks deleted the codex/drawer-swipe-native-touchmove branch June 10, 2026 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: drawer Changes related to the drawer component. mobile Targets mobile platform. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[drawer] Swipe on low end devices has perf considerations

1 participant