Add focus and blur to View commands#52472
Closed
rozele wants to merge 1 commit into
Closed
Conversation
Summary: You can call `ref.focus` on arbitrary views in React Native, but the command implementation is only wired up for TextInput on Android. This wires up focus and blur commands for Android Views behind a feature flag. View does not clear TextInputState from onBlur, calling ref.focus() on a View without first calling ref.focus() on another host component will not result in a native command being issued. This change uses the same feature flag to conditionally remove the check that suppresses focus commands if the last focused ref is equivalent. This works around this issue without wiring up additional functionality in the View JS wrapper. Generally speaking, this should be safe, as most platforms are idempotent with respect to focus requests (i.e., no-ops if trying to focus something already focused). ## Changelog [Android][Added] Focus/blur command handling on View Differential Revision: D77570686
Contributor
|
This pull request was exported from Phabricator. Differential Revision: D77570686 |
Saadnajmi
added a commit
to microsoft/react-native-macos
that referenced
this pull request
Jul 23, 2025
## Summary: This is fixed upstream with react#52472 . Let's commit (and backport) a smaller fix for now. ## Test Plan: Minimal repro of bug is this code below. Basically, you can't focus on "Pressable Child 2" twice by clicking on the container ``` <Pressable style={{backgroundColor: 'lightblue', padding: 10, gap: 10}} onPress={() => {myRef.current.focus();}}> <Text>Pressable Container</Text> <Pressable><Text>Pressable Child 1</Text></Pressable> <Pressable ref={myRef}><Text>Pressable Child 2</Text></Pressable> <Pressable><Text>Pressable Child 3</Text></Pressable> </Pressable> ```
Saadnajmi
added a commit
to Saadnajmi/react-native-macos
that referenced
this pull request
Jul 23, 2025
## Summary: This is fixed upstream with react#52472 . Let's commit (and backport) a smaller fix for now. ## Test Plan: Minimal repro of bug is this code below. Basically, you can't focus on "Pressable Child 2" twice by clicking on the container ``` <Pressable style={{backgroundColor: 'lightblue', padding: 10, gap: 10}} onPress={() => {myRef.current.focus();}}> <Text>Pressable Container</Text> <Pressable><Text>Pressable Child 1</Text></Pressable> <Pressable ref={myRef}><Text>Pressable Child 2</Text></Pressable> <Pressable><Text>Pressable Child 3</Text></Pressable> </Pressable> ```
Saadnajmi
added a commit
to Saadnajmi/react-native-macos
that referenced
this pull request
Jul 23, 2025
## Summary: This is fixed upstream with react#52472 . Let's commit (and backport) a smaller fix for now. ## Test Plan: Minimal repro of bug is this code below. Basically, you can't focus on "Pressable Child 2" twice by clicking on the container ``` <Pressable style={{backgroundColor: 'lightblue', padding: 10, gap: 10}} onPress={() => {myRef.current.focus();}}> <Text>Pressable Container</Text> <Pressable><Text>Pressable Child 1</Text></Pressable> <Pressable ref={myRef}><Text>Pressable Child 2</Text></Pressable> <Pressable><Text>Pressable Child 3</Text></Pressable> </Pressable> ```
Saadnajmi
added a commit
to Saadnajmi/react-native-macos
that referenced
this pull request
Jul 23, 2025
## Summary: This is fixed upstream with react#52472 . Let's commit (and backport) a smaller fix for now. ## Test Plan: Minimal repro of bug is this code below. Basically, you can't focus on "Pressable Child 2" twice by clicking on the container ``` <Pressable style={{backgroundColor: 'lightblue', padding: 10, gap: 10}} onPress={() => {myRef.current.focus();}}> <Text>Pressable Container</Text> <Pressable><Text>Pressable Child 1</Text></Pressable> <Pressable ref={myRef}><Text>Pressable Child 2</Text></Pressable> <Pressable><Text>Pressable Child 3</Text></Pressable> </Pressable> ```
Saadnajmi
added a commit
to Saadnajmi/react-native-macos
that referenced
this pull request
Jul 23, 2025
## Summary: This is fixed upstream with react#52472 . Let's commit (and backport) a smaller fix for now. ## Test Plan: Minimal repro of bug is this code below. Basically, you can't focus on "Pressable Child 2" twice by clicking on the container ``` <Pressable style={{backgroundColor: 'lightblue', padding: 10, gap: 10}} onPress={() => {myRef.current.focus();}}> <Text>Pressable Container</Text> <Pressable><Text>Pressable Child 1</Text></Pressable> <Pressable ref={myRef}><Text>Pressable Child 2</Text></Pressable> <Pressable><Text>Pressable Child 3</Text></Pressable> </Pressable> ```
Saadnajmi
added a commit
to Saadnajmi/react-native-macos
that referenced
this pull request
Jul 23, 2025
## Summary: This is fixed upstream with react#52472 . Let's commit (and backport) a smaller fix for now. ## Test Plan: Minimal repro of bug is this code below. Basically, you can't focus on "Pressable Child 2" twice by clicking on the container ``` <Pressable style={{backgroundColor: 'lightblue', padding: 10, gap: 10}} onPress={() => {myRef.current.focus();}}> <Text>Pressable Container</Text> <Pressable><Text>Pressable Child 1</Text></Pressable> <Pressable ref={myRef}><Text>Pressable Child 2</Text></Pressable> <Pressable><Text>Pressable Child 3</Text></Pressable> </Pressable> ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
You can call
ref.focuson arbitrary views in React Native, but the command implementation is only wired up for TextInput on Android. This wires up focus and blur commands for Android Views behind a feature flag.View does not clear TextInputState from onBlur, calling ref.focus() on a View without first calling ref.focus() on another host component will not result in a native command being issued. This change uses the same feature flag to conditionally remove the check that suppresses focus commands if the last focused ref is equivalent. This works around this issue without wiring up additional functionality in the View JS wrapper. Generally speaking, this should be safe, as most platforms are idempotent with respect to focus requests (i.e., no-ops if trying to focus something already focused).
Changelog
[Android][Added] Focus/blur command handling on View
Differential Revision: D77570686