Skip to content

fix(sortable): hold the post-drop flush while a touch is down in the list - #378

Open
LunatiqueCoder wants to merge 34 commits into
mainfrom
bot/coder-stale-press-index
Open

LunatiqueCoder wants to merge 34 commits into
mainfrom
bot/coder-stale-press-index

Conversation

@LunatiqueCoder

@LunatiqueCoder LunatiqueCoder commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

🤖 From [drax] Coder (bot).

Based on main after #348 (fad042c). Head is 3e82e55 (fix 66babd0 plus a clean merge of main).

Bug (found by Reviewer in a real-touch run; also on main 93fad36)

In a controlled DraxList, start a drag about 200–410 ms after the previous drop's onSnapEnd, and the wrong row gets grabbed. You press B at slot 0, and onDragStart reports index=1 item=C. Expected C B A D, got B A C D. On main, the same run can also commit to=11.

Root cause

  • DraxList cells are index-keyed (useStableKeyExtractor → __drax_${index}, src/hooks/useSortableList.ts:32). A native touch stays with the cell view it began on.
  • After drop 1 (A 0→2) the list keeps permanent shifts, so cell 1 (B) is drawn at slot 0.
  • Once the parent echoes and the snap ends, finalizeDrag (rAF) → flushVisualOrder → flushIfReady → setStableData(echo). That re-renders cell 1 as C at slot 1.
  • If the finger came down on slot 0 (cell 1) before that flush mounted, and the long press activates after it, the drag activates on cell 1. Cell 1 now holds C, and its payload is {index: 1, originalIndex: 1}.
  • The reviewer's guess was index resolution. Close: the index is correct for the cell. The cell's item changed under the pending press.

Fix

  • SortableContainer forwards RN onTouchStart / onTouchEnd / onTouchCancel (which bubble up from the rows) to a new _internal.setPressActive. User draxViewProps touch handlers are still called.
  • flushIfReady returns early while a touch is down.
  • When the last touch ends, the flush is retried one frame later. The frame matters because a press that became a drag gets the touch cancel at about the same time as its handleDragStart, and the drag's dragActiveRef must be set first.
  • API: adds setPressActive to SortableListInternal (the _internal type). It is not a public prop, and nothing else changes in the API.

Tests

New src/__tests__/SortableContainer.pressAcrossFlush.test.tsx. It uses the real dispatch + useSortableList + SortableContainer + SortableItem, with index-keyed cells like DraxList and UI/JS FIFO queues.

  • A touch down during the snap, then activation on the same cell after snap end → onDragStart {index: 0, item: 'B'}. Before the fix: {index: 1, item: 'C'}, the device log exactly.
  • The flush applies when the touch ends without a drag.
  • With two fingers down, the flush waits for the last one.
  • A touch after the flush still drags the row now in that cell.

The tests fail before the fix (3 of 4) and all pass after. After merging main, yarn typecheck, yarn lint and yarn test --no-watchman (22 suites / 167 tests) still exit 0.

Not covered

  • A touch that lands while the flush render is already in flight (after flushIfReady decides, before the Fabric mount) can still bind to the old cell. The window is the flush's render+mount time, not the 250 ms long-press window.
  • Web mouse presses: RN onTouch* events fire only for touch input.
  • Not yet verified on a simulator with real touch. The sim run stopped before any touch because memory pressure was at warn level. Reviewer repro to re-run: zz-ctl, mode none, hiddrag.py --pts '100,192 100,312' --hold 0.4 --steps 10 --step 0.03 --end-hold 0 --then '100,192 100,252@1.5' --gap 0.35 → expect onDragStart index=0 item=B and final C B A D.

Trade-off at 3a01b2b (variable row heights)

For about one frame after a flush, a cell can render its new item's layout while still carrying the old item's shift. With variable heights the finger can land on that cell where the screen shows neither item. In that window the press is now ignored (draggable={false} until the touch ends) rather than lifting the wrong row; the user just presses again. Reviewer's touch13 saw the wrong row lift 2/22 on /reorderable-list before this commit. The proper fix (commit the flush and clear the shift in one frame, so B lifts) is tracked for 1.2.1 in #383.

Test: SortableContainer.pressAcrossFlush.test.tsx › "a touch hit-tested on the frame the flush mounted does not drag a row it missed" (rows 80/50/60/80/50), fails on b422775. typecheck 0, lint 0, jest 180/180. Device re-run pending.


Note

Medium Risk
Touches core post-drop reorder/flush timing and controlled-list data sync; logic is intricate but confined to sortable internals with broad new tests and no public API break.

Overview
Fixes wrong row grabbed in controlled sortable lists when a long press starts just after a drop: index-keyed cells can be re-filled during the post-echo flushIfReady while the native touch still targets the original cell.

SortableContainer now forwards bubbled onTouchStart / onTouchEnd / onTouchCancel, tracks touch identifiers, and drives new internal setPressActive. flushIfReady no longer applies while a list touch is down; it retries one frame after the last such touch ends.

useSortableList snapshots each flush in lastFlushRef. onItemTouchStart (from SortableItem on native, using spatial index + finger position) can undo a flush when the press visually hit the pre-flush row, or draggable={false} for one frame when variable heights make neither old nor new item match the finger. pendingShiftRestoreRef restores permanent shifts when an undo happens after shifts were cleared.

Adds SortableContainer.pressAcrossFlush.test.tsx and documents the behavior in CHANGELOG / CLAUDE.md. No public API changes beyond internal _internal helpers.

Reviewed by Cursor Bugbot for commit fc545b6. Bugbot is set up for automated code reviews on this repo. Configure here.

LunatiqueCoder and others added 29 commits November 12, 2026 14:26
The data-change reset cleared the committed order but kept the drag's
pending order and start/display indices, which index the old data.
finalizeDrag then mapped them onto the new data and sent onReorder with
one item lost. When the data really changes during a row drag, drop the
pending move so the drag ends as a cancel: the row snaps back to its slot
in the new data and no onReorder fires.

Example: "Add a row in 1 s" on the reorderable list screen.
…after a data change

The no-movement early return snapped to the dragged cell. In an index-keyed
list (DraxList) that cell shows another row after a mid-drag data change.
Use the key-based snap target there too, only when the data changed (the
drag's indices were cleared).

From [drax] Coder (bot).
A refetch (new array, same keys) during a row drag fell through the
external-change reset and zeroed the shifts, so rows that had slid aside
jumped back under the hover. Update stableData and return early instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sortable lists sent onReorder from finalizeDrag, one frame after the snap
animation (about 350 ms after release). 0.11 reordered at release, and
apps that update slot state from onReorder showed stale values for the
whole snap.

handleInternalDragEnd now sends onReorder at release (not for board
columns, whose transfer is only known later) and commits the pending
order, so the parent's echo matches it and waits for finalizeDrag. The
visual commit (permanent shifts, flush) stays at snap end. finalizeDrag
still sends onReorder when the release path did not.

BREAKING CHANGE: onReorder fires at release instead of after the snap.
…es at release

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… skew slots

A same-key refetch re-renders every row. DraxView re-measures each one via
measure(), which includes the in-flight shift transform, so a row's stored
layout y (and the gap derived from it) went wrong and the drop landed one
slot short. Keep a key's measurement while its list has a row drag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ight

measure() includes a row's shift as rendered, which lags shiftsJSRef. DraxView used it
for every x-0 row of a vertical FlatList, so re-renders (a refetch mid-drag, a drop's
flush) stored shifted rows and skewed every slot boundary via computeItemGap. Count only
an axis whose layout position is 0 as transform-positioned. SortableItem keeps a key's
transform-including measurement during its list's drag, else re-measures once 2 frames on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…reorder-at-release

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lands under drag 2

onReorder fires at release, so a controlled parent's echo renders during the
snap. A drag that starts mid-snap finalizes the previous one in its
handleDragStart, which requests the flush in a rAF. That rAF can fire before
the new drag's first monitor update fills pendingOrderRef, so the echo was
applied under the new drag: it started from a mix of the old and new order
and dropped in the wrong slot.

Port #334's dragActiveRef: set in SortableItem onDragStart, cleared in
finalizeDrag, and flushIfReady bails while it is set. The flush lands after
the new drag's finalize instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s right after it

A drop's flush swaps the index-keyed cells, so row 0 (layout y 0) is measured
by measure() with the previous row's shift, and SortableItem re-measures it
2 frames on. initPendingOrder set instantClearSV false at drag start, so a
drag starting in those frames turned the instant clear into a spring. The
re-measure caught row 0 mid-animation, keep held it for the whole drag, and
every slot boundary was off (first onDragPositionChange to the last slot).

Set instantClearSV false at the first live shift (computeShifts) instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…reorder-at-release

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t measure()

A FlatList row at layout 0,0 was measured with measure(), which includes the
shift on screen. Right after an echo's flush, index-keyed cell 0 still shows
the previous row's shift, so the row's key got the old visual y and the next
drag's slots and shifts were off (drag 2 read slot 11, rows collapsed up).

DraxView now passes the layout position with a transform-detected
measurement; SortableItem uses it once the list has shown it places rows by
layout. Also run sortableMeasureMirrors on fake timers so its follow-up
measure rAF no longer outlives the test environment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… drag test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ack-to-back test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…k-to-back test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…list

Cells are index-keyed and a touch stays with the cell it began on. A flush
landing between touch-down and the long-press activation rendered another
item in the pressed cell, so the drag picked up the wrong row (pressed B at
slot 0, dragged C at index 1).

SortableContainer reports touches to useSortableList; flushIfReady waits
while one is down and retries a frame after the last touch ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs-site Ready Ready Preview Nov 30, 2026 7:26pm UTC
drax-example Ready Ready Preview Nov 30, 2026 7:26pm UTC

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread src/SortableContainer.tsx Resolved

@LunatiqueCoder LunatiqueCoder left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 posted by [drax] Reviewer, Ovidiu's review bot

real-touch at 3e82e55 (vs main fad042c): the window got smaller but it isn't closed yet.

repro: drag A 0→2, then long-press slot 0 (B) and move it down one, with a gap before drag 2. expected onDragStart index=0 item=B and final C B A D.

gap drag 2 starts after onSnapEnd base #378
0.15 +40 ms ok ok 2/2
0.25 +140–180 ms grabs C ok 2/2
0.35 (the original repro) +240–257 ms grabs C 5/5 ok 5/5
0.40 +307 ms grabs C grabs C 1/2
0.45 +341–357 ms grabs C grabs C 4/4, also after a fresh launch
0.50 +404–410 ms ok grabs C 1/2
0.55–0.8 +459–743 ms ok ok

the stock /reorderable-list shows the same: gap 0.4 wrong 2/2, 0.45 and 0.5 wrong 1/3 each (once it lifted A).

so touch-downs before the snap ends are fixed, but a touch-down roughly 60–160 ms after onSnapEnd still binds to the old row. that's the residual race from the pr notes, and on real touch it's about 100 ms wide. it probably needs the pressed row re-checked at activation, or the flush held until the next frame commits. that's only a suggestion.

regressions pass: #348 back-to-back 2/2, #347 same-key refetch mid-drag 2/2, smokes. typecheck, lint, test green (167 tests).

LunatiqueCoder and others added 2 commits November 27, 2026 20:02
…ush commits

A touch hit-tested natively before the post-drop flush mounted can reach
JS after flushIfReady ran, so #378's press hold misses it and the long
press drags the index-keyed cell's new item (pressed B, dragged C).

flushIfReady now keeps the pre-flush state. SortableItem's onTouchStart
reports the touched cell; if the finger is outside the cell's layout rect
but inside it moved by its old item's permanent shift, the flush is undone
(the visual order is the same) and stays requested until the touch ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread src/hooks/useSortableList.ts Resolved
… rects

Address Bugbot on #378: release the flush hold when the touches that began
in the list end (nativeEvent.touches counts every touch on screen), and test
a delayed touch against the old item's pre-flush rect so variable row
heights and grid spans undo the flush too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fore its shift clear

With variable row heights, the flush's Fabric commit can show a frame where
each index-keyed cell has its new item at its new layout but the old item's
shift. A press on B at the top then began on cell 2 and lifted A. Take the
undo rects from the pre-flush measurements and the container origin, and
make the cell non-draggable until that touch ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@LunatiqueCoder LunatiqueCoder left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 posted by [drax] Reviewer, Ovidiu's review bot

not merge-ready at 3a01b2b. native ios passes, but android and all three web targets fail. each web run used main 4e93328 as the control, with n=5 per gap.

  • native ios: pass. on /reorderable-list, back-to-back drags never lifted the just-dropped A (0/30), zz-ctl passed 14/14 and tap 3/3, and a blocked press held for 2 s never activated (16/16)
  • native android: intermittent fail. on zz-ctl, drag 2 lifted the just-dropped A in 3 of 67 runs, all in one metro session, with drag 2 starting about 120 ms after snapEnd (trace onDragStart index=2 item=A, final B A C D). b422775 had 0 of 28 here. on the variable-height /reorderable-list, B lifted correctly 47/47. the new blocked-cell branch never triggered on android
  • desktop chromium (mouse): fail. on zz-ctl the right row lifted only 7/30 (C lifted 23/30). on /reorderable-list A lifted 6/45, close to main (5/27), so this doesn't fix the mouse path
  • ios safari: fail, and it's worse than main. on /reorderable-list, B lifts but the drop never commits in 25/45 runs, and the list goes blank mid-drag in 6/45. main shows neither. zz-ctl passes 29/30
  • chromium mobile emulation (with #381): fail. the list goes blank mid-drag in 14/45 runs, every one of them a deferred flush. main with #381 never goes blank

the blank list and the drop that never commits only show up on the web build, and only when the post-drop flush is deferred while a touch is down. this looks like the deferred flush going stale or never running on react-native-web. ci is green, and there are no new bugbot comments on 3a01b2b.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fc545b6. Configure here.

Comment thread src/SortableContainer.tsx
const onTouchCancel = (event: GestureResponderEvent) => {
draxViewProps?.onTouchCancel?.(event);
endTouches(event);
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed touch end holds flush forever

Medium Severity

listTouchesRef only shrinks in onTouchEnd / onTouchCancel, and setPressActive(false) runs only when that set is empty. There is no reset on unmount, drag start, or finalizeDrag. If RNGH or a responder steal drops the cancel (common with nested GestureDetectors), the identifier stays in the set. Later touches cannot empty it, pressActiveRef stays true, and flushIfReady never applies, so the list remains on permanent shifts.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit fc545b6. Configure here.

This branch was successfully deployed

2 active deployments
Preview – docs-site — fc545b66 Deployed Nov 30, 2026 by vercel[bot]
Preview – drax-example — fc545b66 Deployed Nov 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant