Skip to content

Commit 1410e6d

Browse files
AlmeroSteyngaearon
authored andcommitted
a11y-doc: Add Fragments and expand Focus Control. (reactjs#381)
1 parent 7cd8188 commit 1410e6d

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

content/docs/accessibility.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,48 @@ Note that all `aria-*` HTML attributes are fully supported in JSX. Whereas most
3939
/>
4040
```
4141

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+
function ListItem({ item }) {
75+
return (
76+
<>
77+
<dt>{item.term}</dt>
78+
<dd>{item.description}</dd>>
79+
</>
80+
);
81+
}
82+
```
83+
4284
## Accessible Forms
4385

4486
### Labeling
@@ -100,7 +142,7 @@ we need to programmatically nudge the keyboard focus in the right direction. For
100142

101143
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).
102144

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).
104146

105147
Using this, we first create a ref to an element in the JSX of a component class:
106148

@@ -124,6 +166,28 @@ Then we can focus it elsewhere in our component when needed:
124166
this.textInput.focus();
125167
}
126168
```
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+
function Field({ 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+
```
127191

128192
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
129193
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

Comments
 (0)