Skip to content

[toast] Fix stuck drag state on release - #4742

Merged
atomiks merged 7 commits into
mui:masterfrom
aeterno-chuck:fix-toast-drag-release
May 11, 2026
Merged

atomiks merged 7 commits into
mui:masterfrom
aeterno-chuck:fix-toast-drag-release

Conversation

@aeterno-chuck

@aeterno-chuck aeterno-chuck commented May 5, 2026 •

Copy link
Copy Markdown

This fixes a toast drag edge case where release cleanup can be missed after dragging a toast far in the direction other than that of the expected dismissal directions.

In practice, the toast can feel like it is still being controlled by the mouse after the press has already been released. In a stack, that same stale drag state can leave the card suspended out of place with its content hidden behind the frontmost toast.

The change keeps the active drag state in refs, listens for release/cancel events on the owner document for the lifetime of the gesture, and safely handles pointer capture APIs so release cleanup still runs.

Before:

base-ui-toast.mp4

After:

base-ui-toast-fixed.mp4

I tested this with:

@pkg-pr-new

pkg-pr-new Bot commented May 5, 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@4742
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4742
    

commit: 0a7efe0

@code-infra-dashboard

code-infra-dashboard Bot commented May 5, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+388B(+0.08%) 🔺+133B(+0.09%)

Details of bundle changes

Performance

Total duration: 1,308.21 ms 🔺+155.34 ms(+13.5%) | Renders: 50 (+0) | Paint: 2,010.30 ms 🔺+237.51 ms(+13.4%)

Test Duration Renders
Tabs mount (200 instances) 235.72 ms 🔺+27.25 ms(+13.1%) 4 (+0)
Slider mount (300 instances) 170.32 ms 🔺+26.13 ms(+18.1%) 3 (+0)
Menu mount (300 instances) 153.03 ms 🔺+19.05 ms(+14.2%) 2 (+0)
Scroll Area mount (300 instances) 97.61 ms 🔺+14.65 ms(+17.7%) 3 (+0)
Mixed surface mount (app-like density) 89.09 ms 🔺+13.44 ms(+17.8%) 5 (+0)

…and 7 more — details


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

@netlify

netlify Bot commented May 5, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 0a7efe0
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a018281b657c30008d28c88
😎 Deploy Preview https://deploy-preview-4742--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.

@aeterno-chuck

Copy link
Copy Markdown
Author

I don’t have permission to add labels on this repo, and the required-label check is failing because the PR currently has none.

@aeterno-chuck
aeterno-chuck force-pushed the fix-toast-drag-release branch from e284426 to 655eee8 Compare May 5, 2026 00:37
@mj12albert mj12albert added the component: toast Changes related to the toast component. label May 5, 2026
@atomiks

atomiks commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

I can't seem to repro the issue myself, do you have steps beyond the recording? It seems like the problem is that the viewport collapses because it thinks it lost focus or such? I can't tell if that's just from you releasing the pointer though.


The main reason this PR's solution gives me pause is it adds a lot of code for what I'd think should be a simpler fix. Here's Codex's suggestion:

The most minimal robust fix is: keep the existing React drag logic, but add a backup document-level end listener for the active pointer. Don’t refactor the whole gesture path.

I’d aim for only these additions:

  1. activePointerIdRef
    This replaces the need for isSwipingRef; null means no active drag.

  2. dragOffsetRef
    Needed because a native document listener installed during pointerdown would otherwise read stale React state.

  3. cleanupDocumentPointerListenersRef
    Needed to remove the temporary document listeners.

Then:

onPointerDown:
  activePointerIdRef.current = event.pointerId
  add document pointerup/pointercancel listeners
  try setPointerCapture(pointerId)

onPointerMove:
  keep existing logic
  also write latest drag offset to dragOffsetRef

onPointerUp / document pointerup:
  if pointerId !== activePointerIdRef.current return
  activePointerIdRef.current = null
  remove document listeners
  clear swiping using dragOffsetRef + initialTransformRef

onPointerCancel / document pointercancel:
  same pointerId guard
  reset drag state

What I’d cut from the current PR:

  • isSwipingRef: redundant if activePointerIdRef is the source of truth for native cleanup.
  • setSwiping() wrapper: then unnecessary.
  • thin handlePointerUp / handlePointerCancel wrappers.
  • cleanupDocumentPointerListeners as a useStableCallback; just call cleanupDocumentPointerListenersRef.current?.() inline.
  • possibly releasePointerCapture() entirely, since capture is implicitly released after pointerup/pointercancel; or guard it with hasPointerCapture() instead of a whole helper.

The only helper I might keep is a tiny guarded setPointerCapture call, if they’ve actually seen it throw. But the core fix should be much smaller than the PR: backup release listener + pointer id ref + latest drag offset ref.

@aeterno-chuck

Copy link
Copy Markdown
Author

Repro steps in the docs hero (toast page):

  1. Click "Create toast" several times so a stack exists (so a non-frontmost toast is present).
  2. Hover the stack to expand it.
  3. Press and hold on a non-frontmost toast.
  4. Drag far to the left, past the toast viewport.
  5. Release.

Without the fix, the dragged toast does not snap back into the stack and its content stays at opacity 0, because the toast remains in data-swiping state with a non-zero --toast-swipe-movement-x.

The new regression test in ToastRoot.test.tsx reproduces this in jsdom by releasing pointerup on document instead of the toast root. It fails on master and passes after this PR; the failing assertion is --toast-swipe-movement-x not resetting to 0px after release.

@aeterno-chuck

Copy link
Copy Markdown
Author

I also verified the regression test against the pre-PR source.

Setup: temporary worktree at origin/master, with only the updated ToastRoot.test.tsx regression test applied.

Command:

pnpm test:jsdom ToastRoot --no-watch

Result: the new test fails as expected on master:

FAIL src/toast/root/ToastRoot.test.tsx > <Toast.Root /> > drag behavior regression > resets drag state after releasing a far swipe even when the release lands on the document

expect(element).not.toHaveAttribute("data-swiping")

Expected the element not to have attribute:
  data-swiping
Received:
  data-swiping=""

So the reduced test captures the stale drag state directly, independently of the visual stack styling in the recording.

@atomiks

atomiks commented May 5, 2026

Copy link
Copy Markdown
Contributor

With those exact steps, including a resized browser window like in the recording (so I drag out of the window), it doesn't reproduce for me - the data-swiping state is removed upon release every time.

That said, I pushed another simplified version in a new commit, can you let me know if it still works for you?

@aeterno-chuck

Copy link
Copy Markdown
Author

With those exact steps, including a resized browser window like in the recording (so I drag out of the window), it doesn't reproduce for me - the data-swiping state is removed upon release every time.

That said, I pushed another simplified version in a new commit, can you let me know if it still works for you?

To calrify, I did not drag outside the window (I was also in full screen) in the video, the desktop background is purely an video editor effect.

And yes, with the latest commit I cannot reproduce the bug and the test passes.

@atomiks

atomiks commented May 7, 2026

Copy link
Copy Markdown
Contributor

@aeterno-caspian Ah, got it — it still doesn't reproduce though regardless, for me

I further trimmed the diff to get bundle size down from 🔺+147B(+0.10%) to 🔺+119B(+0.08%), but it should work the same. Can you let me know if it still passes? If so, I'll approve and merge

@aeterno-chuck

Copy link
Copy Markdown
Author

@aeterno-caspian Ah, got it — it still doesn't reproduce though regardless, for me

I further trimmed the diff to get bundle size down from 🔺+147B(+0.10%) to 🔺+119B(+0.08%), but it should work the same. Can you let me know if it still passes? If so, I'll approve and merge

Yes, the latest commit@324b07d still fixes it for me.

I can no longer reproduce the stale drag state with the original interaction. The tests also pass locally:

pnpm test:jsdom ToastRoot --no-watch
pnpm test:jsdom toast --no-watch

Thanks for tightening this up.

@atomiks
atomiks merged commit 7a6cf63 into mui:master May 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: toast Changes related to the toast component.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants