Skip to content

Commit 81a882b

Browse files
Keep focusable={false} Android text out of keyboard navigation
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 #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.
1 parent bb92824 commit 81a882b

13 files changed

Lines changed: 730 additions & 21 deletions

File tree

‎packages/react-native/Libraries/Text/TextProps.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ export type TextPropsAndroid = {
9494
*/
9595
disabled?: ?boolean,
9696

97+
/**
98+
* Whether this `Text` should be focusable with a non-touch input device, eg.
99+
* receive focus with a hardware keyboard. An accessible `Text` is focusable
100+
* unless this is `false`, and accessibility services reach it either way. A
101+
* `selectable` `Text` stays focusable, as selecting text takes focus.
102+
*
103+
* @platform android
104+
*/
105+
focusable?: ?boolean,
106+
97107
/**
98108
* Highlight color of the text when selected.
99109
*

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6040,6 +6040,7 @@ public class com/facebook/react/views/text/ReactTextView : androidx/appcompat/wi
60406040
public fun onAttachedToWindow ()V
60416041
protected fun onDraw (Landroid/graphics/Canvas;)V
60426042
public final fun onFocusChanged (ZILandroid/graphics/Rect;)V
6043+
public fun onInitializeAccessibilityNodeInfo (Landroid/view/accessibility/AccessibilityNodeInfo;)V
60436044
protected fun onLayout (ZIIII)V
60446045
protected fun onMeasure (II)V
60456046
protected fun onSizeChanged (IIII)V
@@ -6102,7 +6103,10 @@ public class com/facebook/react/views/text/ReactTextViewManager : com/facebook/r
61026103
public final fun setDataDetectorType (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
61036104
public final fun setDisabled (Lcom/facebook/react/views/text/ReactTextView;Z)V
61046105
public final fun setEllipsizeMode (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
6106+
public final fun setFocusable (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/Boolean;)V
61056107
public final fun setFontSize (Lcom/facebook/react/views/text/ReactTextView;F)V
6108+
public synthetic fun setImportantForAccessibility (Landroid/view/View;Ljava/lang/String;)V
6109+
public fun setImportantForAccessibility (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
61066110
public final fun setIncludeFontPadding (Lcom/facebook/react/views/text/ReactTextView;Z)V
61076111
public final fun setLetterSpacing (Lcom/facebook/react/views/text/ReactTextView;F)V
61086112
public final fun setLineBreak (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
@@ -6111,6 +6115,8 @@ public class com/facebook/react/views/text/ReactTextViewManager : com/facebook/r
61116115
public synthetic fun setPadding (Landroid/view/View;IIII)V
61126116
public fun setPadding (Lcom/facebook/react/views/text/ReactTextView;IIII)V
61136117
protected final fun setReactTextViewManagerCallback (Lcom/facebook/react/views/text/ReactTextViewManagerCallback;)V
6118+
public synthetic fun setScreenReaderFocusable (Landroid/view/View;Z)V
6119+
public fun setScreenReaderFocusable (Lcom/facebook/react/views/text/ReactTextView;Z)V
61146120
public final fun setSelectable (Lcom/facebook/react/views/text/ReactTextView;Z)V
61156121
public final fun setSelectionColor (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/Integer;)V
61166122
public final fun setTextAlignVertical (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import android.view.MotionEvent
2121
import android.view.View
2222
import android.view.ViewGroup
2323
import android.view.ViewParent
24+
import android.view.accessibility.AccessibilityNodeInfo
2425
import androidx.annotation.ColorInt
2526
import androidx.annotation.DoNotInline
2627
import androidx.annotation.RequiresApi
@@ -30,12 +31,14 @@ import com.facebook.react.common.annotations.UnstableReactNativeAPI
3031
import com.facebook.react.uimanager.BackgroundStyleApplicator
3132
import com.facebook.react.uimanager.ReactCompoundView
3233
import com.facebook.react.uimanager.RootView
34+
import com.facebook.react.uimanager.ScreenReaderFocusableHelper
3335
import com.facebook.react.uimanager.style.Overflow
3436
import com.facebook.react.views.text.internal.span.AnimatedEffectSpan
3537
import com.facebook.react.views.text.internal.span.CanvasEffectSpan
3638
import com.facebook.react.views.text.internal.span.ReactFragmentIndexSpan
3739
import com.facebook.react.views.text.internal.span.ReactLinkSpan
3840
import com.facebook.react.views.text.internal.span.TouchableSpan
41+
import com.facebook.react.views.view.FocusProps
3942
import kotlin.collections.ArrayList
4043
import kotlin.math.roundToInt
4144

@@ -91,6 +94,11 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
9194

9295
@ColorInt var selectionColor: Int? = null
9396

97+
internal var focusProps: FocusProps = FocusProps.DEFAULT
98+
private set
99+
100+
private var focusPropsChanged = false
101+
94102
val text: CharSequence?
95103
// Avoid mangling the getter name, to allow black box E2E tests to read text content via
96104
// reflection. Returns the full logical text, not `layout.text`, which can be an ellipsized
@@ -108,6 +116,8 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
108116
overflow = Overflow.VISIBLE
109117
selection = null
110118
selectionColor = null
119+
focusProps = FocusProps.DEFAULT
120+
focusPropsChanged = false
111121
lastFrameTimeNanos = 0L
112122
preparedLayout = null
113123
hasClickableSpansCache = null
@@ -422,6 +432,7 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
422432
clearSelection()
423433
}
424434
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect)
435+
focusProps.onFocusChanged(this, gainFocus)
425436
val accessibilityDelegateCompat = ViewCompat.getAccessibilityDelegate(this)
426437
if (
427438
accessibilityDelegateCompat != null &&
@@ -431,6 +442,24 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
431442
}
432443
}
433444

445+
internal fun updateFocusProps(focusProps: FocusProps) {
446+
this.focusProps = focusProps
447+
focusPropsChanged = true
448+
}
449+
450+
internal fun applyFocusProps() {
451+
if (!focusPropsChanged) {
452+
return
453+
}
454+
focusPropsChanged = false
455+
focusProps.applyTo(this)
456+
}
457+
458+
override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo) {
459+
super.onInitializeAccessibilityNodeInfo(info)
460+
ScreenReaderFocusableHelper.initializeAccessibilityNodeInfo(this, info)
461+
}
462+
434463
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
435464
val accessibilityDelegateCompat = ViewCompat.getAccessibilityDelegate(this)
436465
val delegateHandled =

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextViewManager.kt‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.facebook.react.module.annotations.ReactModule
1818
import com.facebook.react.uimanager.BackgroundStyleApplicator
1919
import com.facebook.react.uimanager.BaseViewManager
2020
import com.facebook.react.uimanager.IViewGroupManager
21+
import com.facebook.react.uimanager.ImportantForAccessibilityHelper
2122
import com.facebook.react.uimanager.LayoutShadowNode
2223
import com.facebook.react.uimanager.LengthPercentage
2324
import com.facebook.react.uimanager.PointerEvents
@@ -59,6 +60,11 @@ internal class PreparedLayoutTextViewManager :
5960

6061
override fun getName(): String = REACT_CLASS
6162

63+
override fun onAfterUpdateTransaction(view: PreparedLayoutTextView) {
64+
view.applyFocusProps()
65+
super.onAfterUpdateTransaction(view)
66+
}
67+
6268
override fun updateViewAccessibility(view: PreparedLayoutTextView) {
6369
ReactTextViewAccessibilityDelegate.setDelegate(
6470
view,
@@ -108,7 +114,29 @@ internal class PreparedLayoutTextViewManager :
108114

109115
@ReactProp(name = "accessible")
110116
fun setAccessible(view: PreparedLayoutTextView, accessible: Boolean) {
111-
view.isFocusable = accessible
117+
view.updateFocusProps(view.focusProps.copy(accessible = accessible))
118+
}
119+
120+
@ReactProp(name = "focusable")
121+
fun setFocusable(view: PreparedLayoutTextView, focusable: Boolean?) {
122+
view.updateFocusProps(view.focusProps.copy(focusable = focusable))
123+
}
124+
125+
override fun setScreenReaderFocusable(
126+
view: PreparedLayoutTextView,
127+
screenReaderFocusable: Boolean,
128+
) {
129+
view.updateFocusProps(view.focusProps.copy(screenReaderFocusable = screenReaderFocusable))
130+
}
131+
132+
override fun setImportantForAccessibility(
133+
view: PreparedLayoutTextView,
134+
importantForAccessibility: String?,
135+
) {
136+
val mode =
137+
ImportantForAccessibilityHelper.parseImportantForAccessibility(importantForAccessibility)
138+
?: return
139+
view.updateFocusProps(view.focusProps.copy(importantForAccessibility = mode))
112140
}
113141

114142
@ReactProp(name = "selectable", defaultBoolean = false)

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.view.MotionEvent;
2727
import android.view.View;
2828
import android.view.ViewGroup;
29+
import android.view.accessibility.AccessibilityNodeInfo;
2930
import android.widget.TextView;
3031
import androidx.annotation.Nullable;
3132
import androidx.annotation.RequiresApi;
@@ -45,6 +46,7 @@
4546
import com.facebook.react.uimanager.LengthPercentageType;
4647
import com.facebook.react.uimanager.PixelUtil;
4748
import com.facebook.react.uimanager.ReactCompoundView;
49+
import com.facebook.react.uimanager.ScreenReaderFocusableHelper;
4850
import com.facebook.react.uimanager.ViewDefaults;
4951
import com.facebook.react.uimanager.style.BorderRadiusProp;
5052
import com.facebook.react.uimanager.style.BorderStyle;
@@ -55,6 +57,7 @@
5557
import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan;
5658
import com.facebook.react.views.text.internal.span.ReactFragmentIndexSpan;
5759
import com.facebook.react.views.text.internal.span.ReactTagSpan;
60+
import com.facebook.react.views.view.FocusProps;
5861
import com.facebook.yoga.YogaMeasureMode;
5962
import java.lang.reflect.Method;
6063

@@ -84,6 +87,9 @@ public class ReactTextView extends AppCompatTextView implements ReactCompoundVie
8487
private boolean mShouldAdjustSpannableFontSize;
8588
private Overflow mOverflow = Overflow.VISIBLE;
8689

90+
private FocusProps mFocusProps = FocusProps.TEXT_DEFAULT;
91+
private boolean mFocusPropsChanged;
92+
8793
private @Nullable Spannable mSpanned;
8894
// Fitting the font size to the view shrinks the font size spans of mSpanned in place. These are
8995
// the spans it had before the first fit, so that a later fit (when the view's size changes) can
@@ -141,6 +147,8 @@ private void initView() {
141147
mAdjustsFontSizeToFit = false;
142148
mLinkifyMaskType = 0;
143149
mTextIsSelectable = false;
150+
mFocusProps = FocusProps.TEXT_DEFAULT;
151+
mFocusPropsChanged = false;
144152
mShouldAdjustSpannableFontSize = false;
145153
mEllipsizeLocation = TextUtils.TruncateAt.END;
146154
mFontSize = Float.NaN;
@@ -420,7 +428,14 @@ public void setText(ReactTextUpdate update) {
420428
spanned = new SpannableString(spanned);
421429
}
422430
Linkify.addLinks((Spannable) spanned, mLinkifyMaskType);
431+
boolean wasFocused = isFocused();
432+
// A movement method makes TextView focusable, and a window holding no focus outside touch
433+
// mode gives it focus at once.
423434
setMovementMethod(LinkMovementMethod.getInstance());
435+
applyTextFocusProps();
436+
if (!wasFocused && isFocused() && !getAppliedFocusProps().getKeyboardFocusable()) {
437+
clearFocus();
438+
}
424439
}
425440
applyBaseTextAttributes(update.getBaseTextAttributes());
426441
setText(spanned);
@@ -553,6 +568,11 @@ public void setTextIsSelectable(boolean selectable) {
553568
super.setTextIsSelectable(selectable);
554569
}
555570

571+
/* package */ void setSelectable(boolean selectable) {
572+
setTextIsSelectable(selectable);
573+
mFocusPropsChanged = true;
574+
}
575+
556576
@Override
557577
public void onAttachedToWindow() {
558578
super.onAttachedToWindow();
@@ -565,6 +585,8 @@ public void onAttachedToWindow() {
565585
} else {
566586
setTextIsSelectable(false);
567587
}
588+
// TextView's setTextIsSelectable resets the view's focusability.
589+
applyTextFocusProps();
568590
}
569591

570592
@Override
@@ -765,6 +787,38 @@ public PreparedLayout getPreparedLayout() {
765787

766788
public void setLinkifyMask(int mask) {
767789
mLinkifyMaskType = mask;
790+
mFocusPropsChanged = true;
791+
}
792+
793+
/* package */ FocusProps getFocusProps() {
794+
return mFocusProps;
795+
}
796+
797+
/* package */ void updateFocusProps(FocusProps focusProps) {
798+
mFocusProps = focusProps;
799+
mFocusPropsChanged = true;
800+
}
801+
802+
/* package */ void applyFocusProps() {
803+
if (!mFocusPropsChanged) {
804+
return;
805+
}
806+
mFocusPropsChanged = false;
807+
applyTextFocusProps();
808+
}
809+
810+
private void applyTextFocusProps() {
811+
getAppliedFocusProps().applyTo(this);
812+
}
813+
814+
private FocusProps getAppliedFocusProps() {
815+
return mFocusProps.forText(mTextIsSelectable, mLinkifyMaskType > 0);
816+
}
817+
818+
@Override
819+
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
820+
super.onInitializeAccessibilityNodeInfo(info);
821+
ScreenReaderFocusableHelper.INSTANCE.initializeAccessibilityNodeInfo(this, info);
768822
}
769823

770824
@Override
@@ -795,6 +849,7 @@ protected boolean dispatchHoverEvent(MotionEvent event) {
795849
public final void onFocusChanged(
796850
boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
797851
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
852+
getAppliedFocusProps().onFocusChanged(this, gainFocus);
798853
AccessibilityDelegateCompat accessibilityDelegateCompat =
799854
ViewCompat.getAccessibilityDelegate(this);
800855
if (accessibilityDelegateCompat != null

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.kt‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.facebook.react.module.annotations.ReactModule
2828
import com.facebook.react.uimanager.BackgroundStyleApplicator
2929
import com.facebook.react.uimanager.BaseViewManager
3030
import com.facebook.react.uimanager.IViewManagerWithChildren
31+
import com.facebook.react.uimanager.ImportantForAccessibilityHelper
3132
import com.facebook.react.uimanager.LayoutShadowNode
3233
import com.facebook.react.uimanager.LengthPercentage
3334
import com.facebook.react.uimanager.LengthPercentageType
@@ -111,6 +112,7 @@ public constructor(
111112
override fun getShadowNodeClass(): Class<LayoutShadowNode> = LayoutShadowNode::class.java
112113

113114
override fun onAfterUpdateTransaction(view: ReactTextView) {
115+
view.applyFocusProps()
114116
super.onAfterUpdateTransaction(view)
115117
view.updateView()
116118
}
@@ -251,7 +253,26 @@ public constructor(
251253

252254
@ReactProp(name = "accessible")
253255
public fun setAccessible(view: ReactTextView, accessible: Boolean) {
254-
view.isFocusable = accessible
256+
view.updateFocusProps(view.focusProps.copy(accessible = accessible))
257+
}
258+
259+
@ReactProp(name = "focusable")
260+
public fun setFocusable(view: ReactTextView, focusable: Boolean?) {
261+
view.updateFocusProps(view.focusProps.copy(focusable = focusable))
262+
}
263+
264+
override fun setScreenReaderFocusable(view: ReactTextView, screenReaderFocusable: Boolean) {
265+
view.updateFocusProps(view.focusProps.copy(screenReaderFocusable = screenReaderFocusable))
266+
}
267+
268+
override fun setImportantForAccessibility(
269+
view: ReactTextView,
270+
importantForAccessibility: String?,
271+
) {
272+
val mode =
273+
ImportantForAccessibilityHelper.parseImportantForAccessibility(importantForAccessibility)
274+
?: return
275+
view.updateFocusProps(view.focusProps.copy(importantForAccessibility = mode))
255276
}
256277

257278
// maxLines can only be set in master view (block), doesn't really make sense to set in a span
@@ -307,7 +328,7 @@ public constructor(
307328

308329
@ReactProp(name = "selectable")
309330
public fun setSelectable(view: ReactTextView, isSelectable: Boolean) {
310-
view.setTextIsSelectable(isSelectable)
331+
view.setSelectable(isSelectable)
311332
}
312333

313334
@ReactProp(name = "selectionColor", customType = "Color")

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/FocusProps.kt‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.views.view
99

1010
import android.view.View
11+
import com.facebook.react.uimanager.ReactAxOrderHelper
1112

1213
internal data class FocusProps(
1314
val focusable: Boolean? = null,
@@ -40,7 +41,33 @@ internal data class FocusProps(
4041
importantForAccessibility
4142
}
4243

44+
// Android selects text, and moves through its links, only in a view that can take focus.
45+
fun forText(isSelectable: Boolean, detectsLinks: Boolean): FocusProps =
46+
when {
47+
isSelectable -> copy(focusable = true)
48+
detectsLinks && focusable == null -> copy(focusable = true)
49+
else -> this
50+
}
51+
52+
fun applyTo(view: View) {
53+
ReactAxOrderHelper.setFocusable(view, isFocusable(view.isFocused))
54+
ReactAxOrderHelper.setScreenReaderFocusable(view, isScreenReaderFocusable)
55+
view.importantForAccessibility =
56+
importantForAccessibility(hasContentDescription = !view.contentDescription.isNullOrEmpty())
57+
}
58+
59+
fun onFocusChanged(view: View, gainFocus: Boolean) {
60+
if (!gainFocus && focusableOnlyFromJS) {
61+
ReactAxOrderHelper.setFocusable(view, false)
62+
}
63+
}
64+
4365
companion object {
44-
val DEFAULT: FocusProps = FocusProps()
66+
@JvmField val DEFAULT: FocusProps = FocusProps()
67+
68+
// TextView's constructor makes the view important for accessibility.
69+
@JvmField
70+
val TEXT_DEFAULT: FocusProps =
71+
FocusProps(importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_YES)
4572
}
4673
}

0 commit comments

Comments
 (0)