Skip to content

fix: defer clearSelection after copy to prevent broken subsequent selections#14416

Open
EliasHanken wants to merge 1 commit intoanomalyco:devfrom
EliasHanken:fix/selection-copy-clear-race
Open

fix: defer clearSelection after copy to prevent broken subsequent selections#14416
EliasHanken wants to merge 1 commit intoanomalyco:devfrom
EliasHanken:fix/selection-copy-clear-race

Conversation

@EliasHanken
Copy link

@EliasHanken EliasHanken commented Feb 20, 2026

Issue for this PR

Fixes #14420

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When copy-on-select triggers on mouseup, Selection.copy() calls renderer.clearSelection() synchronously inside the onMouseUp handler. This runs during @opentui's processMouseEvent bubbling, before finishSelection() gets to execute. Nullifying currentSelection mid-event means the renderer can't finish its selection lifecycle (isDragging=false, emit selection event), leaving stale internal state that blocks subsequent text selections.

Fix: wrap clearSelection() in queueMicrotask() so the renderer's handleMouseData completes its full pipeline first. The microtask runs before the next mouse event arrives, so cleanup is still prompt.

How did you verify your code works?

  • Typecheck passes across all 12 packages
  • All 1130 unit tests pass
  • Manual testing: bun dev . from repo root, rapidly select-and-copy text multiple times -- selections now work consistently on every attempt

Screenshots / recordings

Not a UI change -- behavior fix only.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Feb 20, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

…ections

When copy-on-select triggers on mouseup, Selection.copy() was calling
renderer.clearSelection() synchronously inside the onMouseUp handler.
This runs during @OpenTui's processMouseEvent bubbling, BEFORE
finishSelection() executes. By nullifying currentSelection prematurely,
finishSelection() becomes a no-op and cannot properly set isDragging=false
or emit the 'selection' event, leaving internal renderer state stale.

This caused subsequent text selection attempts to fail intermittently
until the stale state was incidentally cleared (e.g. by a resize).

Fix: use queueMicrotask() to defer clearSelection() so that @OpenTui's
handleMouseData can complete its full selection lifecycle first.
@EliasHanken EliasHanken force-pushed the fix/selection-copy-clear-race branch from 91f5841 to b606e3f Compare February 20, 2026 11:21
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 stops working after copy-on-select triggers

1 participant