Skip to content

[useResponderEvents] Caret movement in input prematurely terminates active touch responder #2889

Closed

Description

@jmgraa

Is there an existing issue for this?

  • I have searched the existing issues

Describe the issue

In useResponderEvents, hasValidSelection(domEvent) unconditionally returns true for all DOM select events:

export function hasValidSelection(domEvent: any): boolean {
  if (domEvent.type === 'selectionchange') {
    return isSelectionValid();
  }
  return domEvent.type === 'select';
}

Per the W3C / WHATWG HTML specification, web browsers queue a select event on <input> and <textarea> elements not only when text is highlighted, but also whenever the selection range changes programmatically (e.g. calling setSelectionRange(pos, pos) or during caret restoration in React controlled inputs after state updates).

In these cases, selectionStart === selectionEnd (the selection is collapsed / caret-only; no text is highlighted). However, because hasValidSelection returns true, the ResponderSystem classifies this as a termination event and immediately dispatches onResponderTerminate, killing the active responder lock.

This prematurely cancels active touch gestures (e.g. continuous long-press deletion on custom keypad buttons, sliders, or drag gestures) whenever an input updates its caret.

Expected behavior

Caret movement or programmatic cursor restoration (selectionStart === selectionEnd) should not terminate active touch responders, matching native React Native behavior on iOS and Android. Responders should only be terminated on select if an actual range of text is selected (selectionStart !== selectionEnd), falling back to true (fail-open) if coordinates cannot be determined.

Steps to reproduce

Tested with react-native-web version: 0.21.2 (and reproducible on 0.21.3 / master)

  1. Render a component with touch responder handlers (onStartShouldSetResponder={() => true}, onResponderGrant, onResponderTerminate).
  2. Render a controlled <input> or TextInput.
  3. Press and hold the responder element (it becomes the active responder).
  4. Trigger a programmatic caret update or update the controlled input's value such that setSelectionRange(pos, pos) is called.
  5. The input emits a select event with a collapsed range (selectionStart === selectionEnd).
  6. Result: onResponderTerminate is unexpectedly fired on the responder component and the gesture is cancelled.

Test case

https://codesandbox.io/p/sandbox/zealous-mountain-dwr5h9

Additional comments

This bug was discovered while testing the Expensify app:

expensify_bug.mov

I have a fix ready with full unit test coverage and will open a PR referencing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions