Repository navigation
[toast] Fix stuck drag state on release - #4742
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,308.21 ms 🔺+155.34 ms(+13.5%) | Renders: 50 (+0) | Paint: 2,010.30 ms 🔺+237.51 ms(+13.4%)
…and 7 more — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
I don’t have permission to add labels on this repo, and the required-label check is failing because the PR currently has none. |
e284426 to
655eee8
Compare
|
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:
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 stateWhat I’d cut from the current PR:
The only helper I might keep is a tiny guarded |
|
Repro steps in the docs hero (toast page):
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 The new regression test in |
|
I also verified the regression test against the pre-PR source. Setup: temporary worktree at Command: pnpm test:jsdom ToastRoot --no-watchResult: the new test fails as expected on So the reduced test captures the stale drag state directly, independently of the visual stack styling in the recording. |
|
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 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. |
|
@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:
Thanks for tightening this up. |
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:
pnpm test:jsdom toast --no-watchpnpm typescript --pretty falseI have followed (at least) the PR section of the contributing guide.