Skip to content

Keep focusable={false} Android views out of keyboard navigation - #59048

Open
YevheniiKotyrlo wants to merge 1 commit into
react:mainfrom
YevheniiKotyrlo:fix/android-focusable-keyboard-focus
Open

YevheniiKotyrlo wants to merge 1 commit into
react:mainfrom
YevheniiKotyrlo:fix/android-focusable-keyboard-focus

Conversation

@YevheniiKotyrlo

@YevheniiKotyrlo YevheniiKotyrlo commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

On Android, focusable={false} — and tabIndex={-1}, which View turns into focusable={false} — leaves a view in hardware-keyboard and D-pad navigation whenever it is also accessible. Pressable and the Touchables are accessible by default, so on any of them Tab, Shift+Tab and the arrow keys still land on the view. For View, this change makes the split of keyboard focus from screen-reader focus that #34737 asks for.

On main, setAccessible and setFocusable(true) set isFocusable, which Tab, the arrow keys and nextFocus* read, and setFocusable(false) leaves it set, so a View also stays in keyboard navigation once its focusable turns false. With this change:

  • isFocusable is keyboard focusability: focusable, and accessible while focusable is unset.
  • An accessible view that keyboard navigation passes over stays one accessibility element through screenReaderFocusable, and while its importantForAccessibility is auto is made important for accessibility, which Android infers from screenReaderFocusable only from API 35. Below API 28, where View has no screenReaderFocusable, a view tag holds it and the view's accessibility node carries it in the extras key that AndroidX's AccessibilityNodeInfoCompat.setScreenReaderFocusable writes.
  • focusable={false} behaves as a negative tabindex: with enableImperativeFocus on, ref.focus() on the view still focuses it. The view leaves keyboard navigation again once focus moves on, and a focused view whose focusable turns false keeps focus until then.

accessible, focusable, screenReaderFocusable and importantForAccessibility apply once per transaction, in ReactViewManager.onAfterUpdateTransaction, so the order they arrive in no longer changes the result. focusable is nullable from Android's HostPlatformViewProps through ReactViewManager.setFocusable, and removing it diffs to null. The public bool focusable; that each ReactAndroid*Cxx.api keeps beside the new std::optional<bool> focusable; is the one the tvOS HostPlatformViewProps.h declares, which the Android snapshots also read (#60180). An explicit focusable={false} now reaches the view under enablePropsUpdateReconciliationAndroid, and removing focusable returns the view to what accessible gives.

On Android, ViewShadowNode sets the KeyboardFocusable trait from focusable when it is set, over the isKeyboardFocusable(props) || accessible that ConcreteViewShadowNode gives every component, and FocusOrderingHelper takes its candidates from that trait alone, so a scroll view's search for the next focus among the views removeClippedSubviews detached never returns a View with focusable={false}. Dropping the helper's own focusable || accessible check changes nothing for other components, whose trait is ConcreteViewShadowNode's.

An accessibilityOrder disables its descendants' focus through the isFocusable and screenReaderFocusable these props now set, so while it holds a descendant, the keyboard focusability that focusable and accessible give a View descendant, and a screenReaderFocusable set on any descendant, are kept as the values to restore rather than applied. A held view gets them back only from that restore, so the view with the accessibilityOrder restores every descendant it disabled when its order changes, when accessibility services turn off and when it is dropped, also descendants that removeClippedSubviews or view flattening has since moved out of its subtree. A descendant disables its own descendants' focus for an order only while the view with that order is its ancestor, so a view moved out of the subtree holds no focus for an order it has left.

Beyond the fix and the accessibilityOrder changes above, five behaviours change:

  • A Touchable without onPress, which sends focusable={false}, leaves keyboard navigation and stays one TalkBack element. A disabled Touchable sends it too, but its view is also disabled (setEnabled(false)), which keyboard navigation passes over from API 28 through View.canTakeFocus, so for a disabled Touchable this changes API 24 to 27 only.
  • Under enableImperativeFocus, ref.focus() on a View with focusable={false} that is not accessible focuses that View rather than its first focusable descendant, and such a View is no longer flattened, so the call reaches it.
  • A View with screenReaderFocusable alone is important for accessibility below API 35.
  • A View's screenReaderFocusable takes effect below API 28, where it set nothing.
  • Inside an accessibilityOrder, a screenReaderFocusable view is an element of the order even when it is not focusable, and the descendants whose focus the order disables also lose screenReaderFocusable until it restores them.

Related: #59052 keeps hidden subtrees out of keyboard navigation, independently of this. The same defect on Text is #59139 (draft) and on Image #60168, which #60172 addresses.

Changelog:

[ANDROID] [FIXED] - focusable={false} and tabIndex={-1} take an accessible View out of hardware-keyboard navigation and keep it one accessibility element
[ANDROID] [FIXED] - A View whose focusable turns false leaves hardware-keyboard navigation
[ANDROID] [FIXED] - A View's screenReaderFocusable takes effect below API 28 and makes the View important for accessibility below API 35
[ANDROID] [BREAKING] - ReactViewManager.setFocusable takes a nullable Boolean, null when focusable is removed, and Android's HostPlatformViewProps::focusable is std::optional<bool>, so if (props.focusable) now tests whether the prop is set
[ANDROID] [BREAKING] - ReactViewManager's setAccessible, setFocusable, setScreenReaderFocusable and setImportantForAccessibility take effect in its onAfterUpdateTransaction, which a subclass that overrides it must call
[ANDROID] [BREAKING] - ReactAxOrderHelper.cleanUpAxOrder, restoreFocusability and disableFocusForSubtree are removed; ReactViewGroup restores the focus its accessibilityOrder disabled, and a view moved out of the order's subtree no longer disables its descendants' focus for it

Test Plan:

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest -PenableWarningsAsErrors=true \
  --tests com.facebook.react.views.view.ReactViewFocusTest \
  --tests com.facebook.react.uimanager.BaseViewManagerTest \
  --tests com.facebook.react.uimanager.ImportantForAccessibilityHelperTest

The three classes pass: ReactViewFocusTest's 41 cases on Robolectric's Android 13, 14 and 15 and one on 8.1, 124 runs, BaseViewManagerTest's 32 cases and ImportantForAccessibilityHelperTest's 2. On main, ReactViewFocusTest without the two cases that read ReactViewGroup.focusProps and ReactViewGroup.accessibilityOrderHolds, which main lacks, fails 63 of its 118 runs. build_android passes on this PR.

The six C++ cases this adds compile only for Android: the four in ViewTest and ImperativeFocusViewTest under #ifdef ANDROID, and the two in ViewPropsDiffTest under RN_SERIALIZABLE_STATE, which only Android builds define, so test_react_common_cxx, a host build, does not run them. Built with the NDK's googletest against libreactnative.so from ReactAndroid's arm64-v8a Debug CMake tree, the 19 cases of ViewTest, ImperativeFocusViewTest and ViewPropsDiffTest pass on a Galaxy S23 (Android 14) and on an Android 15 x86_64 emulator. On main the 13 existing cases pass and 4 of the 6 new ones fail; the other 2 assert what stays as on main: a Paragraph's trait, and flattening with enableImperativeFocus off.

The C++ build and run

Copy cxx-tests/ to the repository root. Its project-include.cmake adds three targets to ReactAndroid's arm64-v8a Debug CMake tree: the NDK's googletest as a static library; android_gtests, a shared library of ViewTest.cpp, ViewPropsDiffTest.cpp and cxx-tests/main.cpp linked against libreactnative.so and that googletest; and android_gtests_launcher, which loads it and runs its tests. From the repository root:

./gradlew ':packages:react-native:ReactAndroid:buildCMakeDebug[arm64-v8a]' -PreactNativeArchitectures=arm64-v8a
tree=$(ls -dt packages/react-native/ReactAndroid/.cxx/Debug/*/arm64-v8a | head -1)
tests=$PWD/packages/react-native/ReactCommon/react/renderer/components/view/tests
"$(sed -nE 's/^CMAKE_COMMAND:[A-Z]+=//p' "$tree/CMakeCache.txt")" "$tree" \
  -DCMAKE_PROJECT_ReactAndroid_INCLUDE="$PWD/cxx-tests/project-include.cmake" \
  "-DANDROID_GTEST_SOURCES=$tests/ViewTest.cpp;$tests/ViewPropsDiffTest.cpp"
"$(sed -nE 's/^CMAKE_MAKE_PROGRAM:[A-Z]+=//p' "$tree/CMakeCache.txt")" -C "$tree" android_gtests android_gtests_launcher

Then, with an arm64 phone on adb, the launcher, the library and the libraries they load go to one folder on it, where the tests run:

objects=packages/react-native/ReactAndroid/build/intermediates/cxx/Debug/$(basename "$(dirname "$tree")")/obj/arm64-v8a
fbjni=$(grep -o -m 1 '[^ ]*/libfbjni\.so' "$tree/build.ninja")
ndk=$(sed -nE 's/^ANDROID_NDK:[A-Z]+=//p' "$tree/CMakeCache.txt")
adb shell mkdir -p /data/local/tmp/view-gtests
adb push "$objects/android_gtests_launcher" "$objects/libandroid_gtests.so" "$objects/libreactnative.so" "$objects/libjsi.so" \
  "$fbjni" "$ndk"/toolchains/llvm/prebuilt/*/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so /data/local/tmp/view-gtests
adb shell "cd /data/local/tmp/view-gtests && chmod 755 android_gtests_launcher && LD_LIBRARY_PATH=. ./android_gtests_launcher '--gtest_filter=ViewTest.*:ImperativeFocusViewTest.*:ViewPropsDiffTest.*'"

On the Android 15 x86_64 emulator, the same arm64 files run under the emulator's arm64 translation, which reads the library path only from BERBERIS_GUEST_LD_LIBRARY_PATH:

adb shell "cd /data/local/tmp/view-gtests && chmod 755 android_gtests_launcher && BERBERIS_GUEST_LD_LIBRARY_PATH=. ./android_gtests_launcher '--gtest_filter=ViewTest.*:ImperativeFocusViewTest.*:ViewPropsDiffTest.*'"

The four ReactAndroid*Cxx.api snapshots are yarn cxx-api-build's output with Doxygen 1.16.1, the version Validate C++ API Snapshots installs, and that workflow passes on this PR. ReactAndroid.api is edited by hand, since binaryCompatibilityValidator.validationDisabled is set for ReactAndroid. Test All's lint job, which runs yarn lint and yarn flow-check, passes on this PR.

maestro test -e APP_ID=com.facebook.react.uiapp packages/rn-tester/.maestro/keyboard-focus-exclusion.yml, the flow this adds with RNTester's keyboard focus exclusion example, passes with the change and fails at its first assertion on main with the example added, on an Android 15 google_apis x86_64 emulator at 320×640 and 160 dpi, as test_e2e_android_rntester sets its emulator up. That job runs the flow in Test All and passes on this PR. The flow presses the D-pad, since the Maestro 2.6.1 that CI installs sends Space for pressKey: tab (#60177).

On a RedMagic NX809J (Android 16), a test screen's ten scenes are walked with these keys:

  • a tabIndex={-1} pressable in a view: Tab, Shift+Tab and D-pad Up;
  • the same in a view inside a vertical scroll view: Tab and Shift+Tab;
  • the same in a horizontal scroll view: Tab and Shift+Tab;
  • eight rows in a scroll view with removeClippedSubviews, rows 3 to 5 with tabIndex={-1}, only rows 1 and 2 attached while row 2 has focus: Tab and Shift+Tab;
  • a tabIndex={-1} pressable that another view names as its nextFocusDown and nextFocusForward: D-pad Down and Tab from that view;
  • an accessible focusable={false} view: Tab, Shift+Tab and D-pad Up;
  • an accessible focusable={false} view holding two pressables: Tab and Shift+Tab;
  • an accessible view whose focusable is set to false, then unset, then set to true: Tab after each;
  • a tabIndex={-1} pressable that a button focuses with ref.focus(): Enter on the button, then Tab and Shift+Tab;
  • a TouchableOpacity without onPress beside a disabled TouchableOpacity, a TouchableHighlight with onPress and a disabled Pressable: Tab and Shift+Tab.

With the change, each key passes over each tabIndex={-1} view and each enabled accessible focusable={false} view, where main lands on each, and otherwise stops at the views main stops at, in the same order, among them the two pressables inside the accessible view and the view whose focusable is unset or set back to true. main and the change both pass over the disabled TouchableOpacity and Pressable. A build with the change and enablePropsUpdateReconciliationAndroid forced on gives the same result at every key. With enableImperativeFocus on, ref.focus() focuses the tabIndex={-1} pressable on main and with the change; once Tab moves on, Shift+Tab lands on it again on main and passes over it with the change. With TalkBack on, RNTester's keyboard focus exclusion example keeps each of its two accessible focusable={false} rows, in a view and in a scroll view, one element on the RedMagic, as on main. Results and recordings, with the same walk on a Galaxy S23 (Android 14): evidence comment, which links the test screen and every key press.

ViewShadowNode.cpp is shared with iOS, where only its new ReactNativeFeatureFlags.h include compiles: test_ios_rntester, which builds it, passes on this PR. I could not run RNTester's iOS unit tests. Nothing ran on a device below Android 14: the behaviour below API 28 rests on ReactViewFocusTest's case on Robolectric's Android 8.1, and whether TalkBack below API 28 reads the AndroidX extras key is unverified.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 13, 2026
@YevheniiKotyrlo

YevheniiKotyrlo commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Device evidence — before / after

Before — from the text input, the first Tab lands on the accessible focusable={false} row, which reads Focused: true, and the second on the row after it.

After — the first Tab passes over that row to the next one, and the second reaches the scroll view's text input.

before — main at 6125a5d378 after — this PR at ec5bb84c8b

Screen recordings of RNTester release builds on a RedMagic NX809J (Android 16), the main build with this PR's keyboard focus exclusion example added, cropped to the example's rows, with the pauses between key presses shortened: the first text input is tapped, then Tab is sent twice with adb shell input keyevent KEYCODE_TAB.

Check main this PR
ReactViewFocusTest: 41 cases on Robolectric's Android 13, 14 and 15, and one on 8.1 with the 2 cases that read the focusProps and accessibilityOrderHolds this PR adds to ReactViewGroup taken out, as they do not compile there, 63 of the other 118 runs fail its 124 runs pass
BaseViewManagerTest and ImportantForAccessibilityHelperTest do not compile: their new cases call what this PR adds pass
ViewTest, ImperativeFocusViewTest and ViewPropsDiffTest, 19 cases, on the S23 and an Android 15 x86_64 emulator, built as the test page shows the 13 existing cases pass, and 4 of the 6 new ones fail 19 pass
keyboard-focus-exclusion.yml, which presses the D-pad, with Maestro 2.12.1, on an Android 15 google_apis x86_64 emulator at 320×640 and 160 dpi, the screen of test_e2e_android_rntester's emulator with the example added, fails at its first assertion passes
Tab through the test screen's ten scenes, Shift+Tab in eight and D-pad Up or Down in three, on the RedMagic and the S23 lands on each tabIndex={-1} view and each enabled accessible focusable={false} view passes over each, also with enablePropsUpdateReconciliationAndroid on, and otherwise stops at the views main stops at, in the same order
ref.focus() on a tabIndex={-1} pressable with enableImperativeFocus on, then Tab and Shift+Tab, on both phones focuses it, and Shift+Tab from the next view lands on it again focuses it, and Shift+Tab from the next view passes over it
TalkBack on the keyboard focus exclusion example, touching both texts of each accessible focusable={false} row on the RedMagic and swiping on the S23 (readings) each accessible focusable={false} row is one element: TalkBack's focus covers the whole row the same

The Galaxy S23's builds of this PR are of d73328b5b6, whose app differs from this PR's head only in how a view finds the accessibilityOrder it is in, which neither the test screen nor the example uses.

To rerun the unit tests: ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest -PenableWarningsAsErrors=true --tests com.facebook.react.views.view.ReactViewFocusTest --tests com.facebook.react.uimanager.BaseViewManagerTest --tests com.facebook.react.uimanager.ImportantForAccessibilityHelperTest. The C++ cases: the commands and files in the test page's C++ section. The flow, with RNTester installed: maestro test -e APP_ID=com.facebook.react.uiapp packages/rn-tester/.maestro/keyboard-focus-exclusion.yml. The test screen: its page names the builds and every key press.

@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as ready for review October 14, 2026 13:41
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 14, 2026
@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/android-focusable-keyboard-focus branch from 11de4dc to b969e83 Compare October 16, 2026 12:29
@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/android-focusable-keyboard-focus branch 2 times, most recently from 063ce7f to 709462c Compare November 11, 2026 22:21
@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor Author

@cortinico this fixes #34737, open since 2022, for View.

isFocusable, the flag every Android keyboard path reads, carried
accessible: setAccessible set it so accessibility services treat the
view as one element, and setFocusable(false) left it alone. Tab, the
arrow keys, nextFocus*, default focus, cluster navigation and an
editor's IME Next all reached an accessible view given focusable={false}
or tabIndex={-1}.

isFocusable now follows focusable, and accessible while focusable is
unset. An accessible view that keyboard navigation passes over stays
one accessibility element through screenReaderFocusable, kept below
API 28 in a view tag and the node's extras, and is made important for
accessibility, which Android derives from screenReaderFocusable only
from API 35. As with a negative tabindex in HTML, ref.focus() still
focuses the view where enableImperativeFocus dispatches it, the view
leaves keyboard navigation once focus moves on, and there a View with an
explicit focusable={false} keeps its native view so the call reaches it.

ReactViewManager collects accessible, focusable, screenReaderFocusable
and importantForAccessibility, and ReactViewGroup applies them once per
transaction, so the order the props apply in no longer changes the
result. focusable is optional from HostPlatformViewProps to
setFocusable, so an explicit false reaches the view under Props 2.0
diffing and removing it returns the view to what accessible gives.
ReactAndroid.api gains the overrides this takes: ReactViewManager's
onAfterUpdateTransaction, setImportantForAccessibility and
setScreenReaderFocusable, and ReactViewGroup's onFocusChanged and
onInitializeAccessibilityNodeInfo.

On Android a View's KeyboardFocusable trait takes the same rule, while
other components and platforms keep main's, as their views do, and
FocusOrderingHelper takes its candidates from the trait alone.

While an accessibility order holds a view's focus, ReactViewGroup and
BaseViewManager.setScreenReaderFocusable write through
ReactAxOrderHelper.setFocusable and setScreenReaderFocusable, which keep
the value for the order to restore. The view with accessibilityOrder
records the views it holds and gives their focus back when its order
changes, when accessibility services turn off and when it is dropped,
which also drops its order, including a view that removeClippedSubviews
clipped out or that view flattening moved out of its subtree. A view
follows an order only while it is in that order's subtree, so the
order parent tag buildAxOrderList leaves on it needs no clearing.
ReactAxOrderHelper's public cleanUpAxOrder, restoreFocusability and
disableFocusForSubtree, which walked the order's current subtree, are
removed.
@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/android-focusable-keyboard-focus branch from ec5bb84 to bb92824 Compare November 20, 2026 22:07
YevheniiKotyrlo added a commit to YevheniiKotyrlo/react-native-1 that referenced this pull request Nov 21, 2026
A Text with onPress or onLongPress is accessible, so setAccessible made
it focusable, and Tab, Shift+Tab, the arrow keys, nextFocus*, an
editor's IME Next and cluster navigation all stopped on it. Text had no
focusable prop to take it out, so a pressable label between two inputs
was always an extra keyboard stop.

The text view managers now collect accessible, focusable,
screenReaderFocusable and importantForAccessibility in the FocusProps
react#59048 gives View, and ReactTextView and PreparedLayoutTextView apply
them once per transaction: isFocusable follows focusable, and accessible
while focusable is unset, and an accessible text that keyboard
navigation passes over stays one accessibility element through
screenReaderFocusable, below API 28 in the node's extras. FocusProps
gains applyTo and onFocusChanged, which ReactViewGroup now calls too. A
selectable text stays focusable, since Android selects text only in a
view that takes focus, and ReactTextView applies its focus props again
after setTextIsSelectable, which resets the view's focusability.

On Android a Paragraph's KeyboardFocusable trait takes the same rule, so
a scroll view's focus search, which FocusOrderingHelper answers from
that trait, passes over an excluded text too. TextProps and the legacy
Text types declare the prop; the base view config already sends it.
YevheniiKotyrlo added a commit to YevheniiKotyrlo/react-native-1 that referenced this pull request Nov 21, 2026
A Text with onPress or onLongPress is accessible, so setAccessible made
it focusable, and a hardware keyboard's Tab, Shift+Tab and arrow keys
stopped on it. Text had no focusable prop to take it out, so a pressable
label between two inputs was always an extra keyboard stop.

The text view managers now collect accessible, focusable,
screenReaderFocusable and importantForAccessibility in the FocusProps
react#59048 gives View, and ReactTextView and PreparedLayoutTextView apply
them after each transaction: isFocusable follows focusable, and
accessible while focusable is unset, and an accessible text that
keyboard navigation passes over stays one accessibility element through
screenReaderFocusable. FocusProps gains applyTo and onFocusChanged,
which ReactViewGroup now calls in place of its own copy of them.

A selectable text stays focusable, and so does a ReactTextView whose
dataDetectorType detects links while focusable is unset, as TextView
keeps them. setTextIsSelectable, which ReactTextView calls on every
attach, and setMovementMethod rewrite the view's focusability, so
ReactTextView applies its focus props again after each.

On Android a Paragraph's KeyboardFocusable trait takes the same rule,
counting detected links only outside enablePreparedTextLayout, since
PreparedLayoutTextView detects none, so a scroll view's focus search,
which FocusOrderingHelper answers from that trait, agrees with the text
view. TextProps and the legacy Text types declare the prop; the base
view config already sends it.
ReactAndroid.api gains ReactTextView.onInitializeAccessibilityNodeInfo,
ReactTextViewManager.setFocusable, and ReactTextViewManager's
setImportantForAccessibility and setScreenReaderFocusable overrides.

This branch has not been deployed

No deployments
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. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant