Skip to content

Commit bb92824

Browse files
Take focusable={false} Android views out of keyboard navigation
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.
1 parent 5c062e6 commit bb92824

25 files changed

Lines changed: 1389 additions & 136 deletions

File tree

‎packages/react-native/ReactAndroid/api/ReactAndroid.api‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,9 +3733,6 @@ public final class com/facebook/react/uimanager/ReactAccessibilityDelegate$Role$
37333733
public final class com/facebook/react/uimanager/ReactAxOrderHelper {
37343734
public static final field INSTANCE Lcom/facebook/react/uimanager/ReactAxOrderHelper;
37353735
public final fun buildAxOrderList (Landroid/view/View;Landroid/view/View;Ljava/util/List;[Landroid/view/View;)V
3736-
public static final fun cleanUpAxOrder (Landroid/view/View;)V
3737-
public final fun disableFocusForSubtree (Landroid/view/View;Ljava/util/List;)V
3738-
public static final fun restoreFocusability (Landroid/view/View;)V
37393736
}
37403737

37413738
public abstract interface class com/facebook/react/uimanager/ReactClippingProhibitedView {
@@ -6483,7 +6480,9 @@ public class com/facebook/react/views/view/ReactViewGroup : android/view/ViewGro
64836480
protected fun onAttachedToWindow ()V
64846481
protected fun onConfigurationChanged (Landroid/content/res/Configuration;)V
64856482
protected fun onDetachedFromWindow ()V
6483+
protected fun onFocusChanged (ZILandroid/graphics/Rect;)V
64866484
public fun onHoverEvent (Landroid/view/MotionEvent;)Z
6485+
public fun onInitializeAccessibilityNodeInfo (Landroid/view/accessibility/AccessibilityNodeInfo;)V
64876486
public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z
64886487
protected fun onLayout (ZIIII)V
64896488
protected fun onMeasure (II)V
@@ -6529,6 +6528,8 @@ public class com/facebook/react/views/view/ReactViewManager : com/facebook/react
65296528
public fun nextFocusLeft (Lcom/facebook/react/views/view/ReactViewGroup;I)V
65306529
public fun nextFocusRight (Lcom/facebook/react/views/view/ReactViewGroup;I)V
65316530
public fun nextFocusUp (Lcom/facebook/react/views/view/ReactViewGroup;I)V
6531+
public synthetic fun onAfterUpdateTransaction (Landroid/view/View;)V
6532+
protected fun onAfterUpdateTransaction (Lcom/facebook/react/views/view/ReactViewGroup;)V
65326533
public synthetic fun onDropViewInstance (Landroid/view/View;)V
65336534
public fun onDropViewInstance (Lcom/facebook/react/views/view/ReactViewGroup;)V
65346535
public synthetic fun prepareToRecycleView (Lcom/facebook/react/uimanager/ThemedReactContext;Landroid/view/View;)Landroid/view/View;
@@ -6555,15 +6556,19 @@ public class com/facebook/react/views/view/ReactViewManager : com/facebook/react
65556556
public fun setExperimentalBackgroundPosition (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;)V
65566557
public fun setExperimentalBackgroundRepeat (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;)V
65576558
public fun setExperimentalBackgroundSize (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;)V
6558-
public fun setFocusable (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
6559+
public fun setFocusable (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/Boolean;)V
65596560
public fun setHitSlop (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/Dynamic;)V
6561+
public synthetic fun setImportantForAccessibility (Landroid/view/View;Ljava/lang/String;)V
6562+
public fun setImportantForAccessibility (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;)V
65606563
public fun setNativeBackground (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableMap;)V
65616564
public fun setNativeForeground (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableMap;)V
65626565
public fun setNeedsOffscreenAlphaCompositing (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
65636566
public synthetic fun setOpacity (Landroid/view/View;F)V
65646567
public fun setOpacity (Lcom/facebook/react/views/view/ReactViewGroup;F)V
65656568
public fun setOverflow (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;)V
65666569
public fun setPointerEvents (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;)V
6570+
public synthetic fun setScreenReaderFocusable (Landroid/view/View;Z)V
6571+
public fun setScreenReaderFocusable (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
65676572
public fun setTVPreferredFocus (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
65686573
public synthetic fun setTransformProperty (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/ReadableArray;)V
65696574
protected fun setTransformProperty (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/ReadableArray;)V

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,21 +515,18 @@ public void setAccessibilityValue(T view, ReadableMap accessibilityValue) {
515515
@ReactProp(name = ViewProps.IMPORTANT_FOR_ACCESSIBILITY)
516516
public void setImportantForAccessibility(
517517
@NonNull T view, @Nullable String importantForAccessibility) {
518-
if (importantForAccessibility == null || importantForAccessibility.equals("auto")) {
519-
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
520-
} else if (importantForAccessibility.equals("yes")) {
521-
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
522-
} else if (importantForAccessibility.equals("no")) {
523-
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
524-
} else if (importantForAccessibility.equals("no-hide-descendants")) {
525-
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
518+
@Nullable
519+
Integer mode =
520+
ImportantForAccessibilityHelper.parseImportantForAccessibility(importantForAccessibility);
521+
if (mode != null) {
522+
view.setImportantForAccessibility(mode);
526523
}
527524
}
528525

529526
@ReactProp(name = ViewProps.SCREEN_READER_FOCUSABLE)
530527
public void setScreenReaderFocusable(@NonNull T view, boolean screenReaderFocusable) {
531528
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
532-
view.setScreenReaderFocusable(screenReaderFocusable);
529+
ReactAxOrderHelper.internal_setScreenReaderFocusable(view, screenReaderFocusable);
533530
}
534531
}
535532

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager
9+
10+
import android.view.View
11+
12+
internal object ImportantForAccessibilityHelper {
13+
@JvmStatic
14+
fun parseImportantForAccessibility(importantForAccessibility: String?): Int? =
15+
when (importantForAccessibility) {
16+
null,
17+
"auto" -> View.IMPORTANT_FOR_ACCESSIBILITY_AUTO
18+
"yes" -> View.IMPORTANT_FOR_ACCESSIBILITY_YES
19+
"no" -> View.IMPORTANT_FOR_ACCESSIBILITY_NO
20+
"no-hide-descendants" -> View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
21+
else -> null
22+
}
23+
}

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAxOrderHelper.kt‎

Lines changed: 78 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,89 +10,42 @@ package com.facebook.react.uimanager
1010
import android.view.View
1111
import android.view.ViewGroup
1212
import com.facebook.react.R
13+
import java.util.Collections
14+
import java.util.WeakHashMap
1315

1416
/**
1517
* Helper object for managing accessibility order in React Native views.
1618
*
17-
* This object provides utilities to manage the accessibility focus order of views by storing and
18-
* restoring focusability states, and building ordered lists of views based on accessibility order
19-
* preferences.
19+
* This object provides utilities to build ordered lists of views based on accessibility order
20+
* preferences, and to write the focus flags of views whose focus an accessibility order holds.
2021
*/
2122
public object ReactAxOrderHelper {
22-
/**
23-
* Cleans up accessibility order state from a view and its children.
24-
*
25-
* This method removes stored focusability states and accessibility order parent references from
26-
* the view hierarchy. It recursively processes all children of ViewGroup instances.
27-
*
28-
* @param view The view from which to clean up accessibility order state
29-
*/
30-
@JvmStatic
31-
public fun cleanUpAxOrder(view: View) {
32-
val originalFocusability = view.getTag(R.id.original_focusability) as Boolean?
23+
// A focus flag set while an accessibility order holds the view's focus is the one it restores.
24+
internal fun setFocusable(view: View, focusable: Boolean) {
25+
val originalFocusability = view.originalFocusability
3326
if (originalFocusability != null) {
34-
view.isFocusable = originalFocusability
35-
}
36-
37-
val axOrderParent = view.getTag(R.id.accessibility_order_parent) as View?
38-
if (axOrderParent != null) {
39-
view.setTag(R.id.accessibility_order_parent, null)
40-
}
41-
42-
if (view is ViewGroup) {
43-
for (i in 0..<view.childCount) {
44-
cleanUpAxOrder(view.getChildAt(i))
45-
}
27+
view.setTag(R.id.original_focusability, originalFocusability.copy(focusable = focusable))
28+
} else {
29+
view.isFocusable = focusable
4630
}
4731
}
4832

49-
/**
50-
* Restores the original focusability state of a view and its children.
51-
*
52-
* This method traverses the view hierarchy and restores the focusability state that was
53-
* previously saved with the `R.id.original_focusability` tag. This is typically used after
54-
* accessibility order operations are complete to return views to their original state.
55-
*
56-
* @param view The view whose focusability state should be restored
57-
*/
5833
@JvmStatic
59-
public fun restoreFocusability(view: View) {
60-
val originalFocusability = view.getTag(R.id.original_focusability) as Boolean?
34+
@JvmName("internal_setScreenReaderFocusable")
35+
internal fun setScreenReaderFocusable(view: View, screenReaderFocusable: Boolean) {
36+
val originalFocusability = view.originalFocusability
6137
if (originalFocusability != null) {
62-
view.isFocusable = originalFocusability
63-
}
64-
65-
if (view is ViewGroup) {
66-
for (i in 0..<view.childCount) {
67-
restoreFocusability(view.getChildAt(i))
68-
}
38+
view.setTag(
39+
R.id.original_focusability,
40+
originalFocusability.copy(screenReaderFocusable = screenReaderFocusable),
41+
)
42+
} else {
43+
ScreenReaderFocusableHelper.setScreenReaderFocusable(view, screenReaderFocusable)
6944
}
7045
}
7146

72-
/**
73-
* Disables focus for all views in the subtree that are not in the accessibility order list.
74-
*
75-
* This method recursively traverses the view hierarchy and disables focusability for views that
76-
* are not included in the provided accessibility order list. It stores the original focusability
77-
* state before modifying it, allowing for later restoration.
78-
*
79-
* @param view The root view of the subtree to process
80-
* @param axOrderList The list of native IDs that should maintain their focusability
81-
*/
82-
public fun disableFocusForSubtree(view: View, axOrderList: MutableList<*>) {
83-
if (!axOrderList.contains(view.getTag(R.id.view_tag_native_id))) {
84-
if (view.getTag(R.id.original_focusability) == null) {
85-
view.setTag(R.id.original_focusability, view.isFocusable)
86-
}
87-
view.isFocusable = false
88-
}
89-
90-
if (view is ViewGroup) {
91-
for (i in 0..<view.childCount) {
92-
disableFocusForSubtree(view.getChildAt(i), axOrderList)
93-
}
94-
}
95-
}
47+
internal fun isAxOrderElement(view: View): Boolean =
48+
view.isFocusable || ScreenReaderFocusableHelper.isScreenReaderFocusable(view)
9649

9750
/**
9851
* Builds an ordered list of views based on accessibility order preferences.
@@ -130,3 +83,60 @@ public object ReactAxOrderHelper {
13083
}
13184
}
13285
}
86+
87+
// Recorded, so a release reaches the held views that have left the order's subtree.
88+
internal class AccessibilityOrderHolds {
89+
private val heldViews: MutableSet<View> = Collections.newSetFromMap(WeakHashMap())
90+
91+
fun disableFocusForSubtree(view: View, axOrderList: List<*>) {
92+
if (!axOrderList.contains(view.getTag(R.id.view_tag_native_id))) {
93+
hold(view)
94+
}
95+
96+
if (view is ViewGroup) {
97+
for (i in 0..<view.childCount) {
98+
disableFocusForSubtree(view.getChildAt(i), axOrderList)
99+
}
100+
}
101+
}
102+
103+
fun restoreFocusability() {
104+
val views = heldViews.toList()
105+
heldViews.clear()
106+
for (view in views) {
107+
val originalFocusability = view.originalFocusability
108+
if (originalFocusability?.holds === this) {
109+
view.setTag(R.id.original_focusability, null)
110+
view.isFocusable = originalFocusability.focusable
111+
ScreenReaderFocusableHelper.setScreenReaderFocusable(
112+
view,
113+
originalFocusability.screenReaderFocusable,
114+
)
115+
}
116+
}
117+
}
118+
119+
private fun hold(view: View) {
120+
view.setTag(
121+
R.id.original_focusability,
122+
view.originalFocusability?.copy(holds = this)
123+
?: OriginalFocusability(
124+
holds = this,
125+
focusable = view.isFocusable,
126+
screenReaderFocusable = ScreenReaderFocusableHelper.isScreenReaderFocusable(view),
127+
),
128+
)
129+
heldViews.add(view)
130+
view.isFocusable = false
131+
ScreenReaderFocusableHelper.setScreenReaderFocusable(view, false)
132+
}
133+
}
134+
135+
private data class OriginalFocusability(
136+
val holds: AccessibilityOrderHolds,
137+
val focusable: Boolean,
138+
val screenReaderFocusable: Boolean,
139+
)
140+
141+
private val View.originalFocusability: OriginalFocusability?
142+
get() = getTag(R.id.original_focusability) as? OriginalFocusability
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager
9+
10+
import android.os.Build
11+
import android.view.View
12+
import android.view.accessibility.AccessibilityNodeInfo
13+
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
14+
import com.facebook.react.R
15+
16+
// Below API 28 View has no screenReaderFocusable, so a tag holds it and node extras carry it.
17+
internal object ScreenReaderFocusableHelper {
18+
fun isScreenReaderFocusable(view: View): Boolean =
19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
20+
view.isScreenReaderFocusable
21+
} else {
22+
view.getTag(R.id.screen_reader_focusable) == true
23+
}
24+
25+
fun setScreenReaderFocusable(view: View, screenReaderFocusable: Boolean) {
26+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
27+
view.isScreenReaderFocusable = screenReaderFocusable
28+
} else {
29+
view.setTag(R.id.screen_reader_focusable, screenReaderFocusable)
30+
}
31+
}
32+
33+
fun initializeAccessibilityNodeInfo(view: View, info: AccessibilityNodeInfo) {
34+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && isScreenReaderFocusable(view)) {
35+
AccessibilityNodeInfoCompat.wrap(info).isScreenReaderFocusable = true
36+
}
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.views.view
9+
10+
import android.view.View
11+
12+
internal data class FocusProps(
13+
val focusable: Boolean? = null,
14+
val accessible: Boolean = false,
15+
val screenReaderFocusable: Boolean = false,
16+
val importantForAccessibility: Int = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO,
17+
) {
18+
val keyboardFocusable: Boolean
19+
get() = focusable ?: accessible
20+
21+
// HTML's focus() reaches an element a negative tabindex keeps out of sequential navigation.
22+
val focusableOnlyFromJS: Boolean
23+
get() = focusable == false
24+
25+
// An accessible view that keyboard navigation passes over stays one element for screen readers.
26+
val isScreenReaderFocusable: Boolean
27+
get() = screenReaderFocusable || (accessible && !keyboardFocusable)
28+
29+
fun isFocusable(isFocused: Boolean): Boolean =
30+
keyboardFocusable || (focusableOnlyFromJS && isFocused)
31+
32+
// Android makes a labelled view important, and a screenReaderFocusable one only from API 35.
33+
fun importantForAccessibility(hasContentDescription: Boolean): Int =
34+
if (
35+
importantForAccessibility == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO &&
36+
(isScreenReaderFocusable || hasContentDescription)
37+
) {
38+
View.IMPORTANT_FOR_ACCESSIBILITY_YES
39+
} else {
40+
importantForAccessibility
41+
}
42+
43+
companion object {
44+
val DEFAULT: FocusProps = FocusProps()
45+
}
46+
}

0 commit comments

Comments
 (0)