Repository navigation
Keep focusable={false} Android views out of keyboard navigation - #59048
YevheniiKotyrlo wants to merge 1 commit into
Conversation
Device evidence — before / afterBefore — from the text input, the first Tab lands on the accessible After — the first Tab passes over that row to the next one, and the second reaches the scroll view's text input.
Screen recordings of RNTester release builds on a RedMagic NX809J (Android 16), the
The Galaxy S23's builds of this PR are of To rerun the unit tests: |
11de4dc to
b969e83
Compare
063ce7f to
709462c
Compare
|
@cortinico this fixes #34737, open since 2022, for |
d73328b to
7240566
Compare
7240566 to
ec5bb84
Compare
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.
ec5bb84 to
bb92824
Compare
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.
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.


Summary:
On Android,
focusable={false}— andtabIndex={-1}, whichViewturns intofocusable={false}— leaves a view in hardware-keyboard and D-pad navigation whenever it is alsoaccessible.Pressableand the Touchables are accessible by default, so on any of them Tab, Shift+Tab and the arrow keys still land on the view. ForView, this change makes the split of keyboard focus from screen-reader focus that #34737 asks for.On
main,setAccessibleandsetFocusable(true)setisFocusable, which Tab, the arrow keys andnextFocus*read, andsetFocusable(false)leaves it set, so aViewalso stays in keyboard navigation once itsfocusableturns false. With this change:isFocusableis keyboard focusability:focusable, andaccessiblewhilefocusableis unset.screenReaderFocusable, and while itsimportantForAccessibilityisautois made important for accessibility, which Android infers fromscreenReaderFocusableonly from API 35. Below API 28, whereViewhas noscreenReaderFocusable, a view tag holds it and the view's accessibility node carries it in the extras key that AndroidX'sAccessibilityNodeInfoCompat.setScreenReaderFocusablewrites.focusable={false}behaves as a negativetabindex: withenableImperativeFocuson,ref.focus()on the view still focuses it. The view leaves keyboard navigation again once focus moves on, and a focused view whosefocusableturns false keeps focus until then.accessible,focusable,screenReaderFocusableandimportantForAccessibilityapply once per transaction, inReactViewManager.onAfterUpdateTransaction, so the order they arrive in no longer changes the result.focusableis nullable from Android'sHostPlatformViewPropsthroughReactViewManager.setFocusable, and removing it diffs tonull. Thepublic bool focusable;that eachReactAndroid*Cxx.apikeeps beside the newstd::optional<bool> focusable;is the one the tvOSHostPlatformViewProps.hdeclares, which the Android snapshots also read (#60180). An explicitfocusable={false}now reaches the view underenablePropsUpdateReconciliationAndroid, and removingfocusablereturns the view to whataccessiblegives.On Android,
ViewShadowNodesets theKeyboardFocusabletrait fromfocusablewhen it is set, over theisKeyboardFocusable(props) || accessiblethatConcreteViewShadowNodegives every component, andFocusOrderingHelpertakes its candidates from that trait alone, so a scroll view's search for the next focus among the viewsremoveClippedSubviewsdetached never returns aViewwithfocusable={false}. Dropping the helper's ownfocusable || accessiblecheck changes nothing for other components, whose trait isConcreteViewShadowNode's.An
accessibilityOrderdisables its descendants' focus through theisFocusableandscreenReaderFocusablethese props now set, so while it holds a descendant, the keyboard focusability thatfocusableandaccessiblegive aViewdescendant, and ascreenReaderFocusableset 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 theaccessibilityOrderrestores every descendant it disabled when its order changes, when accessibility services turn off and when it is dropped, also descendants thatremoveClippedSubviewsor 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
accessibilityOrderchanges above, five behaviours change:onPress, which sendsfocusable={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 throughView.canTakeFocus, so for a disabled Touchable this changes API 24 to 27 only.enableImperativeFocus,ref.focus()on aViewwithfocusable={false}that is notaccessiblefocuses that View rather than its first focusable descendant, and such a View is no longer flattened, so the call reaches it.ViewwithscreenReaderFocusablealone is important for accessibility below API 35.View'sscreenReaderFocusabletakes effect below API 28, where it set nothing.accessibilityOrder, ascreenReaderFocusableview is an element of the order even when it is not focusable, and the descendants whose focus the order disables also losescreenReaderFocusableuntil it restores them.Related: #59052 keeps hidden subtrees out of keyboard navigation, independently of this. The same defect on
Textis #59139 (draft) and onImage#60168, which #60172 addresses.Changelog:
[ANDROID] [FIXED] -
focusable={false}andtabIndex={-1}take an accessibleViewout of hardware-keyboard navigation and keep it one accessibility element[ANDROID] [FIXED] - A
Viewwhosefocusableturns false leaves hardware-keyboard navigation[ANDROID] [FIXED] - A
View'sscreenReaderFocusabletakes effect below API 28 and makes the View important for accessibility below API 35[ANDROID] [BREAKING] -
ReactViewManager.setFocusabletakes a nullableBoolean, null whenfocusableis removed, and Android'sHostPlatformViewProps::focusableisstd::optional<bool>, soif (props.focusable)now tests whether the prop is set[ANDROID] [BREAKING] -
ReactViewManager'ssetAccessible,setFocusable,setScreenReaderFocusableandsetImportantForAccessibilitytake effect in itsonAfterUpdateTransaction, which a subclass that overrides it must call[ANDROID] [BREAKING] -
ReactAxOrderHelper.cleanUpAxOrder,restoreFocusabilityanddisableFocusForSubtreeare removed;ReactViewGrouprestores the focus itsaccessibilityOrderdisabled, and a view moved out of the order's subtree no longer disables its descendants' focus for itTest Plan:
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 andImportantForAccessibilityHelperTest's 2. Onmain,ReactViewFocusTestwithout the two cases that readReactViewGroup.focusPropsandReactViewGroup.accessibilityOrderHolds, whichmainlacks, fails 63 of its 118 runs.build_androidpasses on this PR.The six C++ cases this adds compile only for Android: the four in
ViewTestandImperativeFocusViewTestunder#ifdef ANDROID, and the two inViewPropsDiffTestunderRN_SERIALIZABLE_STATE, which only Android builds define, sotest_react_common_cxx, a host build, does not run them. Built with the NDK's googletest againstlibreactnative.sofrom ReactAndroid's arm64-v8a Debug CMake tree, the 19 cases ofViewTest,ImperativeFocusViewTestandViewPropsDiffTestpass on a Galaxy S23 (Android 14) and on an Android 15 x86_64 emulator. Onmainthe 13 existing cases pass and 4 of the 6 new ones fail; the other 2 assert what stays as onmain: a Paragraph's trait, and flattening withenableImperativeFocusoff.The C++ build and run
Copy
cxx-tests/to the repository root. Itsproject-include.cmakeadds three targets to ReactAndroid's arm64-v8a Debug CMake tree: the NDK's googletest as a static library;android_gtests, a shared library ofViewTest.cpp,ViewPropsDiffTest.cppandcxx-tests/main.cpplinked againstlibreactnative.soand that googletest; andandroid_gtests_launcher, which loads it and runs its tests. From the repository root: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: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.apisnapshots areyarn 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.apiis edited by hand, sincebinaryCompatibilityValidator.validationDisabledis set for ReactAndroid. Test All'slintjob, which runsyarn lintandyarn 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 onmainwith the example added, on an Android 15 google_apis x86_64 emulator at 320×640 and 160 dpi, astest_e2e_android_rntestersets 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 forpressKey: tab(#60177).On a RedMagic NX809J (Android 16), a test screen's ten scenes are walked with these keys:
tabIndex={-1}pressable in a view: Tab, Shift+Tab and D-pad Up;removeClippedSubviews, rows 3 to 5 withtabIndex={-1}, only rows 1 and 2 attached while row 2 has focus: Tab and Shift+Tab;tabIndex={-1}pressable that another view names as itsnextFocusDownandnextFocusForward: D-pad Down and Tab from that view;focusable={false}view: Tab, Shift+Tab and D-pad Up;focusable={false}view holding two pressables: Tab and Shift+Tab;focusableis set to false, then unset, then set to true: Tab after each;tabIndex={-1}pressable that a button focuses withref.focus(): Enter on the button, then Tab and Shift+Tab;TouchableOpacitywithoutonPressbeside a disabledTouchableOpacity, aTouchableHighlightwithonPressand a disabledPressable: Tab and Shift+Tab.With the change, each key passes over each
tabIndex={-1}view and each enabled accessiblefocusable={false}view, wheremainlands on each, and otherwise stops at the viewsmainstops at, in the same order, among them the two pressables inside the accessible view and the view whosefocusableis unset or set back to true.mainand the change both pass over the disabledTouchableOpacityandPressable. A build with the change andenablePropsUpdateReconciliationAndroidforced on gives the same result at every key. WithenableImperativeFocuson,ref.focus()focuses thetabIndex={-1}pressable onmainand with the change; once Tab moves on, Shift+Tab lands on it again onmainand passes over it with the change. With TalkBack on, RNTester's keyboard focus exclusion example keeps each of its two accessiblefocusable={false}rows, in a view and in a scroll view, one element on the RedMagic, as onmain. 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.cppis shared with iOS, where only its newReactNativeFeatureFlags.hinclude 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 onReactViewFocusTest's case on Robolectric's Android 8.1, and whether TalkBack below API 28 reads the AndroidX extras key is unverified.