Skip to content

Commit 0abf83c

Browse files
Update "focusable element has no keyboard trap" examples to not use single key shortcuts (80af7b) (#1794)
* Made editorial update * Updated examples to not use single key shortcut * Update example descriptions * Moved note to background * Moved notes to background * Update the descriptions of examples * Improved readability of the examples * Removed note from background * Fixed typo * Move scripts to test-assets * Update focusable-no-keyboard-trap-80af7b.md * Fix html * Update _rules/focusable-no-keyboard-trap-80af7b.md Co-authored-by: Kathy Eng <kengdoj@users.noreply.github.com> * Update _rules/focusable-no-keyboard-trap-non-standard-nav-ebe86a.md Co-authored-by: Kathy Eng <kengdoj@users.noreply.github.com> Co-authored-by: Kathy Eng <kengdoj@users.noreply.github.com>
1 parent d52fcf0 commit 0abf83c

4 files changed

Lines changed: 103 additions & 149 deletions

File tree

_rules/focusable-no-keyboard-trap-80af7b.md

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ acknowledgments:
3535

3636
## Applicability
3737

38-
This rule only applies to any [HTML or SVG element][] that is [focusable][].
39-
40-
**Note:** This rule only applies to HTML and SVG. Thus, it is a partial check for WCAG 2.0 success criterion 2.1.2, which applies to all content.
38+
This rule applies to any [HTML or SVG element][] that is [focusable][].
4139

4240
## Expectation
4341

@@ -68,97 +66,79 @@ _There are no major accessibility support issues known for this rule._
6866

6967
#### Passed Example 1
7068

71-
No trap for keyboard navigation.
69+
These [focusable][] elements do not create a trap for keyboard navigation.
7270

7371
```html
7472
<a href="#">Link 1</a> <button>Button1</button>
7573
```
7674

7775
#### Passed Example 2
7876

79-
Using `tabindex="1"`.
77+
This element is made [focusable][] by the `tabindex` attribute. It does not create a trap for keyboard navigation.
8078

8179
```html
8280
<div tabindex="1">Text</div>
8381
```
8482

8583
#### Passed Example 3
8684

87-
Using `tabindex="-1"`.
85+
This element is made [focusable][] by the `tabindex` attribute, even if it is not part of the sequential focus navigation. It does not create a trap for keyboard navigation.
8886

8987
```html
9088
<div tabindex="-1">Text</div>
9189
```
9290

9391
#### Passed Example 4
9492

95-
Keyboard trap with help information in a paragraph before, and where the method advised works.
93+
These focusable `button` elements have scripts that create a keyboard trap. The document includes help information in a paragraph before the `button` elements and the method advised works to escape the keyboard trap.
9694

9795
```html
98-
<script>
99-
var trapOn = false
100-
</script>
96+
<script src="/test-assets/focusable-no-keyboard-trap/keyboard.js"></script>
10197

102-
<p>Press the M-key to Exit</p>
98+
<p>Press Ctrl+M to Exit</p>
10399
<a id="link1" href="#">Link 1</a>
104-
<button id="btn1" onblur="(function(e){trapOn=true; document.getElementById('btn2').focus();})(event)">
100+
<button id="btn1" onfocus="trapOn = true" onblur="moveFocusToButton('btn2')" onkeydown="escapeTrapOnCtrlM(event)">
105101
Button 1
106102
</button>
107-
<button
108-
id="btn2"
109-
onkeydown="(function(e){ if (e.keyCode === 77){trapOn=false;document.getElementById('link2').focus();}})(event)"
110-
onblur="(function(e){ if(trapOn){document.getElementById('btn1').focus();}})(event)"
111-
>
103+
<button id="btn2" onfocus="trapOn = true" onblur="moveFocusToButton('btn1')" onkeydown="escapeTrapOnCtrlM(event)">
112104
Button 2
113105
</button>
114106
<a id="link2" href="#">Link 2</a>
115107
```
116108

117109
#### Passed Example 5
118110

119-
Keyboard trap with help information within the trap, and where the method advised works.
111+
These focusable `button` elements have scripts that create a keyboard trap. The document includes help information within the trap and the method advised works to escape the keyboard trap.
120112

121113
```html
122-
<script>
123-
var trapOn = false
124-
</script>
114+
<script src="/test-assets/focusable-no-keyboard-trap/keyboard.js"></script>
125115

126116
<a id="link1" href="#">Link 1</a>
127-
<button id="btn1" onblur="(function(e){trapOn=true; document.getElementById('btn2').focus();})(event)">
117+
<button id="btn1" onfocus="trapOn = true" onblur="moveFocusToButton('btn2')" onkeydown="escapeTrapOnCtrlM(event)">
128118
Button 1
129119
</button>
130-
<p>Press the M-key to Exit</p>
131-
<button
132-
id="btn2"
133-
onkeydown="(function(e){ if (e.keyCode === 77){trapOn=false;document.getElementById('link2').focus();}})(event)"
134-
onblur="(function(e){ if(trapOn){document.getElementById('btn1').focus();}})(event)"
135-
>
120+
<p>Press Ctrl+M to Exit</p>
121+
<button id="btn2" onfocus="trapOn = true" onblur="moveFocusToButton('btn1')" onkeydown="escapeTrapOnCtrlM(event)">
136122
Button 2
137123
</button>
138124
<a id="link2" href="#">Link 2</a>
139125
```
140126

141127
#### Passed Example 6
142128

143-
Keyboard trap with "help" link that once clicked exposes the instructions.
129+
These focusable `button` elements have scripts that create a keyboard trap. The document includes help information in a "help" link that once clicked exposes the instructions to escape the keyboard trap.
144130

145131
```html
146-
<script>
147-
var trapOn = false
148-
149-
function showHelpText() {
150-
document.getElementById('helptext').innerHTML = '<p>Press the M-key to Exit</p>'
151-
}
152-
</script>
132+
<script src="/test-assets/focusable-no-keyboard-trap/keyboard.js"></script>
153133

154-
<div onkeydown="(function(e){ if (e.keyCode === 77){trapOn=false;document.getElementById('link2').focus();}})(event)">
134+
<div onkeydown="escapeTrapOnCtrlM(event)">
155135
<a id="link1" href="#">Link 1</a>
156-
<button id="btn1" onblur="(function(e){trapOn=true; document.getElementById('helpLink').focus();})(event)">
136+
<button id="btn1" onfocus="trapOn = true" onblur="moveFocusTo('helpLink')">
157137
Button 1
158138
</button>
159139
<a id="helpLink" href="#" onclick="showHelpText()">How to go the next element</a>
160140
<div id="helptext"></div>
161-
<button id="btn2" onblur="(function(e){ if(trapOn){document.getElementById('btn1').focus();}})(event)">
141+
<button id="btn2" onblur="moveFocusTo('btn1')">
162142
Button 2
163143
</button>
164144
</div>
@@ -169,7 +149,7 @@ Keyboard trap with "help" link that once clicked exposes the instructions.
169149

170150
#### Failed Example 1
171151

172-
Keyboard trap one element.
152+
This [focusable][] element creates a keyboard trap bringing focus to the `button`.
173153

174154
```html
175155
<a href="#">Link 1</a>
@@ -180,7 +160,7 @@ Keyboard trap one element.
180160

181161
#### Failed Example 2
182162

183-
Keyboard trap group.
163+
These [focusable][] `button` elements create a keyboard trap preventing the last `button` to be reached using the keyboard.
184164

185165
```html
186166
<button onblur="setTimeout(() => this.nextElementSibling.focus(), 10)">
@@ -196,7 +176,7 @@ Keyboard trap group.
196176

197177
#### Failed Example 3
198178

199-
A [focusable][] element between keyboard traps.
179+
This `button` element is between other `button` elements creating a keyboard trap.
200180

201181
```html
202182
<button onblur="setTimeout(() => this.focus(), 10)">Button 1</button>
@@ -206,66 +186,52 @@ A [focusable][] element between keyboard traps.
206186

207187
#### Failed Example 4
208188

209-
Keyboard trap with no instructions.
189+
These focusable `button` elements create a keyboard trap with no instructions.
210190

211191
```html
212-
<script>
213-
var trapOn = false
214-
</script>
192+
<script src="/test-assets/focusable-no-keyboard-trap/keyboard.js"></script>
215193

216194
<a id="link1" href="#">Link 1</a>
217-
<button id="btn1" onblur="(function(e){trapOn=true; document.getElementById('btn2').focus();})(event)">
195+
<button id="btn1" onfocus="trapOn = true" onblur="moveFocusToButton('btn2')" onkeydown="escapeTrapOnCtrlM(event)">
218196
Button 1
219197
</button>
220-
<button
221-
id="btn2"
222-
onkeydown="(function(e){ if (e.keyCode === 77){trapOn=false;document.getElementById('link2').focus();}})(event)"
223-
onblur="(function(e){ if(trapOn){document.getElementById('btn1').focus();}})(event)"
224-
>
198+
<button id="btn2" onfocus="trapOn = true" onblur="moveFocusToButton('btn1')" onkeydown="escapeTrapOnCtrlM(event)">
225199
Button 2
226200
</button>
227201
<a id="link2" href="#">Link 2</a>
228202
```
229203

230204
#### Failed Example 5
231205

232-
Keyboard trap with instructions that doesn't give advise on the method for proceeding.
206+
These focusable `button` elements create a keyboard trap with instructions that don't give advice on the method for proceeding.
233207

234208
```html
235-
<script>
236-
var trapOn = false
237-
</script>
209+
<script src="/test-assets/focusable-no-keyboard-trap/keyboard.js"></script>
238210

239211
<p>Go to the next element</p>
240212
<a id="link1" href="#">Link 1</a>
241-
<button id="btn1" onblur="(function(e){trapOn=true; document.getElementById('btn2').focus();})(event)">
213+
<button id="btn1" onfocus="trapOn = true" onblur="moveFocusToButton('btn2')" onkeydown="escapeTrapOnCtrlM(event)">
242214
Button 1
243215
</button>
244-
<button
245-
id="btn2"
246-
onkeydown="(function(e){ if (e.keyCode === 77){trapOn=false;document.getElementById('link2').focus();}})(event)"
247-
onblur="(function(e){ if(trapOn){document.getElementById('btn1').focus();}})(event)"
248-
>
216+
<button id="btn2" onfocus="trapOn = true" onblur="moveFocusToButton('btn1')" onkeydown="escapeTrapOnCtrlM(event)">
249217
Button 2
250218
</button>
251219
<a id="link2" href="#">Link 2</a>
252220
```
253221

254222
#### Failed Example 6
255223

256-
Keyboard trap with help text, where the method advised doesn't work.
224+
These focusable `button` elements create a keyboard trap with help text, where the method advised doesn't work.
257225

258226
```html
259-
<script>
260-
var trapOn = false
261-
</script>
227+
<script src="/test-assets/focusable-no-keyboard-trap/keyboard.js"></script>
262228

263229
<a id="link1" href="#">Link 1</a>
264-
<button id="btn1" onblur="(function(e){trapOn=true; document.getElementById('btn2').focus();})(event)">
230+
<button id="btn1" onfocus="trapOn = true" onblur="moveFocusToButton('btn2')">
265231
Button 1
266232
</button>
267-
<p>Press the M-key to Exit</p>
268-
<button id="btn2" onblur="(function(e){ if(trapOn){document.getElementById('btn1').focus();}})(event)">
233+
<p>Press Ctrl+M to Exit</p>
234+
<button id="btn2" onfocus="trapOn = true" onblur="moveFocusToButton('btn1')">
269235
Button 2
270236
</button>
271237
<a id="link2" href="#">Link 2</a>
@@ -275,31 +241,31 @@ Keyboard trap with help text, where the method advised doesn't work.
275241

276242
#### Inapplicable Example 1
277243

278-
No [focusable][] element.
244+
There is no [focusable][] element.
279245

280246
```html
281247
<h1>Page 1</h1>
282248
```
283249

284250
#### Inapplicable Example 2
285251

286-
Disabled element.
252+
There is no [focusable][] element.
287253

288254
```html
289255
<button type="button" disabled>Click Me!</button>
290256
```
291257

292258
#### Inapplicable Example 3
293259

294-
Hidden element using `display:none`.
260+
There is no [focusable][] element.
295261

296262
```html
297263
<button type="button" style="display:none;">Click Me!</button>
298264
```
299265

300266
#### Inapplicable Example 4
301267

302-
Hidden element using `visibility:hidden`.
268+
There is no [focusable][] element.
303269

304270
```html
305271
<a href="#" style="visibility:hidden;">Link 1</a> <button style="visibility:hidden;">Button1</button>

0 commit comments

Comments
 (0)