Accessibility
Screen readers read your app aloud and let people move through it by touch: VoiceOver on iPhone and TalkBack on Android. Lynx elements take attributes that tell the screen reader what each element is and what to say.
id, a11y-id and name
Three attributes name an element, and they're often confused:
Use id unless you have a reason not to.
Label an element
<text>and<image>are read by default. A<text>reads its own text, so most text needs nothing extra.[accessibility-element]="true"makes an element one stop for the screen reader. Use it on a<view>that acts as a button or groups related content.accessibility-labelis what gets read. Give one to anything without readable text, such as an icon button.accessibility-traitssays what kind of thing it is, such asbutton,image,linkorheader. Note the plural: Lynx ignoresaccessibility-trait.- iOS takes several traits, separated by commas with no spaces, such as
button,selected. Android reads only one. - A heading needs two attributes:
accessibility-traits="header"for iOS, andaccessibility-headingfor Android, which reads only thenone,imageandbuttontraits. Screen reader users skim a screen by jumping from heading to heading. accessibility-valueis a current value, read after the label, such as a slider's position.accessibility-statusis read before the label, on iOS and Android:accessibility-status="Unread"on a row labeledInboxreads "Unread, Inbox".- An active
routerLinkActivelink isselectedfor you.LYNX_ELEMENTSadds the trait while its route is active, after the element's own traits. See Routing.
Every accessibility attribute, with the platforms that read it, is in the API reference.
Use ARIA attributes
Lynx doesn't read ARIA, so on iOS and Android AngularLynx gives each ARIA attribute that has an equivalent as Lynx's own. That covers the attributes you'd write on the web, and what libraries such as Angular Aria and the CDK set. Bindings work too, such as [aria-label]="name()", [aria-checked]="on()" and [aria-expanded]="open()".
The first is a button whose value is "Collapsed" until it opens. The second is a button labeled by the total, which its label follows as total() changes. The dialog keeps the screen reader inside it while it's shown: aria-modal gives that, and role="dialog" gives nothing (see below).
- A Lynx attribute you set yourself wins. With
aria-label="Close"andaccessibility-label="Close the dialog", the screen reader reads "Close the dialog", whichever you set first. Your ownaccessibility-valuewins over a state's, and your ownaccessibility-traitsreplace the role's, andaria-selected,aria-pressedandaria-disabledadd to whichever it has, as[lynxAccessibilityTraits]does. aria-valuetextreplaces the states' words. Without it, an element with several states says each, in turn: "Checked, Collapsed".- States are words, not the system's own states. Lynx 4.1 has no checked, pressed, expanded or range state on either platform, so VoiceOver and TalkBack read the word as the element's value, after its label. TalkBack doesn't say "checked" or "not checked" in the phone's language for them, and Android 7 to 10 don't read a value at all. The words are English unless you give your own.
aria-labelledbytakes the text of each element it names, in its order, or that element's ownaria-label. An element inside one is read by its ownaria-label, or an image'salt, in place of its text, and one witharia-hidden="true"adds nothing, as in a browser, unless the element the id names is hidden itself: then all of it is read. A blankaria-labelgives way to the text. It keeps up when that text changes, or when an element with the id appears later. It comes before the element's ownaria-label, as in a browser, and thearia-labelis the label while the ids name no text. An id with#,.,[,>or,in it can't be found, as Lynx looks ids up with its own selector.- An
<img>'saria-labelledbyandaria-labelcome before itsalt, as in a browser. - TalkBack hears
aria-selectedandaria-disabledonly as a value. Lynx 4.1's Android reads one trait per element, and has no selected or disabled state (an open Lynx pull request, lynx-family/lynx#11000, would add them). So on Android they're words in the element's value, "Selected" or "Disabled", after any other state's: a disabled checked checkbox is "Checked, Disabled". VoiceOver reads both as traits. - There's no swipe to change a slider's value. Lynx 4.1 gives VoiceOver no increment or decrement, so
role="slider"doesn't get theadjustabletrait. Giveaccessibility-actionsfor that, as Dolan's slider does. - TalkBack still reaches what's inside an
aria-hiddenelement, as it does withaccessibility-elements-hidden. See Hide decoration. - VoiceOver takes each view directly inside a
role="tablist"for a tab. Put it on the row whose children are the tabs. - The rest of ARIA does nothing, because Lynx has no equivalent:
aria-live,aria-describedby,aria-current,aria-controlsand the other relationship attributes, and the container roles such aslistbox,menuanddialog. Arole="dialog"isn't a stop of its own, so the screen reader doesn't read a label from itsaria-labeloraria-labelledby; itsaria-modalworks. Announce a change withLynxAccessibility.announce(). - In the web preview, the browser reads the ARIA attributes themselves.
Say states in your app's language
The words for ARIA states and roles come from ACCESSIBILITY_WORDS, which is English unless you provide your own. Give a function to translate them with $localize: it runs as the app starts, after main.ts has loaded the translations.
- The words you leave out stay English. The full list is in the API reference.
- Dolan's checkbox, switch and slider use the same words for their states, value and actions.
- They're read once, as the app starts. To switch language, restart the app, as for most text.
Mark what's selected
When one of several options is chosen, such as Landscape or Portrait, the chosen one needs the selected trait. Bind it with [lynxAccessibilityTraits], which LYNX_ELEMENTS includes:
- Each trait that's
trueis added to the element's own traits, and taken away again when it turnsfalse. VoiceOver reads the view as a selected button. - Don't write the list into
accessibility-traitsyourself.accessibility-traits="button,selected"works only on iOS. Android reads the attribute as one trait, so the list stops being a button there. With the binding, Android keepsbutton: it has no selected trait to add. disabledworks the same way, for an option that can't be chosen now. VoiceOver says it's dimmed.
A test finds the chosen option with getByRole('button', { name: 'Landscape', selected: true }).
Set the reading order
- Screen readers go top to bottom, then left to right, by default.
accessibility-elementslists childids, separated by commas, in the order to read them. The example reads the title before the price.- Children not in the list can't be focused once a parent sets it.
accessibility-elements-a11ydoes the same witha11y-ids.
Hide decoration
[accessibility-elements-hidden]="true", or aria-hidden="true", hides an element and everything inside it from VoiceOver.
On Android, Lynx 4.1 hides only the element itself. It leaves the children in TalkBack's tree, so hide each child that's an element of its own too:
- With
[accessibility-element]="false". - Or with
role="presentation", on a child that doesn't setaccessibility-elementitself. A child's ownaccessibility-element="true"wins over the role, so change that one tofalse.
Keep focus inside a dialog
With [accessibility-exclusive-focus]="true", the screen reader moves only between the elements inside it. People can't swipe out of the dialog to the page behind. See Dialogs and overlays.
Announce changes and move focus from code
Some changes don't move the screen reader, such as a cart count going up. Tell people about them with LynxAccessibility:
announce(text)makes the screen reader speak, without moving focus.requestFocus('#id')moves the screen reader to the element with thatid, such asrequestFocus('#total').- Both return a Promise that rejects where Lynx can't do it, such as in the web preview. The
catchkeeps that quiet.
The signatures are in the API reference.
Check it in unit tests
The testing library's queries read these attributes, so a test finds an element the way a screen reader does:
getByRole finds a <view> only with [accessibility-element]="true", so a missing one fails the test. See *ByRole and *ByAccessibilityLabel.
Follow the user's display settings
Reduce Motion, Reduce Transparency and Increase Contrast are signals on LynxAccessibilitySettings:
The apps ng build --platform makes send the settings, and the web preview reads the browser's. See LynxAccessibilitySettings for where each comes from. The Glass component follows them on its own.
Follow the phone's text size
Apps from ng build --platform follow the text size the person chose, and change while the app is open:
- iOS: Dynamic Type, in Settings → Display & Brightness → Text Size. The larger sizes are under Accessibility → Display & Text Size.
- Android: the font size, in Settings → Display → Font size.
The page's root font size grows by the person's scale, so everything sized in rem grows with it: Tailwind's text-base, h-12 and p-4, and the Dolan UI components. A size in px stays as written, for the few labels that mustn't grow:
A <text> with no font size of its own takes its parent's. That's the page's unless an element above it sets one, so it grows too. It doesn't if that size is in px: under a <view> with font-size: 18px, it stays 18px.
To change a layout that can't simply grow, such as a row that puts its label under the icon at a large size, read textScale():
If a screen isn't ready for the largest sizes, cap the scale, or turn it off, with textScaling in the build target's options in angular.json:
"textScaling": false keeps the text at its default size. The web preview and Lynx Explorer stay at the default size.
Check a screen at the largest sizes: in the iOS Simulator, with Xcode's Environment Overrides or xcrun simctl ui booted content_size accessibility-extra-extra-extra-large, and on Android with adb shell settings put system font_scale 2.0. Look for anything with a fixed height in px that the text now overflows.
Test with a screen reader
On an iPhone:
- Open Settings → Accessibility → VoiceOver and turn it on.
- Swipe right or left to move between elements. Double-tap anywhere to tap the selected one.
- To toggle it quickly, set Settings → Accessibility → Accessibility Shortcut to VoiceOver, then triple-click the side button.
VoiceOver doesn't run in the iOS Simulator, the virtual iPhone on your Mac. Instead, open Accessibility Inspector from Xcode's Xcode → Open Developer Tool menu and point it at the Simulator. It shows the label and traits of each element you hover.
On Android:
- Open Settings → Accessibility → TalkBack and turn it on. On some phones, it's under Installed apps or Downloaded apps in that menu.
- Swipe right or left to move between elements. Double-tap to tap the selected one.
TalkBack also runs in the Android emulator, the virtual Android phone on your computer. Drag with the mouse to swipe. If its Settings has no TalkBack, install Android Accessibility Suite from the Play Store, which needs an emulator image with Google Play.
You can test either way in Lynx Explorer or in your own app. AngularLynx turns on Lynx's accessibility support in every build, so there's nothing to enable.
Related
LynxAccessibilityand every accessibility attribute, with platform support- Dialogs and overlays
- Lynx's accessibility guide
