Skip to content

fix: keep native context menus open when long-pressing sortable items#576

Draft
MatiPl01 wants to merge 1 commit into
mainfrom
fix/keep-context-menu-open
Draft

fix: keep native context menus open when long-pressing sortable items#576
MatiPl01 wants to merge 1 commit into
mainfrom
fix/keep-context-menu-open

Conversation

@MatiPl01

@MatiPl01 MatiPl01 commented Jun 27, 2026

Copy link
Copy Markdown
Owner

Closes #417.

Problem

When a Sortable.Grid / Sortable.Flex item's renderItem contains a native iOS context menu (zeego, @expo/ui, react-native-ios-context-menu - all backed by UIContextMenuInteraction), long pressing the item opens the menu and then immediately dismisses it.

Root cause: on long press the sortable activates a drag and elevates the item's zIndex (per item via useItemZIndex, and the container via the layer provider). When the native menu commits it cancels the gesture, handleDragEnd resets the zIndex, and that stacking context change on an ancestor of the menu's view dismisses the menu. Setting bringToFrontWhenActive: false did not help, because the per item zIndex still elevated and reset.

Fix

Defer every "bring to front" zIndex change until the item is actually dragged. A plain long press no longer changes any zIndex, so the native menu is never dismissed, while a real drag elevates exactly as before. The fix is library agnostic: it only changes WHEN the sortable mutates zIndex, nothing context menu specific.

The "is it a drag yet" check reuses the existing dragActivationFailOffset (the offset already used to tell a scroll from a drag) instead of a new threshold.

// useItemZIndex.ts - keep the plain order zIndex until the item is dragged
if (!activeItemBroughtToFront.value) {
  return orderZIndex;
}
// DragProvider.handleTouchesMove - elevate only once the touch actually moves
if (
  !activeItemBroughtToFront.value &&
  movedDistance >= dragActivationFailOffset.value
) {
  activeItemBroughtToFront.value = true;
  updateLayer?.(LayerState.FOCUSED);
}

Verification

  • Added a unit test for useItemZIndex: no elevation during a long press, identical elevation once the item is dragged.
  • On the iOS simulator (iPhone 16 Pro) with react-native-ios-context-menu wrapping each grid item, dragging still reorders correctly, and the menu was observed opening and staying with the fix.

The fix removes the exact thing the issue diagnoses as the cause (the long press zIndex change), so a native menu that opens on long press is no longer dismissed by it.

Note: the dismissal is a transient, timing sensitive race between the native long press recognizer and the sortable's gesture, which is hard to trigger deterministically with automated touch input on the simulator. Before / after recordings are best captured on a physical device.

Before (without fix) After (with fix)
recording recording

@MatiPl01 MatiPl01 self-assigned this Jun 27, 2026
@vercel

vercel Bot commented Jun 27, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
react-native-sortables-docs Ignored Ignored Preview Jun 28, 2026 10:05am

@MatiPl01 MatiPl01 force-pushed the fix/keep-context-menu-open branch from 6dd6913 to 1585f0c Compare June 28, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: can't work with ContextMenu such as zeego

1 participant