Accessibility
Familiar web accessibility APIs in a platform-agnostic form.
Accessibility in React Native for Web combines several separate web APIs into a cohesive system. Assistive technologies (e.g., VoiceOver, TalkBack screen readers) derive useful information about the structure, purpose, and interactivity of web apps from their HTML elements, attributes, and ARIA in HTML.
Accessibility Props API
React Native for Web includes APIs for making accessible apps. (Note that for compatibility with existing React Native code, the React Native-specific accessibility*
props are also supported.)
Accessibility patterns
Links
The Text
and View
components can be rendered as links. If the href
prop is set, the element will render <a>
tags without altering the presentation of the element.
<Text href="/" />
// <a href="/" ></a>
The hrefAttrs
prop sets link-related attributes.
const hrefAttrs = { download: true, rel: "nofollow", target: "blank" };
<Text
href="/document.pdf"
hrefAttrs={hrefAttrs}
/>
// <a download href="/document.pdf" rel="nofollow" target="_blank"></a>
Keyboard focus
The tabIndex
prop determines whether a component is user-focusable and appears in the keyboard tab flow. This prop should be used instead of the accessible
(or focusable
prop) found in React Native for Android/iOS, which is not implemented by React Native for Web/Windows/macOS.
<View tabindex={0} />
// <div tabindex="0"></div>
<Text tabindex={-1} href="/" />
// <a href="/" tabindex="-1"></a>
Did you know? Any element (including elements not in the keyboard tab flow) can be programmatically focused via UIManager.focus(viewRef.current)
.
Accessible HTML
React Native for Web components express semantics exclusively via the aria-*
props.
<View
aria-label="..."
aria-pressed={false}
id="abc"
role="menuitem"
/>
/*
<div
aria-label="..."
aria-pressed="false"
id="abc"
role="menuitem"
/>
*/
Semantic HTML
The value of the role
prop is used to infer an analogous HTML element where appropriate. This is done to rely on well-supported native mechanisms for encoding semantics and accessibility information.
<View role="article">
<Text role="paragraph">This is an article</Text>
</View>
/*
<article>
<p>This is an article</p>
</article>
*/
If the "heading"
role is combined with an aria-level
, the equivalent HTML heading element is rendered. Otherwise, it is rendered as <h1>
.
<Text role="heading" /> /* <h1> */
<Text role="heading" aria-level={2} /> /* <h2> */
Note: Avoid changing role
values over time or after user actions. Generally, accessibility APIs do not provide a means of notifying assistive technologies if a role
changes.