Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/react-native/Libraries/Text/TextProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ export type TextPropsAndroid = {
*/
disabled?: ?boolean,

/**
* Whether this `Text` should be focusable with a non-touch input device, eg.
* receive focus with a hardware keyboard. An accessible `Text` is focusable
* unless this is `false`, and accessibility services reach it either way. A
* `selectable` `Text` stays focusable, as selecting text takes focus.
*
* @platform android
*/
focusable?: ?boolean,

/**
* Highlight color of the text when selected.
*
Expand Down
19 changes: 15 additions & 4 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3733,9 +3733,6 @@ public final class com/facebook/react/uimanager/ReactAccessibilityDelegate$Role$
public final class com/facebook/react/uimanager/ReactAxOrderHelper {
public static final field INSTANCE Lcom/facebook/react/uimanager/ReactAxOrderHelper;
public final fun buildAxOrderList (Landroid/view/View;Landroid/view/View;Ljava/util/List;[Landroid/view/View;)V
public static final fun cleanUpAxOrder (Landroid/view/View;)V
public final fun disableFocusForSubtree (Landroid/view/View;Ljava/util/List;)V
public static final fun restoreFocusability (Landroid/view/View;)V
}

public abstract interface class com/facebook/react/uimanager/ReactClippingProhibitedView {
Expand Down Expand Up @@ -6043,6 +6040,7 @@ public class com/facebook/react/views/text/ReactTextView : androidx/appcompat/wi
public fun onAttachedToWindow ()V
protected fun onDraw (Landroid/graphics/Canvas;)V
public final fun onFocusChanged (ZILandroid/graphics/Rect;)V
public fun onInitializeAccessibilityNodeInfo (Landroid/view/accessibility/AccessibilityNodeInfo;)V
protected fun onLayout (ZIIII)V
protected fun onMeasure (II)V
protected fun onSizeChanged (IIII)V
Expand Down Expand Up @@ -6105,7 +6103,10 @@ public class com/facebook/react/views/text/ReactTextViewManager : com/facebook/r
public final fun setDataDetectorType (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
public final fun setDisabled (Lcom/facebook/react/views/text/ReactTextView;Z)V
public final fun setEllipsizeMode (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
public final fun setFocusable (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/Boolean;)V
public final fun setFontSize (Lcom/facebook/react/views/text/ReactTextView;F)V
public synthetic fun setImportantForAccessibility (Landroid/view/View;Ljava/lang/String;)V
public fun setImportantForAccessibility (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
public final fun setIncludeFontPadding (Lcom/facebook/react/views/text/ReactTextView;Z)V
public final fun setLetterSpacing (Lcom/facebook/react/views/text/ReactTextView;F)V
public final fun setLineBreak (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
Expand All @@ -6114,6 +6115,8 @@ public class com/facebook/react/views/text/ReactTextViewManager : com/facebook/r
public synthetic fun setPadding (Landroid/view/View;IIII)V
public fun setPadding (Lcom/facebook/react/views/text/ReactTextView;IIII)V
protected final fun setReactTextViewManagerCallback (Lcom/facebook/react/views/text/ReactTextViewManagerCallback;)V
public synthetic fun setScreenReaderFocusable (Landroid/view/View;Z)V
public fun setScreenReaderFocusable (Lcom/facebook/react/views/text/ReactTextView;Z)V
public final fun setSelectable (Lcom/facebook/react/views/text/ReactTextView;Z)V
public final fun setSelectionColor (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/Integer;)V
public final fun setTextAlignVertical (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
Expand Down Expand Up @@ -6483,7 +6486,9 @@ public class com/facebook/react/views/view/ReactViewGroup : android/view/ViewGro
protected fun onAttachedToWindow ()V
protected fun onConfigurationChanged (Landroid/content/res/Configuration;)V
protected fun onDetachedFromWindow ()V
protected fun onFocusChanged (ZILandroid/graphics/Rect;)V
public fun onHoverEvent (Landroid/view/MotionEvent;)Z
public fun onInitializeAccessibilityNodeInfo (Landroid/view/accessibility/AccessibilityNodeInfo;)V
public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z
protected fun onLayout (ZIIII)V
protected fun onMeasure (II)V
Expand Down Expand Up @@ -6529,6 +6534,8 @@ public class com/facebook/react/views/view/ReactViewManager : com/facebook/react
public fun nextFocusLeft (Lcom/facebook/react/views/view/ReactViewGroup;I)V
public fun nextFocusRight (Lcom/facebook/react/views/view/ReactViewGroup;I)V
public fun nextFocusUp (Lcom/facebook/react/views/view/ReactViewGroup;I)V
public synthetic fun onAfterUpdateTransaction (Landroid/view/View;)V
protected fun onAfterUpdateTransaction (Lcom/facebook/react/views/view/ReactViewGroup;)V
public synthetic fun onDropViewInstance (Landroid/view/View;)V
public fun onDropViewInstance (Lcom/facebook/react/views/view/ReactViewGroup;)V
public synthetic fun prepareToRecycleView (Lcom/facebook/react/uimanager/ThemedReactContext;Landroid/view/View;)Landroid/view/View;
Expand All @@ -6555,15 +6562,19 @@ public class com/facebook/react/views/view/ReactViewManager : com/facebook/react
public fun setExperimentalBackgroundPosition (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;)V
public fun setExperimentalBackgroundRepeat (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;)V
public fun setExperimentalBackgroundSize (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;)V
public fun setFocusable (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
public fun setFocusable (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/Boolean;)V
public fun setHitSlop (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/Dynamic;)V
public synthetic fun setImportantForAccessibility (Landroid/view/View;Ljava/lang/String;)V
public fun setImportantForAccessibility (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;)V
public fun setNativeBackground (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableMap;)V
public fun setNativeForeground (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableMap;)V
public fun setNeedsOffscreenAlphaCompositing (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
public synthetic fun setOpacity (Landroid/view/View;F)V
public fun setOpacity (Lcom/facebook/react/views/view/ReactViewGroup;F)V
public fun setOverflow (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;)V
public fun setPointerEvents (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;)V
public synthetic fun setScreenReaderFocusable (Landroid/view/View;Z)V
public fun setScreenReaderFocusable (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
public fun setTVPreferredFocus (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
public synthetic fun setTransformProperty (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/ReadableArray;)V
protected fun setTransformProperty (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;Lcom/facebook/react/bridge/ReadableArray;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,18 @@ public void setAccessibilityValue(T view, ReadableMap accessibilityValue) {
@ReactProp(name = ViewProps.IMPORTANT_FOR_ACCESSIBILITY)
public void setImportantForAccessibility(
@NonNull T view, @Nullable String importantForAccessibility) {
if (importantForAccessibility == null || importantForAccessibility.equals("auto")) {
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
} else if (importantForAccessibility.equals("yes")) {
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
} else if (importantForAccessibility.equals("no")) {
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
} else if (importantForAccessibility.equals("no-hide-descendants")) {
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
@Nullable
Integer mode =
ImportantForAccessibilityHelper.parseImportantForAccessibility(importantForAccessibility);
if (mode != null) {
view.setImportantForAccessibility(mode);
}
}

@ReactProp(name = ViewProps.SCREEN_READER_FOCUSABLE)
public void setScreenReaderFocusable(@NonNull T view, boolean screenReaderFocusable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
view.setScreenReaderFocusable(screenReaderFocusable);
ReactAxOrderHelper.internal_setScreenReaderFocusable(view, screenReaderFocusable);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.uimanager

import android.view.View

internal object ImportantForAccessibilityHelper {
@JvmStatic
fun parseImportantForAccessibility(importantForAccessibility: String?): Int? =
when (importantForAccessibility) {
null,
"auto" -> View.IMPORTANT_FOR_ACCESSIBILITY_AUTO
"yes" -> View.IMPORTANT_FOR_ACCESSIBILITY_YES
"no" -> View.IMPORTANT_FOR_ACCESSIBILITY_NO
"no-hide-descendants" -> View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,89 +10,42 @@ package com.facebook.react.uimanager
import android.view.View
import android.view.ViewGroup
import com.facebook.react.R
import java.util.Collections
import java.util.WeakHashMap

/**
* Helper object for managing accessibility order in React Native views.
*
* This object provides utilities to manage the accessibility focus order of views by storing and
* restoring focusability states, and building ordered lists of views based on accessibility order
* preferences.
* This object provides utilities to build ordered lists of views based on accessibility order
* preferences, and to write the focus flags of views whose focus an accessibility order holds.
*/
public object ReactAxOrderHelper {
/**
* Cleans up accessibility order state from a view and its children.
*
* This method removes stored focusability states and accessibility order parent references from
* the view hierarchy. It recursively processes all children of ViewGroup instances.
*
* @param view The view from which to clean up accessibility order state
*/
@JvmStatic
public fun cleanUpAxOrder(view: View) {
val originalFocusability = view.getTag(R.id.original_focusability) as Boolean?
// A focus flag set while an accessibility order holds the view's focus is the one it restores.
internal fun setFocusable(view: View, focusable: Boolean) {
val originalFocusability = view.originalFocusability
if (originalFocusability != null) {
view.isFocusable = originalFocusability
}

val axOrderParent = view.getTag(R.id.accessibility_order_parent) as View?
if (axOrderParent != null) {
view.setTag(R.id.accessibility_order_parent, null)
}

if (view is ViewGroup) {
for (i in 0..<view.childCount) {
cleanUpAxOrder(view.getChildAt(i))
}
view.setTag(R.id.original_focusability, originalFocusability.copy(focusable = focusable))
} else {
view.isFocusable = focusable
}
}

/**
* Restores the original focusability state of a view and its children.
*
* This method traverses the view hierarchy and restores the focusability state that was
* previously saved with the `R.id.original_focusability` tag. This is typically used after
* accessibility order operations are complete to return views to their original state.
*
* @param view The view whose focusability state should be restored
*/
@JvmStatic
public fun restoreFocusability(view: View) {
val originalFocusability = view.getTag(R.id.original_focusability) as Boolean?
@JvmName("internal_setScreenReaderFocusable")
internal fun setScreenReaderFocusable(view: View, screenReaderFocusable: Boolean) {
val originalFocusability = view.originalFocusability
if (originalFocusability != null) {
view.isFocusable = originalFocusability
}

if (view is ViewGroup) {
for (i in 0..<view.childCount) {
restoreFocusability(view.getChildAt(i))
}
view.setTag(
R.id.original_focusability,
originalFocusability.copy(screenReaderFocusable = screenReaderFocusable),
)
} else {
ScreenReaderFocusableHelper.setScreenReaderFocusable(view, screenReaderFocusable)
}
}

/**
* Disables focus for all views in the subtree that are not in the accessibility order list.
*
* This method recursively traverses the view hierarchy and disables focusability for views that
* are not included in the provided accessibility order list. It stores the original focusability
* state before modifying it, allowing for later restoration.
*
* @param view The root view of the subtree to process
* @param axOrderList The list of native IDs that should maintain their focusability
*/
public fun disableFocusForSubtree(view: View, axOrderList: MutableList<*>) {
if (!axOrderList.contains(view.getTag(R.id.view_tag_native_id))) {
if (view.getTag(R.id.original_focusability) == null) {
view.setTag(R.id.original_focusability, view.isFocusable)
}
view.isFocusable = false
}

if (view is ViewGroup) {
for (i in 0..<view.childCount) {
disableFocusForSubtree(view.getChildAt(i), axOrderList)
}
}
}
internal fun isAxOrderElement(view: View): Boolean =
view.isFocusable || ScreenReaderFocusableHelper.isScreenReaderFocusable(view)

/**
* Builds an ordered list of views based on accessibility order preferences.
Expand Down Expand Up @@ -130,3 +83,60 @@ public object ReactAxOrderHelper {
}
}
}

// Recorded, so a release reaches the held views that have left the order's subtree.
internal class AccessibilityOrderHolds {
private val heldViews: MutableSet<View> = Collections.newSetFromMap(WeakHashMap())

fun disableFocusForSubtree(view: View, axOrderList: List<*>) {
if (!axOrderList.contains(view.getTag(R.id.view_tag_native_id))) {
hold(view)
}

if (view is ViewGroup) {
for (i in 0..<view.childCount) {
disableFocusForSubtree(view.getChildAt(i), axOrderList)
}
}
}

fun restoreFocusability() {
val views = heldViews.toList()
heldViews.clear()
for (view in views) {
val originalFocusability = view.originalFocusability
if (originalFocusability?.holds === this) {
view.setTag(R.id.original_focusability, null)
view.isFocusable = originalFocusability.focusable
ScreenReaderFocusableHelper.setScreenReaderFocusable(
view,
originalFocusability.screenReaderFocusable,
)
}
}
}

private fun hold(view: View) {
view.setTag(
R.id.original_focusability,
view.originalFocusability?.copy(holds = this)
?: OriginalFocusability(
holds = this,
focusable = view.isFocusable,
screenReaderFocusable = ScreenReaderFocusableHelper.isScreenReaderFocusable(view),
),
)
heldViews.add(view)
view.isFocusable = false
ScreenReaderFocusableHelper.setScreenReaderFocusable(view, false)
}
}

private data class OriginalFocusability(
val holds: AccessibilityOrderHolds,
val focusable: Boolean,
val screenReaderFocusable: Boolean,
)

private val View.originalFocusability: OriginalFocusability?
get() = getTag(R.id.original_focusability) as? OriginalFocusability
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.uimanager

import android.os.Build
import android.view.View
import android.view.accessibility.AccessibilityNodeInfo
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import com.facebook.react.R

// Below API 28 View has no screenReaderFocusable, so a tag holds it and node extras carry it.
internal object ScreenReaderFocusableHelper {
fun isScreenReaderFocusable(view: View): Boolean =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
view.isScreenReaderFocusable
} else {
view.getTag(R.id.screen_reader_focusable) == true
}

fun setScreenReaderFocusable(view: View, screenReaderFocusable: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
view.isScreenReaderFocusable = screenReaderFocusable
} else {
view.setTag(R.id.screen_reader_focusable, screenReaderFocusable)
}
}

fun initializeAccessibilityNodeInfo(view: View, info: AccessibilityNodeInfo) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && isScreenReaderFocusable(view)) {
AccessibilityNodeInfoCompat.wrap(info).isScreenReaderFocusable = true
}
}
}
Loading
Loading