Docs
Accessibility
NSH renders with native views, so the platforms’ assistive technologies (VoiceOver on iOS and Mac Catalyst, TalkBack on Android, Switch Control, Voice Control, …) see your app’s real view hierarchy. What you add are the descriptions those technologies cannot infer: what an element is, what it says, what state it is in and what it can do. Every element accepts the methods (Rust) and props (TSX) below.
#What you get without doing anything
- Text is read. A text element is an accessibility element whose label is its text. A view without a label is read as the texts inside it.
- Pressable views are elements. A view with a press or long press handler (
.on_press,onPress) is an accessibility element by default, and activating it (double tap with VoiceOver or TalkBack) presses it. - Inputs are text fields, with their placeholder, value and keyboard.
- Text follows the user’s size setting. See Text size.
#Labels, hints and roles
view()
.on_press(play)
.role(AccessibilityRole::Button)
.label("Play")
.hint("Starts the song")
.size(px(44.0))
.items_center()
.justify_center()
.child(text("▶"))<View
onPress={play}
role="button"
accessibilityLabel="Play"
accessibilityHint="Starts the song"
style={{ width: 44, height: 44, alignItems: "center", justifyContent: "center" }}
>
<Text>▶</Text>
</View>| Rust | TSX | Type | Description |
|---|---|---|---|
.accessible(..) |
accessible |
boolean |
The view is one element for assistive technologies; its children are not exposed separately. |
.label(..) |
accessibilityLabel |
string |
What screen readers read, instead of the texts inside. |
.hint(..) |
accessibilityHint |
string |
What activating the element does, read after a pause. |
.accessibility_description(..) |
accessibilityDescription |
string |
More about the element than its name, read after it: a dialog’s message, what a field expects. |
.role(..) |
role |
AccessibilityRole |
What the element is. |
.accessibility_state(..) |
accessibilityState |
AccessibilityState |
Its state: disabled, selected, checked, busy, expanded, pressed, current, has popup. selected also applies the selected style, checked the checked style when true and the indeterminate style when "mixed", pressed the pressed style when true. |
.accessibility_level(..) |
accessibilityLevel |
number |
The level of a heading (1 the top), the depth of a tree item (1 at the top). |
.accessibility_value(..) |
accessibilityValue |
{ min?, max?, now?, text? } |
The value of a range or textual element. |
.accessibility_active_descendant(..) |
accessibilityActiveDescendant |
ViewRef | null |
The item that is active inside the focused element: a combo box’s highlighted option, a list’s cursor row. |
.accessibility_controls(..) |
accessibilityControls |
ViewRef | null |
The element whose content this one controls: a combo box’s list. |
.accessibility_described_by(..) |
accessibilityDescribedBy |
ViewRef | null |
The element whose text describes this one: a field’s error line. |
accessibilityActions |
{ name, label? }[] |
Actions offered to assistive technologies. | |
.on_accessibility_action(..) |
onAccessibilityAction |
(e: { actionName }) => void |
An action was performed (Rust: its name). |
.accessibility_elements_hidden(..) |
accessibilityElementsHidden |
boolean |
Hides the view and its subtree from assistive technologies. |
importantForAccessibility |
"auto" | "yes" | "no" | "no-hide-descendants" |
Whether the view (and its subtree) is exposed. | |
android={{ accessibilityLiveRegion }} |
"none" | "polite" | "assertive" |
Android: how changes of the view are announced to TalkBack. | |
.test_id(..) |
testID |
string |
An identifier for UI tests (the accessibility identifier on iOS, the resource id on Android); not read aloud. |
Give every icon-only control a label: a screen reader cannot read a glyph or an image. Keep labels short and do not include the role (“Play”, not “Play button”): the role is announced on its own.
#Roles
The role (.role(AccessibilityRole::Header), role="header") tells the user what kind of element this is and how to operate it. In Rust it is an AccessibilityRole variant (Button, Header, Switch, …); the TSX values are "none", "button", "link", "search", "image", "imagebutton", "keyboardkey", "text", "adjustable", "header", "summary", "alert", "checkbox", "combobox", "menu", "menubar", "menuitem", "progressbar", "radio", "radiogroup", "scrollbar", "spinbutton", "switch", "tab", "tablist", "timer", "toolbar", "list", "grid", "table", "rowgroup", "row", "columnheader" and "cell" (the roles of Table’s parts), "listbox" and "option" (a list of options to choose from, such as Select’s list, and its options, the chosen one marked by accessibilityState.selected), "group" (related elements that belong together), "region" (a section worth reaching on its own, such as an accordion’s panel), "navigation" (links that move around the app, such as a pagination), "tree" and "treeitem" (items that expand and collapse, each at its accessibilityLevel), "menuitemcheckbox" and "menuitemradio" (menu items that are checked, alone or as one choice of a group), "dialog" and "alertdialog" (a dialog over the rest of the app, named by its accessibilityLabel, and one that asks the user to confirm something; Dialog is a "dialog" unless its role says otherwise).
The most useful ones:
"button"for anything pressable that is not a link;"link"for navigation to content."header"for section titles: screen reader users jump between headers to skim a screen.accessibilityLevelgives its level (1 the top) where the platform reads one."button"withaccessibilityState.pressedfor a toggle button, one that stays on or off (bold in a toolbar)."navigation"around links that move between pages, the link of the shown page withaccessibilityState.current: "page"."switch","checkbox","radio"withaccessibilityState.checked."adjustable"for sliders and steppers, withaccessibilityValueand theincrement/decrementactions (each thumb of aSlideris one)."spinbutton"for a field that holds a number the user can also step, with the same value and actions (the field of aNumberInput)."image"for meaningful images, with a label; decorative images should be hidden instead.
text("Settings")
.role(AccessibilityRole::Header)
.text_size(px(22.0))
.font_weight(FontWeight::BOLD)<Text role="header" style={{ fontSize: 22, fontWeight: "700" }}>Settings</Text>#States and values
For an on / off control in an app, use Switch from nsh/base, which sets the role and state itself. The example shows what a role and state on a view do.
use nsh_core::{Checked, prelude::*};
// A toggle drawn with views.
view()
.on_press(move || on.update(|v| *v = !*v))
.role(AccessibilityRole::Switch)
.label("Wi-Fi")
.accessibility_state(move || AccessibilityState {
checked: Some(if on.get() { Checked::True } else { Checked::False }),
..AccessibilityState::default()
})
.hit_slop(EdgeInsets::all(px(8.0)))
.w(px(51.0))
.h(px(31.0))
.rounded_full()
.p(px(2.0))
.bg(move || if on.get() { rgb(0x22c55e) } else { rgb(0xd4d4d8) })
.child(
view()
.size(px(27.0))
.rounded_full()
.bg(white())
.style_with(move |s| s.translate_x(px(if on.get() { 20.0 } else { 0.0 }))),
)// A toggle drawn with views.
<View
onPress={() => on.set(!on())}
role="switch"
accessibilityLabel="Wi-Fi"
accessibilityState={{ checked: on() }}
hitSlop={8}
class="h-[31px] w-[51px] rounded-full p-0.5 transition-colors"
classList={{ "bg-green-500": on(), "bg-zinc-300": !on() }}
>
<View class="size-[27px] rounded-full bg-white transition-transform" classList={{ "translate-x-5": on() }} />
</View>The state is AccessibilityState { disabled, selected, checked: Option<Checked>, busy, expanded, pressed: Option<Checked>, current: Option<AccessibilityCurrent>, has_popup, invalid } in Rust and { disabled?, selected?, checked?: boolean | "mixed", busy?, expanded?, pressed?: boolean | "mixed", current?: boolean | "page" | "step" | "location" | "date" | "time", hasPopup?, invalid? } in TSX. pressed makes a button a toggle button (ARIA’s aria-pressed); current marks the current item of a set (ARIA’s aria-current): a pagination’s page, a wizard’s step; hasPopup marks the trigger of a popup, a menu or a list (ARIA’s aria-haspopup), whose expanded says whether it is open, where an expanded element without it is a disclosure that shows its content in place (Popover, Menu, HoverCard, Select, Combobox and DatePicker set it on their triggers); invalid marks content that does not pass the app’s checks (ARIA’s aria-invalid): a field whose text was rejected, a required select left empty. Screen readers read it with the element, and aria-invalid: classes style it. disabled is also implied by disabling the view (.disabled(..), disabled). accessibilityValue with min, max and now is read as a percentage; with text it is read as given (“3 of 10”, “Medium”).
#The active item of a focused element
A control can keep the keyboard focus while the user moves through items inside it: a combo box’s field stays focused as the arrow keys walk its list, a list moves a cursor row. accessibilityActiveDescendant names the item that is active now, and accessibilityControls names the element the control drives, so a screen reader reads each item as it becomes active while the focus stays where the user types. Both take a ref of the element named (a ViewRef in TSX, an ElementRef in Rust), which may be rendered later than the control, as a virtualized list’s rows are; while the element named is not on screen, the relation names nothing.
// A list that keeps the focus and moves a cursor row.
let rows: Rc<Vec<ElementRef>> = Rc::new(results.iter().map(|_| element_ref()).collect());
let named = rows.clone();
view()
.role(AccessibilityRole::Listbox)
.label("Results")
.focusable(true)
.accessibility_active_descendant(move || named.get(cursor.get()).copied())
.children(results.iter().enumerate().map(|(i, result)| {
view()
.role(AccessibilityRole::Option)
.element_ref(&rows[i])
.accessibility_state(move || AccessibilityState {
selected: cursor.get() == i,
..AccessibilityState::default()
})
.child(text(result.title.clone()))
}))// A list that keeps the focus and moves a cursor row.
const cursor = signal(0);
const rows = signal<ViewRef[]>([]);
<View
role="listbox"
accessibilityLabel="Results"
focusable
accessibilityActiveDescendant={rows()[cursor()] ?? null}
>
<Each of={results()}>
{(result, i) => (
<View
ref={(row) =>
rows.update((all) => {
const next = all.slice();
next[i()] = row;
return next;
})
}
role="option"
accessibilityState={{ selected: cursor() === i() }}
>
<Text>{result().title}</Text>
</View>
)}
</Each>
</View>On the web these are aria-activedescendant and aria-controls; on macOS VoiceOver reads the active item as the control’s selected child. Select, Combobox and Command name their highlighted item and their list this way, and on the other platforms they read the highlighted item to the screen reader themselves.
#An element that describes another
A form shows a field’s error or help in a line of its own, apart from the field. accessibilityDescribedBy names that line on the field, so a screen reader reads its text after the field’s name each time the focus comes to it, and reads the new text when the line changes. It takes a ref of the element named, as accessibilityControls does; its labels and texts are the field’s description, in the place of the field’s own accessibilityDescription, which comes back while the element named is not on screen. The line may be accessibilityHidden, so that it is read once, with its field: an element named reads its text all the same. Every platform reads it where it reads a description; on the web it is aria-describedby.
// A field and the error line under it.
let error = element_ref();
view()
.child(
input()
.label("Name")
.accessibility_state(move || AccessibilityState {
invalid: message.get().is_some(),
..AccessibilityState::default()
})
.accessibility_described_by(move || message.get().map(|_| error)),
)
.child(when(
move || message.get().is_some(),
move || text(move || message.get().unwrap_or_default()).element_ref(&error),
))// A field and the error line under it.
const error = signal<ViewRef>();
<View>
<Input
accessibilityLabel="Name"
accessibilityState={{ invalid: message() != null }}
accessibilityDescribedBy={message() != null ? error() ?? null : null}
/>
<If when={message()}>
<Text ref={error}>{message()}</Text>
</If>
</View>#Actions
A pressable view supports activation already. For anything else, declare actions with accessibilityActions and handle them with onAccessibilityAction; a Rust component hears the performed action’s name with .on_accessibility_action(|name: &str| ..):
function Quantity() {
const n = signal(1);
return (
<View
accessible
role="adjustable"
accessibilityLabel="Quantity"
accessibilityValue={{ text: String(n()) }}
accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
onAccessibilityAction={(e) => {
if (e.actionName === "increment") n.update((v) => v + 1);
if (e.actionName === "decrement") n.update((v) => Math.max(0, v - 1));
}}
style={{ flexDirection: "row", alignItems: "center", gap: 12 }}
>
<View onPress={() => n.update((v) => Math.max(0, v - 1))}><Text>−</Text></View>
<Text>{n()}</Text>
<View onPress={() => n.update((v) => v + 1)}><Text>+</Text></View>
</View>
);
}
Some action names are standard and map to the platforms’ own gestures:
| Name | iOS (VoiceOver) | Android (TalkBack) |
|---|---|---|
activate |
Double tap | Double tap (click) |
increment, decrement |
Swipe up / down, then VoiceOver reads the new value | Scroll forward / backward |
longpress |
Double tap and hold | |
expand, collapse |
Expand / collapse | |
magicTap |
Two-finger double tap | |
escape |
Two-finger Z scrub |
An element with increment or decrement is adjustable whatever its role: a chart keeps its "image" role and steps through its points with VoiceOver’s swipes and TalkBack’s scrolls, its accessibilityValue read after each step.
Any other name becomes a custom action, listed in VoiceOver’s actions rotor and TalkBack’s actions menu with its label (or its name). Custom actions are the accessible form of swipe-to-delete and similar gestures:
<View
accessibilityActions={[{ name: "archive", label: "Archive" }, { name: "delete", label: "Delete" }]}
onAccessibilityAction={(e) => (e.actionName === "archive" ? archive(mail.id) : remove(mail.id))}
>
…
</View>
#Grouping and hiding
Making a view accessible (.accessible(true), accessible) makes a view one element: a card with a title, a subtitle and a price is read in one go, instead of the user swiping through three texts.
view()
.accessible(true)
.on_press(open)
.role(AccessibilityRole::Button)
.p(px(16.0))
.child(text("Blue jacket"))
.child(text("Size M"))
.child(text("$120"))<View accessible onPress={open} role="button" style={{ padding: 16 }}>
<Text>Blue jacket</Text>
<Text>Size M</Text>
<Text>$120</Text>
</View>Hide what adds nothing: decorative images, dividers, an icon next to a text that already says the same.
<Image src={decoration} accessibilityElementsHidden importantForAccessibility="no-hide-descendants" />
accessibilityElementsHidden (iOS’s name) and importantForAccessibility="no-hide-descendants" (Android’s) both hide the subtree on both platforms; setting both is harmless.
#The screen reader and the user’s settings
In Rust, the prelude reports the user’s settings and talks to the screen reader: screen_reader() and reduce_motion() are reactive, announce_for_accessibility(message) has the screen reader read a message, and an element’s element_ref() moves the screen reader’s focus to it with set_accessibility_focus(). In TSX, nsh’s accessibility API reports the user’s settings and lets you talk to the screen reader.
| Member | Description |
|---|---|
screenReaderEnabled() |
Whether VoiceOver or TalkBack runs (reactive). |
reduceMotion() |
Whether the user asked for less motion (reactive). |
announce(message) |
The screen reader reads message. |
setAccessibilityFocus(ref) |
Moves the screen reader’s focus to a view. |
The settings are accessors: read them in a binding or an effect, and what reads them updates when the user changes the setting, with no cleanup.
use nsh_core::prelude::*;
fn upload() -> impl IntoElement {
let done = signal(false);
let result = element_ref();
let finish = move || {
done.set(true);
announce_for_accessibility("Upload complete");
result.set_accessibility_focus();
};
view()
.gap(px(12.0))
// Autoplaying carousels are hard to use with a screen reader: show a list instead.
.child(when(move || !screen_reader(), photo_carousel).otherwise(photo_list))
.child(
text(move || if done.get() { "Uploaded 3 photos" } else { "Uploading…" })
.element_ref(&result),
)
}import { signal, Text, View, type ViewRef } from "nsh";
import { announce, screenReaderEnabled, setAccessibilityFocus } from "nsh";
function Upload() {
const done = signal(false);
let result!: ViewRef;
const finish = () => {
done.set(true);
announce("Upload complete");
setAccessibilityFocus(result);
};
return (
<View style={{ gap: 12 }}>
{/* Autoplaying carousels are hard to use with a screen reader: show a list instead. */}
<If when={!screenReaderEnabled()} else={<PhotoList />}>
<PhotoCarousel />
</If>
<Text ref={result}>{done() ? "Uploaded 3 photos" : "Uploading…"}</Text>
</View>
);
}Announce changes the user would otherwise miss: a background save finishing, an error appearing away from the focus, a timer ending. On Android, android={{ accessibilityLiveRegion: "polite" }} on a view makes TalkBack announce its changes by itself.
An alert (.role(AccessibilityRole::Alert), role="alert") is read by itself on macOS and the web: its label, or else the texts inside it, as it appears and again when its content changes, ahead of what the screen reader was saying. An error shown with the role needs no announcement of its own there; on macOS the same text announced in the same update is read once. On the other platforms, announce it as well.
#Text size
Text and inputs follow the system text size (Dynamic Type on iOS, font size on Android) unless you turn it off (.allow_font_scaling(false), allowFontScaling={false}). Rather than turning it off, design for it:
- Let text wrap: avoid fixed heights on views that contain text.
- Cap very large headings with
.max_font_size_multiplier(..)/maxFontSizeMultiplierinstead of disabling scaling. - Switch layouts when the text is large: read
font_scale()(Rust) orfontScale()fromnsh(TSX) (for example, stack a label and its value vertically above 1.3).
#Reduced motion
reduce_motion() (Rust) and reduceMotion() (TSX) are true when the user turned on iOS’s Reduce Motion or Android’s remove animations setting. Your animations run as you write them, so use it, or the motion-reduce: and motion-safe: Tailwind variants, to replace large movement with a fade or an instant change:
use nsh_core::{element::LayoutAnimation, prelude::*};
let toggle = move || {
if !reduce_motion() {
animate_layout(LayoutAnimation::SPRING);
}
expanded.update(|e| *e = !*e);
};
view()
.on_press(toggle)
.when(!reduce_motion(), |v| v.active(|s| s.scale(0.95)))import { View } from "nsh";
import { reduceMotion } from "nsh";
import { LayoutAnimation } from "nsh";
const toggle = () => {
if (!reduceMotion()) LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
expanded.set(!expanded());
};
<View onPress={open} class="transition active:scale-95 motion-reduce:transition-none" />
<View class="motion-safe:animate-bounce" />#Touch targets
Make pressable elements at least 44 × 44 points. When a control must look smaller, extend its touch area with the hit slop (.hit_slop(..), hitSlop) instead of its size; see Touch and gestures.
#Checking your app
- Turn on VoiceOver (iOS Settings, Accessibility, or triple-click the side button if set up) or TalkBack, and use the app without looking: every control should say what it is and what it does.
- On iOS simulators, Xcode’s Accessibility Inspector shows each element’s label, traits and value.
- Try the largest text size and dark mode.
- A test id (
.test_id(..),testID) gives UI tests a stable handle that does not change with the label’s language.
#Related
nsh’s accessibility API: the reference.View: every accessibility prop.- Animation and Device and environment.