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
36 changes: 27 additions & 9 deletions apps/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useSafeAreaInsets,
} from "nitro-ui-renderer";
import { Text } from './Text';
import { useLayoutEffect, useMemo } from "react";
import { useLayoutEffect, useMemo, useState } from "react";
import {
createBottomTabNavigator,
createStackNavigator,
Expand Down Expand Up @@ -59,15 +59,33 @@ const TypographyStack = createStackNavigator<{ Typography: undefined }>();

function ModalScreen() {
const navigation = useNavigation<StackNavigationProp<Routes>>();
const [confirmingClose, setConfirmingClose] = useState(false);
return (
<ScrollView>
<ScrollViewContent>
<Item text="Open another modal" onTap={() => navigation.push('Modal')} />
<Item text="Close" onTap={() => navigation.goBack()} />
<Item text="Close all" onTap={() => navigation.popToTop()} />
<Item text="Replace" onTap={() => navigation.replace('Modal')} />
</ScrollViewContent>
</ScrollView>
<View layout={{ flex: 1 }}>
<ScrollView layout={{ flex: 1 }}>
<ScrollViewContent>
<Item text="Open another modal" onTap={() => navigation.push('Modal')} />
<Item text="Close" onTap={() => navigation.goBack()} />
<Item text="Close all" onTap={() => setConfirmingClose(true)} />
<Item text="Replace" onTap={() => navigation.replace('Modal')} />
</ScrollViewContent>
</ScrollView>
{confirmingClose && (
// Screen readers skip the list behind this overlay while it's shown.
<View
aria-modal
aria-label="Close all modals?"
layout={{ position: 'absolute', top: 0, right: 0, bottom: 0, left: 0, justifyContent: 'center', padding: 24 }}
style={{ background: 'rgba(0, 0, 0, 0.4)' }}
>
<View layout={{ padding: 16 }} style={{ background: 'rgba(250, 250, 250, 1)', cornerRadius: 12 }}>
<Text role="heading" color="black" layout={{ marginBottom: 12 }}>Close all modals?</Text>
<Item text="Close all" onTap={() => navigation.popToTop()} />
<Item text="Cancel" onTap={() => setConfirmingClose(false)} />
</View>
</View>
)}
</View>
);
}

Expand Down
22 changes: 22 additions & 0 deletions docs/content/docs/accessibility.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ const actions = [

The renderer compares actions by name and label, so an inline array or handler doesn't call native on every render. On iOS, `View` and `Text` handle the standard actions. Other components get custom actions only.

## Contain a custom alert

`aria-modal` makes screen readers ignore the view's siblings while it's shown, so focus stays inside a custom alert or overlay. Render the view as a sibling of the content it covers. On Android, the view's `aria-label` also becomes its pane title, which TalkBack announces when it appears.

```tsx
<View layout={{ flex: 1 }}>
<Inbox />
{confirming && (
<View
aria-modal
aria-label="Delete message?"
layout={{ position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 }}
>
<ConfirmDialog />
</View>
)}
</View>
```

Navigation sheets and modal screens are already modal to screen readers on both platforms.

## Props

| Prop | Type | iOS | Android |
Expand All @@ -141,3 +162,4 @@ The renderer compares actions by name and label, so an inline array or handler d
| `aria-hidden` | `boolean` | `accessibilityElementsHidden` | Important for accessibility, no, hiding descendants |
| `aria-valuemin`, `aria-valuemax`, `aria-valuenow`, `aria-valuetext` | `number`, or `string` for `aria-valuetext` | `accessibilityValue` | Node range info, or state description for `aria-valuetext` |
| `accessibilityActions`, `onAccessibilityAction` | `AccessibilityAction[]`, function | Standard actions and `accessibilityCustomActions` | Node actions |
| `aria-modal` | `boolean` | `accessibilityViewIsModal` | Hides siblings from accessibility, pane title |
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`, `aria-disabled`, `aria-selected`, `aria-checked`, `aria-expanded`, `aria-busy`, `aria-hidden`, `aria-valuemin`, `aria-valuemax`, `aria-valuenow`, `aria-valuetext`, `accessibilityActions`, `onAccessibilityAction` | 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`, `aria-hidden`, `aria-valuemin`, `aria-valuemax`, `aria-valuenow`, `aria-valuetext`, `accessibilityActions`, `onAccessibilityAction`, `aria-modal` | 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 @@ -374,6 +374,11 @@ public extension View {
}
}

var ariaModal: Bool {
get { view.accessibilityViewIsModal }
set { view.accessibilityViewIsModal = newValue }
}

var ariaHidden: Bool {
get { view.accessibilityElementsHidden }
set { view.accessibilityElementsHidden = newValue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface ViewOperations {
var ariaValueText: String?
var accessibilityActions: Array<AccessibilityAction>?
var onAccessibilityAction: Callback<(actionName: String) -> Unit>?
var ariaModal: Boolean

fun setBackgroundColor(color: Double?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,59 @@ class ViewState(
updateImportantForAccessibility()
}

// Like iOS, a modal view hides its siblings while it's attached. Props are set before the view is
// added to its parent, so hiding waits for the attach.
override var ariaModal = false
set(value) {
if (field == value) return
field = value
if (value) {
val listener = modalAttachListener ?: ModalAttachListener().also { modalAttachListener = it }
view.addOnAttachStateChangeListener(listener)
if (view.isAttachedToWindow) hideSiblingsFromAccessibility()
} else {
modalAttachListener?.let { view.removeOnAttachStateChangeListener(it) }
restoreSiblingsForAccessibility()
}
}

private var modalAttachListener: ModalAttachListener? = null
private var hiddenSiblings: List<Pair<View, Int>>? = null

private inner class ModalAttachListener : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) = hideSiblingsFromAccessibility()

override fun onViewDetachedFromWindow(v: View) = restoreSiblingsForAccessibility()
}

// Siblings added while the view is shown stay visible to screen readers.
private fun hideSiblingsFromAccessibility() {
if (hiddenSiblings != null) return
val parent = view.parent as? ViewGroup ?: return
val hidden = mutableListOf<Pair<View, Int>>()
for (index in 0 until parent.childCount) {
val sibling = parent.getChildAt(index)
val importance = sibling.importantForAccessibility
if (sibling === view || importance == View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) continue
hidden.add(sibling to importance)
sibling.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
}
hiddenSiblings = hidden
// TalkBack announces a pane by its title when it appears.
view.contentDescription?.let { ViewCompat.setAccessibilityPaneTitle(view, it) }
}

private fun restoreSiblingsForAccessibility() {
val hidden = hiddenSiblings ?: return
hiddenSiblings = null
for ((sibling, importance) in hidden) {
if (sibling.importantForAccessibility == View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS) {
sibling.importantForAccessibility = importance
}
}
ViewCompat.setAccessibilityPaneTitle(view, null)
}

private fun updateImportantForAccessibility() {
view.importantForAccessibility = when {
ariaHidden -> View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
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.

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.

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.

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

3 changes: 3 additions & 0 deletions packages/nitro-ui-renderer/src/ViewPropSetters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ export const viewPropsSetters: {
},
accessibilityActions: applyAccessibilityActions,
onAccessibilityAction: applyAccessibilityActionHandler,
'aria-modal': (view, value) => {
view.ariaModal = value ?? false
},
};
5 changes: 5 additions & 0 deletions packages/nitro-ui-renderer/src/specs/View.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ export declare class View extends HybridObject {
accessibilityActions?: AccessibilityAction[]
/** Called with the action's name when a screen reader user performs one of `accessibilityActions`. */
onAccessibilityAction?: (actionName: string) => void
/**
* Screen readers ignore this view's siblings, such as the screen behind a custom alert.
* Maps to `accessibilityViewIsModal` on iOS. Android hides the siblings and names the view as a pane.
*/
ariaModal: boolean

animate(duration: number, curve: AnimationCurve, animations: () => void): Animator
/**
Expand Down
5 changes: 5 additions & 0 deletions packages/nitro-ui-renderer/src/types/AccessibilityProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ export type AccessibilityProps = {
accessibilityActions?: readonly AccessibilityAction[]
/** Called when a screen reader user performs one of `accessibilityActions`. */
onAccessibilityAction?: (event: AccessibilityActionEvent) => void
/**
* Screen readers ignore this view's siblings while it's shown, such as the content behind a
* custom alert. Render the view as a sibling of the content it covers.
*/
'aria-modal'?: boolean
}
1 change: 1 addition & 0 deletions packages/nitro-ui-renderer/test/list-recycling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FakeView implements NativeView {
ariaValueText?: string
accessibilityActions?: AccessibilityAction[]
onAccessibilityAction?: (actionName: string) => void
ariaModal = false

constructor(readonly kind: string) {
nodes.set(this.layoutNodePtr, { view: this, children: [] })
Expand Down
Loading