Skip to content

Commit 5b5de5e

Browse files
committed
Add a passing modal example
1 parent b271c8e commit 5b5de5e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

_rules/focusable-no-keyboard-trap-standard-nav-a1b64e.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,54 @@ This element is made [focusable][] by the `tabindex` attribute, even if it is no
7979
<div tabindex="-1">Text</div>
8080
```
8181

82+
#### Passed Example 4
83+
84+
While the elements with id "sentinelBefore" and "sentinelAfter" contain focus to the contents of the div with name "Sample Modal", focus is not trapped since the user can
85+
use [standard keyboard navigation](#standard-keyboard-navigation) using the Escape key or by activating the "Close button" to dismiss the modal
86+
87+
```html
88+
<div>Main page content with <a href="#">some link</a></div>
89+
<div aria-hidden="true">
90+
<a href="#" id="sentinelBefore" style="position:absolute; top:-999em"
91+
>Upon receiving focus, this focus sentinel should wrap focus to the bottom of the modal</a
92+
>
93+
</div>
94+
<div
95+
id="sampleModal"
96+
role="dialog"
97+
aria-label="Sample Modal"
98+
aria-modal="true"
99+
style="border: solid black 1px; padding: 1rem;"
100+
>
101+
<label>First and last name <input id="dialogFirst"/></label><br />
102+
<button id="closeButton">Close button</button>
103+
</div>
104+
<div aria-hidden="true">
105+
<a href="#" id="sentinelAfter" style="position:absolute; top:-999em"
106+
>Upon receiving focus, this focus sentinel should wrap focus to the top of the modal</a
107+
>
108+
</div>
109+
<script>
110+
window.addEventListener('load', () => {
111+
document.getElementById('dialogFirst').focus();
112+
})
113+
document.getElementById('sentinelBefore').addEventListener('focus', () => {
114+
document.getElementById('closeButton').focus()
115+
})
116+
document.getElementById('sentinelAfter').addEventListener('focus', () => {
117+
document.getElementById('dialogFirst').focus()
118+
})
119+
document.getElementById('closeButton').addEventListener('click', () => {
120+
document.getElementById('sampleModal').style.display = 'none'
121+
})
122+
document.getElementById('sampleModal').addEventListener('keydown', (evt) => {
123+
if (evt.key === "Escape") {
124+
document.getElementById('sampleModal').style.display = 'none';
125+
}
126+
})
127+
</script>
128+
```
129+
82130
### Failed
83131

84132
#### Failed Example 1

0 commit comments

Comments
 (0)