-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoggle.html
73 lines (63 loc) · 3.09 KB
/
toggle.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Checkbox Demo</title>
<style type="text/css">
body{font-family:sans-serif;}
</style>
</head>
<body>
<div tabindex="0" id="myDiv" role="checkbox" onclick="ToggleMe();" oncommand="HandleCommand(event);" style="font:bold 96pt verdana;">
<img src="img/unchecked.jpg" alt="" />Awesome!
</div>
<script type="text/javascript">
var toggleState = false;
function HandleRequest(event) {
var myCheckbox = document.getElementById("myDiv");
if (event.attributeName === "checked" && event.target === myCheckbox) {
event.attributeValue = (toggleState ? "true" : "false");
}
}
function HandleCommand(event) {
var myCheckbox = document.getElementById("myDiv");
if (event.target === myCheckbox && event.commandName === "toggle") {
ToggleMe();
event.preventDefault();
}
}
function ToggleMe() {
var myCheckbox = document.getElementById("myDiv");
if(toggleState){
myCheckbox.children[0].setAttribute("src", "img/unchecked.jpg");
myCheckbox.setAttribute("aria-checked", "false");
}else{
myCheckbox.children[0].setAttribute("src", "img/checked.jpg");
myCheckbox.setAttribute("aria-checked", "true");
}
toggleState = !toggleState;
}
</script>
<h1>Checkbox Toggle Demo</h1>
<h2>How to Demo Me</h2>
<p>I only work in Edge on Windows 10 right now. I'd love it if other browsers supported me too.</p>
<ol>
<li>In Edge, go to about:flags and check “experimental accessibility features”</li>
<li>Restart Edge</li>
<li>Start Narrator (Windows+Enter).</li>
<li>Focus should be on the large checkbox, but if it isn't, use CAPSLOCK+Right Arrow to get into and explore the page until it is.</li>
<li>CAPSLOCK + Space.</li>
<li>The checkbox will check. Do it again, and it will uncheck.</li>
</ol>
<h2>Why is that interesting?</h2>
<p>The script that checks the checkbox is responding to a new javascript event <code>oncommand</code>.
The command event is sent by the Edge engine when UIA (the Accessibility API on Windows) sends an action,
in this case, Toggle action from the Toggle pattern. The Toggle pattern is implemented for checkboxes, whether they
are marked up with HTML or ARIA. When you used CAPSLOCK + Space in Narrator, Narrator called <code>Toggle.Toggle()</code> on UIA represenation of the checkbox,
and Edge sent that along to the DOM represenation of the checkbox, where it turned up as this new oncommand event.</p>
<p>The command event exposes <code>event.commandName</code>, in this case equal to "toggle," and the event handler
can do different things based on which command it got from UIA and Narrator.
In this case, it calls the same <code>ToggleMe()</code> function that the onclick handler calls, which swaps the
image and fixes up the ARIA properties. Narrator users get exactly the same behavior as mouse users.</p>
</body>
</html>