Skip to content

Add focus and blur to View commands#52472

Closed
rozele wants to merge 1 commit into
react:mainfrom
rozele:export-D77570686
Closed

Add focus and blur to View commands#52472
rozele wants to merge 1 commit into
react:mainfrom
rozele:export-D77570686

Conversation

@rozele

@rozele rozele commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

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

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
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Facebook Partner: Facebook Partner fb-exported labels Jul 7, 2025
@facebook-github-bot

Copy link
Copy Markdown
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>
```
@cortinico cortinico closed this Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants