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
5 changes: 4 additions & 1 deletion apps/example/src/TypographyScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export function TypographyScreen() {
<Text fontSize={14} color="gray" layout={{ marginTop: 12, marginBottom: 12 }}>Tap the controls to change the font and line limit.</Text>
<Text
gestureRecognizers={fontToggle}
role="button"
role="switch"
aria-label="Abel font"
aria-checked={customFont}
color="#2f7cf6"
layout={{ paddingVertical: 10 }}
>
Expand All @@ -57,6 +59,7 @@ export function TypographyScreen() {
<Text
gestureRecognizers={linesToggle}
role="button"
aria-expanded={expanded}
color="#2f7cf6"
layout={{ paddingVertical: 10 }}
>
Expand Down
18 changes: 18 additions & 0 deletions docs/content/docs/accessibility.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ The roles use the web's ARIA names: `alert`, `button`, `checkbox`, `combobox`, `

On iOS each role maps to traits, and a role replaces the view's own traits, such as a label's static text trait. On Android a role reports the matching widget class, such as `Button` or `CheckBox`. `heading` marks the node as a heading, and `link` and `tab` set its role description. An `alert` is also an assertive live region on Android, so TalkBack announces its changes. Other roles without a matching widget, such as `menu` or `toolbar`, don't change the node on Android.

## Report state

The state props tell the screen reader what a view is doing: `aria-disabled`, `aria-selected`, `aria-checked` (`true`, `false` or `'mixed'`), `aria-expanded` and `aria-busy`. Pair them with a role, such as `checkbox` with `aria-checked`.

```tsx
<View
accessible
role="checkbox"
aria-checked={done}
gestureRecognizers={[toggleTap]}
>
<Text>Water the plants</Text>
</View>
```

`aria-disabled` only changes what the screen reader says. It doesn't block touches.

## Props

| Prop | Type | iOS | Android |
Expand All @@ -63,3 +80,4 @@ On iOS each role maps to traits, and a role replaces the view's own traits, such
| `aria-label` | `string` | `accessibilityLabel` | `contentDescription` |
| `accessibilityHint` | `string` | `accessibilityHint` | Node hint text |
| `role` | `Role` | `accessibilityTraits` | Node class name, heading, or role description |
| `aria-disabled`, `aria-selected`, `aria-checked`, `aria-expanded`, `aria-busy` | `boolean`, or `'mixed'` for `aria-checked` | Traits, value, and expanded status on iOS 18 | Node enabled, selected, checked, expand actions, state description |
2 changes: 1 addition & 1 deletion docs/content/docs/components/view/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ Pass `gestureRecognizers` to attach touch handlers. The `ref` exposes the native
| `style` | Style object | Accepts shared styles, a linear-gradient `background`, and an ordered `boxShadow` array. Each shadow takes `offsetX`, `offsetY`, `color`, and optional `blurRadius`, `spreadDistance`, and `inset`. Shadows require Android API 31+ on Android. [Style properties](/docs/layout#style-properties) |
| `gestureRecognizers` | `GestureRecognizer[]` | Attach these recognizers to the native view. [Gestures](/docs/gestures) |
| `isUserInteractionEnabled` | `boolean` | Set to `false` to disable touch interaction on the view. |
| `accessible`, `aria-label`, `accessibilityHint`, `role` | Accessibility props | Describe the view to screen readers. [Accessibility](/docs/accessibility) |
| `accessible`, `aria-label`, `accessibilityHint`, `role`, `aria-disabled`, `aria-selected`, `aria-checked`, `aria-expanded`, `aria-busy` | Accessibility props | Describe the view to screen readers. [Accessibility](/docs/accessibility) |
| `ref` | React ref | Access the native container as `ViewRef`. |
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,37 @@ public import UIKit
/// Each hybrid view stores one so the props can be read back and cleared.
public struct AccessibilityAttributes {
var role: AccessibilityRole?
var disabled = false
var selected = false
var checked: AccessibilityCheckedState?
var expanded: Bool?
var busy = false
/// The traits the view had before a prop first changed them, restored when the props are cleared.
var defaultTraits: UIAccessibilityTraits?

var traits: UIAccessibilityTraits {
var traits = role?.traits ?? defaultTraits ?? []
if disabled { traits.insert(.notEnabled) }
if selected { traits.insert(.selected) }
return traits
}

/// The state VoiceOver reads after the label. UIKit has no traits for these.
var stateValue: String? {
var parts: [String] = []
switch checked {
case .checked: parts.append("checked")
case .unchecked: parts.append("unchecked")
case .mixed: parts.append("mixed")
case nil: break
}
if #unavailable(iOS 18.0), let expanded {
parts.append(expanded ? "expanded" : "collapsed")
}
if busy { parts.append("busy") }
return parts.isEmpty ? nil : parts.joined(separator: ", ")
}

public init() {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,64 @@ public extension View {
}
}

var ariaDisabled: Bool {
get { accessibilityAttributes.disabled }
set {
accessibilityAttributes.disabled = newValue
updateAccessibilityTraits()
}
}

var ariaSelected: Bool {
get { accessibilityAttributes.selected }
set {
accessibilityAttributes.selected = newValue
updateAccessibilityTraits()
}
}

var ariaChecked: AccessibilityCheckedState? {
get { accessibilityAttributes.checked }
set {
accessibilityAttributes.checked = newValue
updateAccessibilityValue()
}
}

var ariaExpanded: Bool? {
get { accessibilityAttributes.expanded }
set {
accessibilityAttributes.expanded = newValue
if #available(iOS 18.0, *) {
switch newValue {
case true?: view.accessibilityExpandedStatus = .expanded
case false?: view.accessibilityExpandedStatus = .collapsed
case nil: view.accessibilityExpandedStatus = .unsupported
}
} else {
updateAccessibilityValue()
}
}
}

var ariaBusy: Bool {
get { accessibilityAttributes.busy }
set {
accessibilityAttributes.busy = newValue
updateAccessibilityValue()
}
}

/// A role replaces the view's own traits, such as a label's static text trait.
private func updateAccessibilityTraits() {
let defaultTraits = accessibilityAttributes.defaultTraits ?? view.accessibilityTraits
accessibilityAttributes.defaultTraits = defaultTraits
view.accessibilityTraits = accessibilityAttributes.role?.traits ?? defaultTraits
if accessibilityAttributes.defaultTraits == nil {
accessibilityAttributes.defaultTraits = view.accessibilityTraits
}
view.accessibilityTraits = accessibilityAttributes.traits
}

private func updateAccessibilityValue() {
view.accessibilityValue = accessibilityAttributes.stateValue
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.ViewGroup
import android.view.accessibility.AccessibilityEvent
import androidx.core.view.AccessibilityDelegateCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat
import androidx.core.view.accessibility.AccessibilityNodeProviderCompat

/**
Expand All @@ -17,11 +18,43 @@ internal class AccessibilityNodeDelegate(original: AccessibilityDelegateCompat?)

var hint: String? = null
var role: AccessibilityRole? = null
var disabled = false
var selected = false
var checked: AccessibilityCheckedState? = null
var expanded: Boolean? = null
var busy = false

override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfoCompat) {
base.onInitializeAccessibilityNodeInfo(host, info)
hint?.let { info.hintText = it }
role?.let { applyRole(it, info) }
applyState(info)
}

private fun applyState(info: AccessibilityNodeInfoCompat) {
if (disabled) info.isEnabled = false
if (selected) info.isSelected = true
checked?.let {
info.isCheckable = true
info.isChecked = it == AccessibilityCheckedState.CHECKED
}
expanded?.let {
val action = if (it) AccessibilityActionCompat.ACTION_COLLAPSE else AccessibilityActionCompat.ACTION_EXPAND
info.addAction(action)
}
// Android has no node fields for these, so TalkBack reads them from the state description.
// A state description replaces TalkBack's own checked announcement, so it repeats it.
val parts = mutableListOf<String>()
if (checked == AccessibilityCheckedState.MIXED) parts.add("mixed")
expanded?.let { parts.add(if (it) "expanded" else "collapsed") }
if (busy) parts.add("busy")
if (parts.isEmpty()) return
when (checked) {
AccessibilityCheckedState.CHECKED -> parts.add(0, "checked")
AccessibilityCheckedState.UNCHECKED -> parts.add(0, "not checked")
else -> {}
}
info.stateDescription = parts.joinToString(", ")
}

// TalkBack names a node's kind from the widget class it reports. Roles without a matching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ interface ViewOperations {
var ariaLabel: String?
var accessibilityHint: String?
var role: AccessibilityRole?
var ariaDisabled: Boolean
var ariaSelected: Boolean
var ariaChecked: AccessibilityCheckedState?
var ariaExpanded: Boolean?
var ariaBusy: Boolean

fun setBackgroundColor(color: Double?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.LayerDrawable
import android.view.View
import android.view.ViewGroup
import android.view.accessibility.AccessibilityEvent
import android.widget.TextView
import androidx.annotation.Keep
import androidx.core.view.ViewCompat
Expand Down Expand Up @@ -404,6 +405,44 @@ class ViewState(
}
}

override var ariaDisabled: Boolean
get() = accessibilityNodeDelegate?.disabled ?: false
set(value) {
updateAccessibilityState(!value) { it.disabled = value }
}

override var ariaSelected: Boolean
get() = accessibilityNodeDelegate?.selected ?: false
set(value) {
updateAccessibilityState(!value) { it.selected = value }
}

override var ariaChecked: AccessibilityCheckedState?
get() = accessibilityNodeDelegate?.checked
set(value) {
updateAccessibilityState(value == null) { it.checked = value }
}

override var ariaExpanded: Boolean?
get() = accessibilityNodeDelegate?.expanded
set(value) {
updateAccessibilityState(value == null) { it.expanded = value }
}

override var ariaBusy: Boolean
get() = accessibilityNodeDelegate?.busy ?: false
set(value) {
updateAccessibilityState(!value) { it.busy = value }
}

// State changes while a screen reader is on the view, so it re-reads the node.
// sendAccessibilityEvent returns early when no accessibility service is running.
private inline fun updateAccessibilityState(clearing: Boolean, update: (AccessibilityNodeDelegate) -> Unit) {
if (accessibilityNodeDelegate == null && clearing) return
updateAccessibilityNode(clearing, update)
view.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED)
}

// Installed the first time a prop needs node info, so other views keep their default delegate.
private var accessibilityNodeDelegate: AccessibilityNodeDelegate? = null

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading