-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal-dialog-complete.html
More file actions
149 lines (129 loc) · 3.94 KB
/
Copy pathmodal-dialog-complete.html
File metadata and controls
149 lines (129 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AccessU Accessible JavaScript</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; font-size:1.2em;}
#legacyDialog{
height:300px;
width:300px;
position:absolute;
top:200px;
left:40%;
background:lightgrey;
display:none;
}
</style>
</head>
<body>
<h1>Modal Dialog test</h1>
<!-- Non-HTML dialog (legacy) -->
<button id="legacyOpen">Open the legacy dialog</button>
<div id="legacyDialog">
<p>This is a legacy modal Dialog</p>
<form>
<label>Input 1
<input type="text">
</label>
<fieldset>
<label>
<input type="checkbox">
Check 1
</label>
<label>
<input type="checkbox">
Check 2
</label>
<label>
<input type="checkbox">
Check 3
</label>
</fieldset>
<button type="submit">Submit</button>
</form>
<button id="legacyClose">Close</button>
</div>
<!-- HTML Dialog open button -->
<button id="htmlOpen">Open the HTML dialog</button>
<!-- HTML Dialog element -->
<dialog id="htmlDialog">
<p>This is an HTML modal Dialog</p>
<form>
<label>Input 1
<input type="text">
</label>
<fieldset>
<label>
<input type="checkbox">
Check 1
</label>
<label>
<input type="checkbox">
Check 2
</label>
<label>
<input type="checkbox">
Check 3
</label>
</fieldset>
<button type="submit">Submit</button>
</form>
<button id="htmlClose">Close</button>
</dialog>
<script>
// Legacy dialog implementation
const legacyOpen = document.getElementById("legacyOpen");
const legacyDialog = document.getElementById("legacyDialog");
let returnEl;
// Write a function that opens the modal window
legacyOpen.addEventListener("click", function(){
legacyDialog.style.display = "block";
returnEl = event.target;
keyboardTrap(legacyDialog);
});
legacyClose.addEventListener("click", function(){
legacyDialog.style.display = "none";
returnEl.focus();
});
// Write a function to trap keyboard focus inside the modal
function keyboardTrap(activeEl){
// spacing in attribute selector needs to match HTML exactly.
const allElements = "button:not([style='display:none;']), [href], input, select, textarea, [tabindex]:not([tabindex='1'])";
const modalElements = activeEl.querySelectorAll(allElements);
// console.log(modalElements, typeof modalElements)
const firstModalEl = modalElements[0];
const lastModalEl = modalElements[modalElements.length - 1];
firstModalEl.focus();
document.addEventListener("keydown", function(){
if(event.keyCode == 9){
if(document.activeElement === firstModalEl && event.shiftKey){
lastModalEl.focus();
event.preventDefault();
return false;
}
if(document.activeElement === lastModalEl && !event.shiftKey) {
firstModalEl.focus();
event.preventDefault();
return false;
}
}
})
}
// Add an event listener for escape key and close modal, return focus to launch button
// HTML dialog!
// built in keyboard trap and escape key
const htmlDialog = document.getElementById("htmlDialog");
const htmlOpen = document.getElementById("htmlOpen");
const htmlClose = document.getElementById("htmlClose");
// Add click event to the modal open button
htmlOpen.addEventListener("click", () =>{
htmlDialog.showModal();
});
htmlClose.addEventListener("click", () =>{
htmlDialog.close();
});
</script>
</body>
</html>