Skip to content

fix(macos): TextInput Fabric commands - setGhostText, blur, and selection fixes#2912

Merged
Saadnajmi merged 8 commits intomicrosoft:mainfrom
Saadnajmi:t3code/fabric-textinput-refs
Apr 22, 2026
Merged

fix(macos): TextInput Fabric commands - setGhostText, blur, and selection fixes#2912
Saadnajmi merged 8 commits intomicrosoft:mainfrom
Saadnajmi:t3code/fabric-textinput-refs

Conversation

@Saadnajmi
Copy link
Copy Markdown
Collaborator

@Saadnajmi Saadnajmi commented Apr 10, 2026

Summary

TextInput commands (setGhostText, blur, setSelection) don't work correctly under Fabric on macOS. This PR fixes three issues:

  • Add missing setGhostText command to Fabric: Ghost text was implemented for Paper in [iOS/macOS] [TextInput] Implement ghost text #1890 / [iOS/macOS] [TextInput] Implement ghost text #1897 (with undo fixes in Ensure ghost text doesn't appear when performing undos #2105 / [0.73-stable] Ensure ghost text doesn't appear when performing undos #2106), but the Fabric command dispatcher and component view never got the implementation. This ports the ghost text logic (insertion, removal, attributes, delegate cleanup) from RCTBaseTextInputView to RCTTextInputComponentView.
  • Fix blur command on macOS Fabric: blur was a silent no-op because it compared [window firstResponder] against the NSTextField, but on macOS the firstResponder during editing is the field editor (NSTextView), not the text field itself. Fixed by checking currentEditor on NSTextField.
  • Fix ghost text being selectable on Fabric: Ghost text could be selected by the user on Fabric (but not Paper). Added cleanup in textInputDidChangeSelection to clear ghost text when the user changes selection, matching Paper behavior.
  • Fix setTextAndSelection delegate notification: Changed notifyDelegate:YES to notifyDelegate:NO to match Paper's setSelectionStart:selectionEnd: behavior and prevent spurious delegate callbacks during programmatic selection changes.

Test plan

  • Verified setGhostText command works via RNTester on macOS Fabric
  • Verified blur command works on macOS Fabric
  • Verified ghost text is not selectable on Fabric
  • Verified focus, clear, setSelection commands still work
  • JS TextInput tests pass

🤖 Generated with Claude Code

- Add setGhostText command to Fabric TextInput native handler
  (was declared in JS but missing from native dispatch)
- Port ghost text implementation from Paper to Fabric with
  proper Fabric API adaptations (NSRange selection, direct
  _backedTextInputView access)
- Fix setTextAndSelection using notifyDelegate:YES instead of NO,
  matching Paper behavior to prevent spurious delegate callbacks
- Add ghost text cleanup in textInputDidChange delegate
- Add RNTesterPlayground test case for verifying TextInput ref
  and commands (focus, blur, clear, setSelection, setGhostText)
- Add debug logging in setLocalRef to diagnose ref null issue
- Add test assertions for setSelection and setGhostText methods

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Saadnajmi Saadnajmi changed the title fix(fabric): implement missing TextInput commands for macOS fix(macos): TextInput Fabric commands - setGhostText, blur, and selection fixes Apr 18, 2026
- Fix blur on Fabric: use currentEditor to detect focused NSTextField
  (field editor mismatch made blur a silent no-op)
- Prevent ghost text from being selectable on Fabric by clearing it
  in textInputDidChangeSelection, matching Paper behavior
- Remove TARGET_OS_OSX guard from setGhostText command dispatch
  (was causing compilation issues with preprocessor)
- Remove temporary debug logging from TextInput.js setLocalRef
- Add commented-out Paper blur fix for future testing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Removes the RNTesterPlayground interactive test and the commented-out
Paper blur fix to keep the PR focused on Fabric TextInput command fixes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Saadnajmi Saadnajmi force-pushed the t3code/fabric-textinput-refs branch from 47ee03f to c5a26be Compare April 18, 2026 20:42
The command dispatch was calling [componentView setGhostText:] unconditionally,
but the protocol method is only declared under #if TARGET_OS_OSX. This caused
iOS and visionOS builds to fail with "no known instance method for selector".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nakambo
Copy link
Copy Markdown
Collaborator

nakambo commented Apr 20, 2026

Try undo\redo -- e.g.

  • with ghost text showing (and valid undo record), perform undo -- check that ghost text goes away, and undo works as expected. Try redo.
  • with ghost text showing do something to remove it (e.g. move IP), perform undo -- check ghost text doesn't come back. Try redo.
  • try multiple undo\redo's, even when no valid record (check for ObjC exceptions being thrown\RN asserts firing)

@Saadnajmi Saadnajmi marked this pull request as ready for review April 21, 2026 19:11
@Saadnajmi Saadnajmi requested a review from a team as a code owner April 21, 2026 19:11
Saadnajmi and others added 4 commits April 21, 2026 16:38
Ghost text insertion/removal was going through the undo manager, causing
undo/redo to restore or remove ghost text unexpectedly. Wrap ghost text
attributedText mutations in disableUndoRegistration/enableUndoRegistration
to keep ghost text changes off the undo stack, matching Paper's behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Two ghost text bugs:
1. Undo would restore ghost text as real text because the undo manager's
   pre-edit snapshot contained ghost text. Fix: clear ghost text in
   textInputShouldChangeText BEFORE the edit, so the snapshot never
   includes ghost text. This matches Paper's approach.
2. Ghost text persisted after blur. Fix: clear ghost text in
   textInputDidEndEditing, matching Paper's behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove disableUndoRegistration/enableUndoRegistration from ghost text
handling — these were corrupting the undo manager state and breaking
normal undo/redo.

The real undo protection is clearing ghost text in
textInputShouldChangeText (before the edit), so the undo manager's
pre-edit snapshot never contains ghost text.

Also skip breakUndoCoalescing in RCTUITextView when ghostTextChanging,
so ghost text insertions/removals don't create undo coalescing breaks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Ghost text insertions/removals via setAttributedString: on the text
storage were getting on the undo stack, causing Cmd+Z to undo the
invisible ghost text change instead of the user's actual typing.

Wrap the text storage mutation in disableUndoRegistration/
enableUndoRegistration specifically when ghostTextChanging, keeping
it tightly scoped to avoid corrupting the undo manager state.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Saadnajmi
Copy link
Copy Markdown
Collaborator Author

Try undo\redo -- e.g.

  • with ghost text showing (and valid undo record), perform undo -- check that ghost text goes away, and undo works as expected. Try redo.
  • with ghost text showing do something to remove it (e.g. move IP), perform undo -- check ghost text doesn't come back. Try redo.
  • try multiple undo\redo's, even when no valid record (check for ObjC exceptions being thrown\RN asserts firing)

As is, it blocks undos while ghost text is active. I think I'll leave it till we get further integration downstream.

@Saadnajmi Saadnajmi merged commit eda57a2 into microsoft:main Apr 22, 2026
31 checks passed
@Saadnajmi Saadnajmi deleted the t3code/fabric-textinput-refs branch April 22, 2026 00:27
Saadnajmi added a commit that referenced this pull request Apr 22, 2026
…ion fixes (#2937)

## Summary

Backport of #2912 to 0.81-stable.

- **Add missing `setGhostText` command to Fabric**: Ghost text was
implemented for Paper in #1890 / #1897 (with undo fixes in #2105 /
#2106), but the Fabric command dispatcher and component view never got
the implementation. This ports the ghost text logic (insertion, removal,
attributes, delegate cleanup) from `RCTBaseTextInputView` to
`RCTTextInputComponentView`.
- **Fix `blur` command on macOS Fabric**: `blur` was a silent no-op
because it compared `[window firstResponder]` against the NSTextField,
but on macOS the firstResponder during editing is the field editor
(NSTextView), not the text field itself. Fixed by checking
`currentEditor` on NSTextField.
- **Fix ghost text being selectable on Fabric**: Ghost text could be
selected by the user on Fabric (but not Paper). Added cleanup in
`textInputDidChangeSelection` to clear ghost text when the user changes
selection, matching Paper behavior.
- **Fix `setTextAndSelection` delegate notification**: Changed
`notifyDelegate:YES` to `notifyDelegate:NO` to match Paper's
`setSelectionStart:selectionEnd:` behavior and prevent spurious delegate
callbacks during programmatic selection changes.

## Test plan

- [x] Verified `setGhostText` command works via RNTester on macOS Fabric
- [x] Verified `blur` command works on macOS Fabric
- [x] Verified ghost text is not selectable on Fabric
- [x] Verified `focus`, `clear`, `setSelection` commands still work
- [x] JS TextInput tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <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.

3 participants