Skip to content

Commit 9845b5e

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 Tab, Shift+Tab and the arrow keys of a hardware keyboard stopped on it. Text had no focusable prop to take it out, so a pressable label between two inputs was always an extra Tab stop. The text view managers now take focusable, and ReactTextView and PreparedLayoutTextView skip an excluded text in addFocusables and requestFocus while isFocusable stays with accessible, so accessibility services still reach it. A text that holds focus stays a candidate, an inline view inside one stays reachable, and a touch still focuses a selectable text, which text selection needs. TextProps and the legacy Text types declare the prop; the base view config already sends it. On Android a Paragraph's KeyboardFocusable trait takes focusable before accessible and selectable, as a View's does, so a scroll view's focus search, which FocusOrderingHelper answers from that trait, passes over an excluded text too.
1 parent ec5bb84 commit 9845b5e

11 files changed

Lines changed: 316 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ 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.
101+
*
102+
* @platform android
103+
*/
104+
focusable?: ?boolean,
105+
97106
/**
98107
* Highlight color of the text when selected.
99108
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6017,6 +6017,7 @@ public final class com/facebook/react/views/text/ReactFontManager$Companion {
60176017

60186018
public class com/facebook/react/views/text/ReactTextView : androidx/appcompat/widget/AppCompatTextView, com/facebook/react/uimanager/ReactCompoundView {
60196019
public fun <init> (Landroid/content/Context;)V
6020+
public fun addFocusables (Ljava/util/ArrayList;II)V
60206021
protected fun dispatchHoverEvent (Landroid/view/MotionEvent;)Z
60216022
public fun dispatchKeyEvent (Landroid/view/KeyEvent;)Z
60226023
public fun getSpanned ()Landroid/text/Spannable;
@@ -6028,6 +6029,7 @@ public class com/facebook/react/views/text/ReactTextView : androidx/appcompat/wi
60286029
protected fun onMeasure (II)V
60296030
protected fun onSizeChanged (IIII)V
60306031
public fun reactTagForTouch (FF)I
6032+
public fun requestFocus (ILandroid/graphics/Rect;)Z
60316033
public fun setAdjustFontSizeToFit (Z)V
60326034
public fun setBackgroundColor (I)V
60336035
public fun setBorderColor (ILjava/lang/Integer;)V
@@ -6086,6 +6088,7 @@ public class com/facebook/react/views/text/ReactTextViewManager : com/facebook/r
60866088
public final fun setDataDetectorType (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
60876089
public final fun setDisabled (Lcom/facebook/react/views/text/ReactTextView;Z)V
60886090
public final fun setEllipsizeMode (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
6091+
public final fun setFocusable (Lcom/facebook/react/views/text/ReactTextView;Z)V
60896092
public final fun setFontSize (Lcom/facebook/react/views/text/ReactTextView;F)V
60906093
public final fun setIncludeFontPadding (Lcom/facebook/react/views/text/ReactTextView;Z)V
60916094
public final fun setLetterSpacing (Lcom/facebook/react/views/text/ReactTextView;F)V

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
9191

9292
@ColorInt var selectionColor: Int? = null
9393

94+
/**
95+
* Set from the `focusable` prop. [isFocusable] also carries `accessible`, so a text excluded here
96+
* stays focusable for accessibility services while keyboard navigation passes over it.
97+
*/
98+
var excludedFromKeyboardFocus: Boolean = false
99+
94100
val text: CharSequence?
95101
// Avoid mangling the getter name, to allow black box E2E tests to read text content via
96102
// reflection. Returns the full logical text, not `layout.text`, which can be an ellipsized
@@ -108,6 +114,7 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
108114
overflow = Overflow.VISIBLE
109115
selection = null
110116
selectionColor = null
117+
excludedFromKeyboardFocus = false
111118
lastFrameTimeNanos = 0L
112119
preparedLayout = null
113120
hasClickableSpansCache = null
@@ -431,6 +438,23 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re
431438
}
432439
}
433440

441+
override fun addFocusables(views: ArrayList<View>, direction: Int, focusableMode: Int) {
442+
val firstAddedIndex = views.size
443+
super.addFocusables(views, direction, focusableMode)
444+
// A focused view stays a candidate so that focus search can move on from it.
445+
if (excludedFromKeyboardFocus && !isFocused) {
446+
views.subList(firstAddedIndex, views.size).remove(this)
447+
}
448+
}
449+
450+
override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean {
451+
if (!excludedFromKeyboardFocus) {
452+
return super.requestFocus(direction, previouslyFocusedRect)
453+
}
454+
return descendantFocusability != FOCUS_BLOCK_DESCENDANTS &&
455+
onRequestFocusInDescendants(direction, previouslyFocusedRect)
456+
}
457+
434458
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
435459
val accessibilityDelegateCompat = ViewCompat.getAccessibilityDelegate(this)
436460
val delegateHandled =

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ internal class PreparedLayoutTextViewManager :
124124
view.isFocusable = accessible
125125
}
126126

127+
@ReactProp(name = "focusable", defaultBoolean = true)
128+
fun setFocusable(view: PreparedLayoutTextView, focusable: Boolean) {
129+
view.excludedFromKeyboardFocus = !focusable
130+
}
131+
127132
@ReactProp(name = "selectable", defaultBoolean = false)
128133
fun setSelectable(view: PreparedLayoutTextView, isSelectable: Boolean) {
129134
check(!isSelectable) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.facebook.react.views.text.internal.span.ReactTagSpan;
5858
import com.facebook.yoga.YogaMeasureMode;
5959
import java.lang.reflect.Method;
60+
import java.util.ArrayList;
6061

6162
@Nullsafe(Nullsafe.Mode.LOCAL)
6263
public class ReactTextView extends AppCompatTextView implements ReactCompoundView {
@@ -84,6 +85,13 @@ public class ReactTextView extends AppCompatTextView implements ReactCompoundVie
8485
private boolean mShouldAdjustSpannableFontSize;
8586
private Overflow mOverflow = Overflow.VISIBLE;
8687

88+
/**
89+
* Set from the `focusable` prop. {@link #isFocusable()} also carries `accessible`, so a text
90+
* excluded here stays focusable for accessibility services while keyboard navigation passes over
91+
* it.
92+
*/
93+
private boolean mExcludedFromKeyboardFocus;
94+
8795
private @Nullable Spannable mSpanned;
8896
// Fitting the font size to the view shrinks the font size spans of mSpanned in place. These are
8997
// the spans it had before the first fit, so that a later fit (when the view's size changes) can
@@ -141,6 +149,7 @@ private void initView() {
141149
mAdjustsFontSizeToFit = false;
142150
mLinkifyMaskType = 0;
143151
mTextIsSelectable = false;
152+
mExcludedFromKeyboardFocus = false;
144153
mShouldAdjustSpannableFontSize = false;
145154
mEllipsizeLocation = TextUtils.TruncateAt.END;
146155
mFontSize = Float.NaN;
@@ -758,6 +767,28 @@ public void setLinkifyMask(int mask) {
758767
mLinkifyMaskType = mask;
759768
}
760769

770+
/* package */ void setExcludedFromKeyboardFocus(boolean excludedFromKeyboardFocus) {
771+
mExcludedFromKeyboardFocus = excludedFromKeyboardFocus;
772+
}
773+
774+
@Override
775+
public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
776+
// A focused view stays a candidate so that focus search can move on from it.
777+
if (mExcludedFromKeyboardFocus && !isFocused()) {
778+
return;
779+
}
780+
super.addFocusables(views, direction, focusableMode);
781+
}
782+
783+
@Override
784+
public boolean requestFocus(int direction, @Nullable Rect previouslyFocusedRect) {
785+
// Selecting text takes focus from a touch, which `focusable` leaves alone.
786+
if (mExcludedFromKeyboardFocus && !isInTouchMode()) {
787+
return false;
788+
}
789+
return super.requestFocus(direction, previouslyFocusedRect);
790+
}
791+
761792
@Override
762793
protected boolean dispatchHoverEvent(MotionEvent event) {
763794
// if this view has an accessibility delegate set, and that delegate supports virtual view

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ public constructor(
268268
view.isFocusable = accessible
269269
}
270270

271+
@ReactProp(name = "focusable", defaultBoolean = true)
272+
public fun setFocusable(view: ReactTextView, focusable: Boolean) {
273+
view.setExcludedFromKeyboardFocus(!focusable)
274+
}
275+
271276
// maxLines can only be set in master view (block), doesn't really make sense to set in a span
272277
@ReactProp(name = ViewProps.NUMBER_OF_LINES, defaultInt = ViewDefaults.NUMBER_OF_LINES)
273278
public fun setNumberOfLines(view: ReactTextView, numberOfLines: Int) {
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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.text
9+
10+
import android.app.Activity
11+
import android.content.Context
12+
import android.view.FocusFinder
13+
import android.view.View
14+
import android.view.ViewGroup
15+
import androidx.test.platform.app.InstrumentationRegistry
16+
import com.facebook.react.bridge.JavaOnlyMap
17+
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
18+
import com.facebook.react.uimanager.ReactStylesDiffMap
19+
import com.facebook.react.uimanager.ViewManager
20+
import com.facebook.react.views.view.ReactViewGroup
21+
import org.assertj.core.api.Assertions.assertThat
22+
import org.junit.After
23+
import org.junit.Before
24+
import org.junit.Test
25+
import org.junit.runner.RunWith
26+
import org.robolectric.Robolectric
27+
import org.robolectric.RobolectricTestRunner
28+
29+
abstract class TextKeyboardFocusTest<T : View> {
30+
31+
protected lateinit var context: Context
32+
private lateinit var manager: ViewManager<T, *>
33+
34+
protected abstract fun createManager(): ViewManager<T, *>
35+
36+
protected abstract fun createTextView(context: Context): T
37+
38+
protected abstract fun recycle(text: T)
39+
40+
@Before
41+
fun setUp() {
42+
ReactNativeFeatureFlagsForTests.setUp()
43+
context = Robolectric.buildActivity(Activity::class.java).create().get()
44+
manager = createManager()
45+
}
46+
47+
@Test
48+
fun `focusable false - keeps an accessible text out of keyboard focus search`() {
49+
val root = ReactViewGroup(context)
50+
val leading = focusableView(top = 0)
51+
val text = accessibleText(top = 10, "focusable", false)
52+
val trailing = focusableView(top = 20)
53+
root.addView(leading)
54+
root.addView(text)
55+
root.addView(trailing)
56+
57+
assertThat(collectKeyboardFocusables(root)).containsExactly(leading, trailing)
58+
// Accessibility services still treat the text as one focusable element.
59+
assertThat(text.isFocusable).isTrue()
60+
}
61+
62+
@Test
63+
fun `focusable false - default focus passes over an excluded text`() {
64+
val root = ReactViewGroup(context)
65+
val text = accessibleText(top = 0, "focusable", false)
66+
val trailing = focusableView(top = 10)
67+
root.addView(text)
68+
root.addView(trailing)
69+
70+
assertThat(root.requestFocus(View.FOCUS_DOWN)).isTrue()
71+
72+
assertThat(text.isFocused).isFalse()
73+
assertThat(trailing.isFocused).isTrue()
74+
}
75+
76+
@Test
77+
fun `focusable false - keyboard focus moves on from an excluded text that holds focus`() {
78+
val root = ReactViewGroup(context)
79+
val leading = focusableView(top = 0)
80+
val text = accessibleText(top = 10)
81+
val trailing = focusableView(top = 20)
82+
root.addView(leading)
83+
root.addView(text)
84+
root.addView(trailing)
85+
Robolectric.buildActivity(Activity::class.java).setup().get().setContentView(root)
86+
assertThat(text.requestFocus()).isTrue()
87+
88+
applyProps(text, "focusable", false)
89+
90+
assertThat(FocusFinder.getInstance().findNextFocus(root, text, View.FOCUS_FORWARD))
91+
.isSameAs(trailing)
92+
assertThat(FocusFinder.getInstance().findNextFocus(root, text, View.FOCUS_BACKWARD))
93+
.isSameAs(leading)
94+
}
95+
96+
@Test
97+
fun `focusable true - returns a text to keyboard focus search`() {
98+
val root = ReactViewGroup(context)
99+
val text = accessibleText(top = 0, "focusable", false)
100+
root.addView(text)
101+
assertThat(collectKeyboardFocusables(root)).isEmpty()
102+
103+
applyProps(text, "focusable", true)
104+
105+
assertThat(collectKeyboardFocusables(root)).containsExactly(text)
106+
}
107+
108+
@Test
109+
fun `focusable removed - returns a text to keyboard focus search`() {
110+
val root = ReactViewGroup(context)
111+
val text = accessibleText(top = 0, "focusable", false)
112+
root.addView(text)
113+
assertThat(collectKeyboardFocusables(root)).isEmpty()
114+
115+
applyProps(text, "focusable", null)
116+
117+
assertThat(collectKeyboardFocusables(root)).containsExactly(text)
118+
}
119+
120+
@Test
121+
fun `focusable false - recycling a text returns it to keyboard focus search`() {
122+
val root = ReactViewGroup(context)
123+
val text = accessibleText(top = 0, "focusable", false)
124+
root.addView(text)
125+
assertThat(collectKeyboardFocusables(root)).isEmpty()
126+
root.removeView(text)
127+
128+
recycle(text)
129+
applyProps(text, "accessible", true)
130+
root.addView(text)
131+
132+
assertThat(collectKeyboardFocusables(root)).containsExactly(text)
133+
}
134+
135+
protected fun accessibleText(top: Int, vararg props: Any?): T =
136+
createTextView(context).apply {
137+
layout(0, top, 100, top + 10)
138+
applyProps(this, "accessible", true, *props)
139+
}
140+
141+
protected fun applyProps(text: T, vararg keysAndValues: Any?) {
142+
manager.updateProperties(text, ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)))
143+
}
144+
145+
protected fun focusableView(top: Int): View =
146+
View(context).apply {
147+
isFocusable = true
148+
layout(0, top, 100, top + 10)
149+
}
150+
151+
protected fun collectKeyboardFocusables(root: ViewGroup): List<View> {
152+
val focusables = ArrayList<View>()
153+
root.addFocusables(focusables, View.FOCUS_FORWARD, View.FOCUSABLES_ALL)
154+
return focusables
155+
}
156+
}
157+
158+
@RunWith(RobolectricTestRunner::class)
159+
class ReactTextViewKeyboardFocusTest : TextKeyboardFocusTest<ReactTextView>() {
160+
161+
override fun createManager(): ViewManager<ReactTextView, *> = ReactTextViewManager()
162+
163+
override fun createTextView(context: Context): ReactTextView = ReactTextView(context)
164+
165+
override fun recycle(text: ReactTextView) {
166+
text.recycleView()
167+
}
168+
169+
@After
170+
fun tearDown() {
171+
InstrumentationRegistry.getInstrumentation().setInTouchMode(false)
172+
}
173+
174+
@Test
175+
fun `focusable false - a touch still focuses an excluded selectable text`() {
176+
InstrumentationRegistry.getInstrumentation().setInTouchMode(true)
177+
val root = ReactViewGroup(context)
178+
val text = accessibleText(top = 0, "focusable", false, "selectable", true)
179+
root.addView(text)
180+
Robolectric.buildActivity(Activity::class.java).setup().get().setContentView(root)
181+
assertThat(text.isInTouchMode).isTrue()
182+
183+
assertThat(text.requestFocus()).isTrue()
184+
}
185+
}
186+
187+
@RunWith(RobolectricTestRunner::class)
188+
internal class PreparedLayoutTextViewKeyboardFocusTest :
189+
TextKeyboardFocusTest<PreparedLayoutTextView>() {
190+
191+
override fun createManager(): ViewManager<PreparedLayoutTextView, *> =
192+
PreparedLayoutTextViewManager()
193+
194+
override fun createTextView(context: Context): PreparedLayoutTextView =
195+
PreparedLayoutTextView(context)
196+
197+
override fun recycle(text: PreparedLayoutTextView) {
198+
text.recycleView()
199+
}
200+
201+
@Test
202+
fun `focusable false - keeps the inline views of an excluded text in keyboard focus search`() {
203+
val root = ReactViewGroup(context)
204+
val text = accessibleText(top = 0, "focusable", false)
205+
val inlineView = focusableView(top = 0)
206+
text.addView(inlineView)
207+
root.addView(text)
208+
209+
assertThat(collectKeyboardFocusables(root)).containsExactly(inlineView)
210+
}
211+
}

‎packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ const char ParagraphComponentName[] = "Paragraph";
4040

4141
void ParagraphShadowNode::initialize() noexcept {
4242
#ifdef ANDROID
43-
if (getConcreteProps().isSelectable) {
43+
// The Android text views' keyboard focus takes `focusable` over `accessible`
44+
// and `selectable`.
45+
const auto& props = getConcreteProps();
46+
if (props.focusable.has_value()) {
47+
if (*props.focusable) {
48+
traits_.set(ShadowNodeTraits::Trait::KeyboardFocusable);
49+
} else {
50+
traits_.unset(ShadowNodeTraits::Trait::KeyboardFocusable);
51+
}
52+
} else if (props.isSelectable) {
4453
traits_.set(ShadowNodeTraits::Trait::KeyboardFocusable);
4554
}
4655
#endif

0 commit comments

Comments
 (0)