You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/accessibility.md
+65-1
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,48 @@ Note that all `aria-*` HTML attributes are fully supported in JSX. Whereas most
39
39
/>
40
40
```
41
41
42
+
## Semantic HTML
43
+
Semantic HTML is the foundation of accessibility in a web application. Using the various HTML elements to reinforce the meaning of information
44
+
in our websites will often give us accessibility for free.
45
+
46
+
-[MDN HTML elements reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element)
47
+
48
+
Sometimes we break HTML semantics when we add `<div>` elements to our JSX to make our React code work, especially when working with lists (`<ol>`, `<ul>` and `<dl>`) and the HTML `<table>`.
49
+
In these cases we should rather use React Fragments to group together multiple elements.
50
+
51
+
Use `<Fragment>` when a `key` prop is required:
52
+
53
+
```javascript{1,8,11}
54
+
import React, { Fragment } from 'react';
55
+
56
+
function Glossary(props) {
57
+
return (
58
+
<dl>
59
+
{props.items.map(item => (
60
+
// Without the `key`, React will fire a key warning
61
+
<Fragment key={item.id}>
62
+
<dt>{item.term}</dt>
63
+
<dd>{item.description}</dd>
64
+
</Fragment>
65
+
)}
66
+
</dl>
67
+
);
68
+
}
69
+
```
70
+
71
+
Use `<></>` syntax everywhere else:
72
+
73
+
```javascript
74
+
functionListItem({ item }) {
75
+
return (
76
+
<>
77
+
<dt>{item.term}</dt>
78
+
<dd>{item.description}</dd>>
79
+
</>
80
+
);
81
+
}
82
+
```
83
+
42
84
## Accessible Forms
43
85
44
86
### Labeling
@@ -100,7 +142,7 @@ we need to programmatically nudge the keyboard focus in the right direction. For
100
142
101
143
The Mozilla Developer Network takes a look at this and describes how we can build [keyboard-navigable JavaScript widgets](https://developer.mozilla.org/en-US/docs/Web/Accessibility/Keyboard-navigable_JavaScript_widgets).
102
144
103
-
To set focus in React, we can use [Refs to Components](refs-and-the-dom.html).
145
+
To set focus in React, we can use [Refs to DOM elements](refs-and-the-dom.html).
104
146
105
147
Using this, we first create a ref to an element in the JSX of a component class:
106
148
@@ -124,6 +166,28 @@ Then we can focus it elsewhere in our component when needed:
124
166
this.textInput.focus();
125
167
}
126
168
```
169
+
170
+
Sometimes a parent component needs to set focus to an element in a child component. Although we can create [refs to class components](refs-and-the-dom.html#adding-a-ref-to-a-class-component),
171
+
we need a pattern that also works with functional components and when [using refs with HOCs](higher-order-components.html#refs-arent-passed-through).
172
+
To ensure that our parent component can always access the ref, we pass a callback as a prop to the child component to [expose the ref to the parent component](refs-and-the-dom.html#exposing-dom-refs-to-parent-components).
173
+
174
+
```js
175
+
// Expose the ref with a callback prop
176
+
functionField({ inputRef, ...rest }) {
177
+
return<input ref={inputRef} {...rest} />;
178
+
}
179
+
180
+
// Inside a parent class component's render method...
181
+
<Field
182
+
inputRef={(inputEl) => {
183
+
// This callback gets passed through as a regular prop
184
+
this.inputEl= inputEl
185
+
}}
186
+
/>
187
+
188
+
// Now you can set focus when required.
189
+
this.inputEl.focus();
190
+
```
127
191
128
192
A great focus management example is the [react-aria-modal](https://github.com/davidtheclark/react-aria-modal). This is a relatively rare example of a fully accessible modal window. Not only does it set initial focus on
129
193
the cancel button (preventing the keyboard user from accidentally activating the success action) and trap keyboard focus inside the modal, it also resets focus back to the element that
0 commit comments