source: frontend/node_modules/eslint-plugin-jsx-a11y/docs/rules/interactive-supports-focus.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[9af201e]1# jsx-a11y/interactive-supports-focus
2
3💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`.
4
5<!-- end auto-generated rule header -->
6
7Elements with an interactive role and interaction handlers (mouse or key press) must be focusable.
8
9## How do I resolve this error?
10
11### Case: I got the error "Elements with the '${role}' interactive role must be tabbable". How can I fix this?
12
13This element is a stand-alone control like a button, a link or a form element. A user should be able to reach this element by pressing the tab key on their keyboard.
14
15Add the `tabIndex` property to your component. A value of zero indicates that this element can be tabbed to.
16
17```jsx
18<div
19 role="button"
20 tabIndex={0} />
21```
22
23-- or --
24
25Replace the component with one that renders semantic html element like `<button>`, `<a href>` or `<input>` -- whichever fits your purpose.
26
27Generally buttons, links and form elements should be reachable via tab key presses. An element that can be tabbed to is said to be in the _tab ring_.
28
29### Case: I got the error "Elements with the '${role}' interactive role must be focusable". How can I fix this?
30
31This element is part of a group of buttons, links, menu items, etc. Or this element is part of a composite widget. Composite widgets prescribe standard [keyboard interaction patterns](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav). Within a group of similar elements -- like a button bar -- or within a composite widget, elements that can be focused are given a tabindex of -1. This makes the element _focusable_ but not _tabbable_. Generally one item in a group should have a tabindex of zero so that a user can tab to the component. Once an element in the component has focus, your key management behaviors will control traversal within the component's pieces. As the UI author, you will need to implement the key handling behaviors such as listening for traversal key (up/down/left/right) presses and moving the page focus between the focusable elements in your widget.
32
33```jsx
34<div role="menu">
35 <div role="menuitem" tabIndex="0">Open</div>
36 <div role="menuitem" tabIndex="-1">Save</div>
37 <div role="menuitem" tabIndex="-1">Close</div>
38</div>
39```
40
41In the example above, the first item in the group can be tabbed to. The developer provides the ability to traverse to the subsequent items via the up/down/left/right arrow keys. Traversing via arrow keys is not provided by the browser or the assistive technology. See [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav) for information about established traversal behaviors for various UI widgets.
42
43### Case: This element is not a button, link, menuitem, etc. It is catching bubbled events from elements that it contains
44
45If your element is catching bubbled click or key events from descendant elements, then the proper role for this element is `presentation`.
46
47```jsx
48<div
49 onClick={onClickHandler}
50 role="presentation">
51 <button>Save</button>
52</div>
53```
54
55Marking an element with the role `presentation` indicates to assistive technology that this element should be ignored; it exists to support the web application and is not meant for humans to interact with directly.
56
57## Rule options
58
59This rule takes an options object with the key `tabbable`. The value is an array of interactive ARIA roles that should be considered tabbable, not just focusable. Any interactive role not included in this list will be flagged as needing to be focusable (tabindex of -1).
60
61```js
62{
63 'jsx-a11y/interactive-supports-focus': [
64 'error',
65 {
66 tabbable: [
67 'button',
68 'checkbox',
69 'link',
70 'searchbox',
71 'spinbutton',
72 'switch',
73 'textbox',
74 ],
75 },
76 ]
77}
78```
79
80The recommended options list interactive roles that act as form elements. Generally, elements with a role like `menuitem` are a part of a composite widget. Focus in a composite widget is controlled and moved programmatically to satisfy the prescribed [keyboard interaction pattern](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav) for the widget.
81
82The list of possible values includes:
83
84```js
85[
86 'button',
87 'checkbox',
88 'columnheader',
89 'combobox',
90 'grid',
91 'gridcell',
92 'link',
93 'listbox',
94 'menu',
95 'menubar',
96 'menuitem',
97 'menuitemcheckbox',
98 'menuitemradio',
99 'option',
100 'progressbar',
101 'radio',
102 'radiogroup',
103 'row',
104 'rowheader',
105 'searchbox',
106 'slider',
107 'spinbutton',
108 'switch',
109 'tab',
110 'tablist',
111 'textbox',
112 'toolbar',
113 'tree',
114 'treegrid',
115 'treeitem',
116 'doc-backlink',
117 'doc-biblioref',
118 'doc-glossref',
119 'doc-noteref',
120]
121```
122
123### Succeed
124
125```jsx
126<!-- Good: div with onClick attribute is hidden from screen reader -->
127<div aria-hidden onClick={() => void 0} />
128<!-- Good: span with onClick attribute is in the tab order -->
129<span onClick="doSomething();" tabIndex="0" role="button">Click me!</span>
130<!-- Good: span with onClick attribute may be focused programmatically -->
131<span onClick="doSomething();" tabIndex="-1" role="menuitem">Click me too!</span>
132<!-- Good: anchor element with href is inherently focusable -->
133<a href="javascript:void(0);" onClick="doSomething();">Click ALL the things!</a>
134<!-- Good: buttons are inherently focusable -->
135<button onClick="doSomething();">Click the button :)</button>
136```
137
138### Fail
139
140```jsx
141<!-- Bad: span with onClick attribute has no tabindex -->
142<span onclick="submitForm();" role="button">Submit</span>
143<!-- Bad: anchor element without href is not focusable -->
144<a onclick="showNextPage();" role="button">Next page</a>
145```
146
147## Accessibility guidelines
148
149- [WCAG 2.1.1](https://www.w3.org/WAI/WCAG21/Understanding/keyboard)
150
151### Resources
152
153- [Chrome Audit Rules, AX_FOCUS_02](https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_focus_02)
154- [Mozilla Developer Network - ARIA Techniques](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role#Keyboard_and_focus)
155- [Fundamental Keyboard Navigation Conventions](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_generalnav)
156- [WAI-ARIA Authoring Practices Guide - Design Patterns and Widgets](https://www.w3.org/TR/wai-aria-practices-1.1/#aria_ex)
Note: See TracBrowser for help on using the repository browser.