From d87790846bfcacae30810803fb1f5d18a8129406 Mon Sep 17 00:00:00 2001 From: Joe Vilches Date: Fri, 23 May 2025 10:42:37 -0700 Subject: [PATCH] Fixup focusability logic of PreparedLayoutTextView (#51508) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51508 For now, we are opting to not auto-focus `PreparedLayoutTextViews` if they have links. The reason being this would not work well with Text nested in a View which is also accessible. If that Text had links, and was set to focusable, then TalkBack would individually focus that Text, which users may not want. So this diff removes that link detection, and fixes up accessibility in general. Since this isn't a TextView, we need to explicitly set the `text` on the `AccessibilityNodeInfo` object in the delegate so TalkBack know what to annouce. In the future we aim to bring back auto-focusing with links, but only if a screen reader is not on, so that keyboard users can benefit from this. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D75103779 fbshipit-source-id: 05e17f4eb8d5d79ed1d84458f6d5fc6d1571e382 --- .../facebook/react/views/text/PreparedLayoutTextView.kt | 6 ------ .../views/text/ReactTextViewAccessibilityDelegate.kt | 9 +++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt index 95e7b33d314..b23e2d5b190 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt @@ -52,11 +52,6 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context) { clickableSpans = value?.text?.let { filterClickableSpans(it) } ?: emptyList() - // T221698736: This and `accessible` prop can clobber each other, and ShadowTree does not - // know - // about this. Need to figure out desired behavior for controlling implicit focusability. - isFocusable = clickableSpans.isNotEmpty() - field = value invalidate() } @@ -92,7 +87,6 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context) { public fun recycleView(): Unit { initView() BackgroundStyleApplicator.reset(this) - isFocusable = false overflow = Overflow.HIDDEN } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index 3cefcb95012..47abeda2009 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -174,6 +174,15 @@ internal class ReactTextViewAccessibilityDelegate : ReactAccessibilityDelegate { } } + override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfoCompat) { + super.onInitializeAccessibilityNodeInfo(host, info) + // PreparedLayoutTextView isn't actually a TextView, so we need to teach it about its text that + // it is holding so TalkBack knows what to announce when focusing it. + if (host is PreparedLayoutTextView) { + info.text = host.text + } + } + @Suppress("DEPRECATION") override fun onPopulateNodeForVirtualView(virtualViewId: Int, node: AccessibilityNodeInfoCompat) { // If we get an invalid virtualViewId for some reason (which is known to happen in API 19 and