Skip to content

fix: clear text selection when user taps outside the view#46

Draft
haileyok wants to merge 1 commit into
mainfrom
worktree-fix-clear-selection-outside-tap
Draft

fix: clear text selection when user taps outside the view#46
haileyok wants to merge 1 commit into
mainfrom
worktree-fix-clear-selection-outside-tap

Conversation

@haileyok
Copy link
Copy Markdown
Member

@haileyok haileyok commented May 9, 2026

Summary

Fixes #36.

A non-editable, selectable UITextView does not auto-resign first responder on outside tap, so the text selection persists indefinitely. This adds a window-level UITapGestureRecognizer that fires on taps anywhere outside the component's bounds and clears selectedTextRange.

Notes on the design:

  • cancelsTouchesInView = NO so the tap still reaches whatever the user actually tapped (button, scroll view, etc.).
  • gestureRecognizer:shouldReceiveTouch: filters via hit-test so the recognizer only fires when the touch is not a descendant of self — taps inside our own text view are handled by UITextView itself.
  • The actual selectedTextRange = nil is wrapped in dispatch_async(dispatch_get_main_queue(), …) so any in-flight edit-menu action (Copy / Define / Look Up / Share / …) gets to read the live selection before we drop it. Without the defer, on iOS ≤15 the tap on a UICalloutBar button can race the action handler and Copy ends up with an empty pasteboard.
  • The recognizer is attached in didMoveToWindow and removed when the view leaves the window or is dealloc'd, so view recycling stays clean.

Test plan

  • Select text in a <UITextView selectable uiTextView>, tap empty space outside it — selection clears.
  • Select text, tap Copy in the edit menu — text is copied to the pasteboard, then selection clears.
  • Select text, tap a button elsewhere — button action fires AND selection clears.
  • Two <UITextView selectable uiTextView> on the same screen — selecting in one then tapping the other clears the first.
  • Inside a ScrollView, scrolling does not clear the selection (it isn't a tap).
  • Recycle a list of selectable text views (e.g. in a FlatList) — no crash, no leaked recognizers.

🤖 Generated with Claude Code

Adds a window-level UITapGestureRecognizer that fires when the user taps
anywhere outside the component's bounds and clears selectedTextRange.
The clear is deferred via dispatch_async so any in-flight edit-menu
action (Copy / Define / Look Up / …) reads the live selection before
we drop it.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

Text selection dose not go away when clicked outside of text

1 participant