|
| 1 | +When crafting an accessible component, the first and most important thing is that the component should render valid HTML. |
| 2 | + |
| 3 | +Both the HTML and ARIA specifications have been written in a way that make them work together. Semantic HTML provides the necessary _context_ to screen readers. |
| 4 | + |
| 5 | +Browsers have implemented the spec in a way that provides functionality for free. |
| 6 | +For example, consider this code sample: |
| 7 | + |
| 8 | +```html |
| 9 | +<button type="submit">Submit Form</button> |
| 10 | +``` |
| 11 | + |
| 12 | +Here is what would be provided by the browser that the developer would otherwise need to provide: |
| 13 | + |
| 14 | +- keyboard interactions on interactive elements (i.e., using the `ENTER` key to activate a `<button>` element) |
| 15 | +- a machine-readable name |
| 16 | +- a place in the `TAB` order of the page |
| 17 | +- the intrinsic role of button |
| 18 | + |
| 19 | +If the interactive element would be written another way, such as: |
| 20 | + |
| 21 | +```html |
| 22 | +<div>Submit Form</div> |
| 23 | +``` |
| 24 | + |
| 25 | +The developer would need to write the following additional code: |
| 26 | + |
| 27 | +- add the role of button (`role="button"`) |
| 28 | +- add the button to the tab order (`tabindex="0"`) |
| 29 | +- add the keyboard functionality (a JavaScript function to activate the associated action when the `ENTER` key is pressed) |
| 30 | + |
| 31 | +This is just one example of how developers can use HTML's built in features to improve accessibility and reduce the need for custom code. Read more here: ["Just use a button"](https://developer.paciellogroup.com/blog/2011/04/html5-accessibility-chops-just-use-a-button/). |
| 32 | + |
| 33 | +## Focus management in components |
| 34 | + |
| 35 | +Focus is one of the main ways a component can communicate with screen readers. |
| 36 | + |
| 37 | +For example, when you hit tab on a page or click on a form field, a blue border usually appears around the element. This kind of behavior is part of focus. |
| 38 | +Developers can use JavaScript to control the focus in their apps, enabling keyboard navigation and usability by screen readers. |
| 39 | +For example, if there is a button that launches a modal with interactive elements in it, that button's click handler needs to contain code that brings focus to the new content. |
| 40 | + |
| 41 | +This article is a good launching point for learning more about focus: [Keyboard accessibility](https://webaim.org/techniques/keyboard/) |
| 42 | + |
| 43 | +Here are some other tips to get you started: |
| 44 | + |
| 45 | +- There is a difference between browse mode and focus mode in screen readers- see ["Focus Please"](https://codepen.io/melsumner/live/ZJeYoP). |
| 46 | +- Focus should return from whence it came- for example, if a `<button>` element opens a modal, the focus should then be returned to that same trigger button once the modal is closed. |
| 47 | +- Note: `role="presentation"` or `aria-hidden="true"` should not be used on a focusable element. |
| 48 | + |
| 49 | + |
| 50 | +## Accessible name |
| 51 | + |
| 52 | +All interactive elements must have an accessible name. But what does that mean, exactly? |
| 53 | + |
| 54 | +Because the code that is written must be readable by other machines (assistive tech like screen readers, for example), there is documentation about how this accessible name is determined: [Accessible Name and Description Computation](https://www.w3.org/TR/accname-1.1/). |
| 55 | + |
| 56 | +However, the most common methods for providing accessible names can be reviewed here. |
| 57 | + |
| 58 | +### Adding a label to an input element |
| 59 | + |
| 60 | +Every `<input>` element should have an associated `<label>` element. To do this, the `<input>` element's `id` attribute value should be the same as the `for` attribute value on the `<label>`. Ember has a built-in `unique-id` helper that can generate unique ids that you can use like this: |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +```html |
| 65 | +{{#let (unique-id) as |id|}} |
| 66 | + <label for={{id}}>Name:</label> |
| 67 | + <input id={{id}} name="name" value="" type="text" /> |
| 68 | +{{/let}} |
| 69 | +``` |
| 70 | + |
| 71 | +It is also valid to wrap the `<label>` element around the `<input />` element: |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +```html |
| 76 | +<label>Name: |
| 77 | + <input name="name" value="" type="text" /> |
| 78 | +</label> |
| 79 | +``` |
| 80 | + |
| 81 | +However, this option can be a little harder to apply styles to, so both should be tested before determining which approach to use. |
| 82 | + |
| 83 | +<div class="cta"> |
| 84 | + <div class="cta-note"> |
| 85 | + <div class="cta-note-body"> |
| 86 | + <div class="cta-note-heading">Zoey says...</div> |
| 87 | + <div class="cta-note-message"> |
| 88 | +To dig deeper into accessible input patterns in Ember check out the <a href="https://emberjs-1.gitbook.io/ember-component-patterns/form-components/input">ember-component-patterns article on Input Fields</a>. |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + <img src="/images/mascots/zoey.png" role="presentation" alt=""> |
| 92 | + </div> |
| 93 | +</div> |
0 commit comments