Skip to content

Commit dd21617

Browse files
authored
Create updated tests for APG design pattern example: Command Button (#683)
* Update timestamped example directory and references.csv * Remove previous example * Example: remove unneeded CSS/JS references and update page title * Example: remove 'Related Links' nav and similar example links, as well as 'Accessibility Features', 'Keyboard Support', 'Role, Property, State, and Tabindex Attributes' and source code sections and footer nav * Generate .html source files with scripts automatically Co-authored-by: IsaDC <IsaDC@users.noreply.github.com>
1 parent 0063eec commit dd21617

File tree

7 files changed

+196
-74
lines changed

7 files changed

+196
-74
lines changed

tests/command-button/data/references.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ refId,value
22
author,Isabel Del Castillo
33
authorEmail,isa.delcastillo5@gmail.com
44
title,Command Button Example
5-
reference,reference/2021-3-29_132754/button.html
5+
reference,reference/2022-4-8_124112/button.html
66
designPattern,https://w3c.github.io/aria-practices/#button
77
example,https://w3c.github.io/aria-practices/examples/button/button.html
88
button,https://w3c.github.io/aria/#button
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
3-
<head>
4-
<meta charset="utf-8">
5-
<title>Command Button Example</title>
6-
7-
<!-- CSS and JS for this example. -->
8-
<link rel="stylesheet" href="css/button.css">
9-
</head>
10-
<body>
11-
<main>
12-
<h1>Command Button Example</h1>
13-
14-
<p>
15-
The following command button example demonstrates the <a href="https://w3c.github.io/aria-practices/#button">button design pattern.</a>
16-
</p>
17-
18-
<section>
19-
<div class="example-header">
20-
<h2 id="ex_label">Example</h2>
21-
</div>
22-
23-
<div role="separator" id="ex_start_sep" aria-labelledby="ex_start_sep ex_label" aria-label="Start of"></div>
24-
25-
<div id="example">
26-
<p>This <q>Print</q> action button uses a <code>div</code> element.</p>
27-
<a id="beforelink" href="#beforelink">Navigate forwards from here</a>
28-
<div tabindex="0" role="button" id="action">Print Page</div>
29-
<a id="afterlink" href="#afterlink">Navigate backwards from here</a>
30-
</div>
31-
32-
<div role="separator" id="ex_end_sep" aria-labelledby="ex_end_sep ex_label" aria-label="End of"></div>
33-
</section>
34-
</main>
35-
</body>
36-
</html>
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Command Button Example</title>
6+
7+
<!-- CSS and JS for this example. -->
8+
<link rel="stylesheet" href="css/button.css">
9+
<script src="js/button.js"></script>
10+
</head>
11+
<body>
12+
<main>
13+
<h1>Command Button Example</h1>
14+
15+
<p>
16+
The following command button example demonstrates the <a href="https://w3c.github.io/aria-practices/#button">button design pattern.</a>
17+
</p>
18+
19+
<section>
20+
<div class="example-header">
21+
<h2 id="ex_label">Example</h2>
22+
</div>
23+
24+
<div role="separator" id="ex_start_sep" aria-labelledby="ex_start_sep ex_label" aria-label="Start of"></div>
25+
26+
<div id="example">
27+
<p>This <q>Print</q> action button uses a <code>div</code> element.</p>
28+
<a id="beforelink" href="#beforelink">Navigate forwards from here</a>
29+
<div tabindex="0" role="button" id="action">Print Page</div>
30+
<a id="afterlink" href="#afterlink">Navigate backwards from here</a>
31+
</div>
32+
33+
<div role="separator" id="ex_end_sep" aria-labelledby="ex_end_sep ex_label" aria-label="End of"></div>
34+
</section>
35+
</main>
36+
</body>
37+
</html>

tests/command-button/reference/2021-3-29_132754/button.setFocusAfterButton.html renamed to tests/command-button/reference/2022-4-8_124112/button.setFocusAfterButton.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<!-- CSS and JS for this example. -->
88
<link rel="stylesheet" href="css/button.css">
9+
<script src="js/button.js"></script>
910

1011
<!-- Generated by create-example-tests.js -->
1112
<script>

tests/command-button/reference/2021-3-29_132754/button.setFocusBeforeButton.html renamed to tests/command-button/reference/2022-4-8_124112/button.setFocusBeforeButton.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<!-- CSS and JS for this example. -->
88
<link rel="stylesheet" href="css/button.css">
9+
<script src="js/button.js"></script>
910

1011
<!-- Generated by create-example-tests.js -->
1112
<script>

tests/command-button/reference/2021-3-29_132754/button.setFocusOnButton.html renamed to tests/command-button/reference/2022-4-8_124112/button.setFocusOnButton.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<!-- CSS and JS for this example. -->
88
<link rel="stylesheet" href="css/button.css">
9+
<script src="js/button.js"></script>
910

1011
<!-- Generated by create-example-tests.js -->
1112
<script>

tests/command-button/reference/2021-3-29_132754/css/button.css renamed to tests/command-button/reference/2022-4-8_124112/css/button.css

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
display: inline-block;
33
position: relative;
44
padding: 0.4em 0.7em;
5-
border: 1px solid hsl(213, 71%, 49%);
5+
border: 1px solid hsl(213deg 71% 49%);
66
border-radius: 5px;
7-
box-shadow: 0 1px 2px hsl(216, 27%, 55%);
7+
box-shadow: 0 1px 2px hsl(216deg 27% 55%);
88
color: #fff;
9-
text-shadow: 0 -1px 1px hsl(216, 27%, 25%);
10-
background-color: hsl(216, 82%, 51%);
9+
text-shadow: 0 -1px 1px hsl(216deg 27% 25%);
10+
background-color: hsl(216deg 82% 51%);
1111
background-image: linear-gradient(
1212
to bottom,
13-
hsl(216, 82%, 53%),
14-
hsl(216, 82%, 47%)
13+
hsl(216deg 82% 53%),
14+
hsl(216deg 82% 47%)
1515
);
1616
}
1717

1818
[role="button"]:hover {
19-
border-color: hsl(213, 71%, 29%);
20-
background-color: hsl(216, 82%, 31%);
19+
border-color: hsl(213deg 71% 29%);
20+
background-color: hsl(216deg 82% 31%);
2121
background-image: linear-gradient(
2222
to bottom,
23-
hsl(216, 82%, 33%),
24-
hsl(216, 82%, 27%)
23+
hsl(216deg 82% 33%),
24+
hsl(216deg 82% 27%)
2525
);
2626
cursor: default;
2727
}
@@ -39,72 +39,72 @@
3939
right: calc(-1px - 3px - 3px);
4040
bottom: calc(-1px - 3px - 3px);
4141
left: calc(-1px - 3px - 3px);
42-
border: 3px solid hsl(213, 71%, 49%);
42+
border: 3px solid hsl(213deg 71% 49%);
4343

4444
/* button border radius + outline width + offset */
4545
border-radius: calc(5px + 3px + 3px);
4646
content: "";
4747
}
4848

4949
[role="button"]:active {
50-
border-color: hsl(213, 71%, 49%);
51-
background-color: hsl(216, 82%, 31%);
50+
border-color: hsl(213deg 71% 49%);
51+
background-color: hsl(216deg 82% 31%);
5252
background-image: linear-gradient(
5353
to bottom,
54-
hsl(216, 82%, 53%),
55-
hsl(216, 82%, 47%)
54+
hsl(216deg 82% 53%),
55+
hsl(216deg 82% 47%)
5656
);
57-
box-shadow: inset 0 3px 5px 1px hsl(216, 82%, 30%);
57+
box-shadow: inset 0 3px 5px 1px hsl(216deg 82% 30%);
5858
}
5959

6060
[role="button"][aria-pressed] {
61-
border-color: hsl(261, 71%, 49%);
62-
box-shadow: 0 1px 2px hsl(261, 27%, 55%);
63-
text-shadow: 0 -1px 1px hsl(261, 27%, 25%);
64-
background-color: hsl(261, 82%, 51%);
61+
border-color: hsl(261deg 71% 49%);
62+
box-shadow: 0 1px 2px hsl(261deg 27% 55%);
63+
text-shadow: 0 -1px 1px hsl(261deg 27% 25%);
64+
background-color: hsl(261deg 82% 51%);
6565
background-image: linear-gradient(
6666
to bottom,
67-
hsl(261, 82%, 53%),
68-
hsl(261, 82%, 47%)
67+
hsl(261deg 82% 53%),
68+
hsl(261deg 82% 47%)
6969
);
7070
}
7171

7272
[role="button"][aria-pressed]:hover {
73-
border-color: hsl(261, 71%, 29%);
74-
background-color: hsl(261, 82%, 31%);
73+
border-color: hsl(261deg 71% 29%);
74+
background-color: hsl(261deg 82% 31%);
7575
background-image: linear-gradient(
7676
to bottom,
77-
hsl(261, 82%, 33%),
78-
hsl(261, 82%, 27%)
77+
hsl(261deg 82% 33%),
78+
hsl(261deg 82% 27%)
7979
);
8080
}
8181

8282
[role="button"][aria-pressed="true"] {
8383
padding-top: 0.5em;
8484
padding-bottom: 0.3em;
85-
border-color: hsl(261, 71%, 49%);
86-
background-color: hsl(261, 82%, 31%);
85+
border-color: hsl(261deg 71% 49%);
86+
background-color: hsl(261deg 82% 31%);
8787
background-image: linear-gradient(
8888
to bottom,
89-
hsl(261, 82%, 63%),
90-
hsl(261, 82%, 57%)
89+
hsl(261deg 82% 63%),
90+
hsl(261deg 82% 57%)
9191
);
92-
box-shadow: inset 0 3px 5px 1px hsl(261, 82%, 30%);
92+
box-shadow: inset 0 3px 5px 1px hsl(261deg 82% 30%);
9393
}
9494

9595
[role="button"][aria-pressed="true"]:hover {
96-
border-color: hsl(261, 71%, 49%);
97-
background-color: hsl(261, 82%, 31%);
96+
border-color: hsl(261deg 71% 49%);
97+
background-color: hsl(261deg 82% 31%);
9898
background-image: linear-gradient(
9999
to bottom,
100-
hsl(261, 82%, 43%),
101-
hsl(261, 82%, 37%)
100+
hsl(261deg 82% 43%),
101+
hsl(261deg 82% 37%)
102102
);
103-
box-shadow: inset 0 3px 5px 1px hsl(261, 82%, 20%);
103+
box-shadow: inset 0 3px 5px 1px hsl(261deg 82% 20%);
104104
}
105105

106106
[role="button"][aria-pressed]:focus::before {
107-
border-color: hsl(261, 71%, 49%);
107+
border-color: hsl(261deg 71% 49%);
108108
}
109109

110110
[role="button"] svg {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* This content is licensed according to the W3C Software License at
3+
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
4+
*
5+
* JS code for the button design pattern
6+
*/
7+
8+
'use strict';
9+
10+
var ICON_MUTE_URL = '#icon-mute';
11+
var ICON_SOUND_URL = '#icon-sound';
12+
13+
function init() {
14+
var actionButton = document.getElementById('action');
15+
actionButton.addEventListener('click', activateActionButton);
16+
actionButton.addEventListener('keydown', actionButtonKeydownHandler);
17+
actionButton.addEventListener('keyup', actionButtonKeyupHandler);
18+
19+
var toggleButton = document.getElementById('toggle');
20+
toggleButton.addEventListener('click', toggleButtonClickHandler);
21+
toggleButton.addEventListener('keydown', toggleButtonKeydownHandler);
22+
toggleButton.addEventListener('keyup', toggleButtonKeyupHandler);
23+
}
24+
25+
/**
26+
* Activates the action button with the enter key.
27+
*
28+
* @param {KeyboardEvent} event
29+
*/
30+
function actionButtonKeydownHandler(event) {
31+
// The action button is activated by space on the keyup event, but the
32+
// default action for space is already triggered on keydown. It needs to be
33+
// prevented to stop scrolling the page before activating the button.
34+
if (event.keyCode === 32) {
35+
event.preventDefault();
36+
}
37+
// If enter is pressed, activate the button
38+
else if (event.keyCode === 13) {
39+
event.preventDefault();
40+
activateActionButton();
41+
}
42+
}
43+
44+
/**
45+
* Activates the action button with the space key.
46+
*
47+
* @param {KeyboardEvent} event
48+
*/
49+
function actionButtonKeyupHandler(event) {
50+
if (event.keyCode === 32) {
51+
event.preventDefault();
52+
activateActionButton();
53+
}
54+
}
55+
56+
function activateActionButton() {
57+
window.print();
58+
}
59+
60+
/**
61+
* Toggles the toggle button’s state if it’s actually a button element or has
62+
* the `role` attribute set to `button`.
63+
*
64+
* @param {MouseEvent} event
65+
*/
66+
function toggleButtonClickHandler(event) {
67+
if (
68+
event.currentTarget.tagName === 'button' ||
69+
event.currentTarget.getAttribute('role') === 'button'
70+
) {
71+
toggleButtonState(event.currentTarget);
72+
}
73+
}
74+
75+
/**
76+
* Toggles the toggle button’s state with the enter key.
77+
*
78+
* @param {KeyboardEvent} event
79+
*/
80+
function toggleButtonKeydownHandler(event) {
81+
if (event.keyCode === 32) {
82+
event.preventDefault();
83+
} else if (event.keyCode === 13) {
84+
event.preventDefault();
85+
toggleButtonState(event.currentTarget);
86+
}
87+
}
88+
89+
/**
90+
* Toggles the toggle button’s state with space key.
91+
*
92+
* @param {KeyboardEvent} event
93+
*/
94+
function toggleButtonKeyupHandler(event) {
95+
if (event.keyCode === 32) {
96+
event.preventDefault();
97+
toggleButtonState(event.currentTarget);
98+
}
99+
}
100+
101+
/**
102+
* Toggles the toggle button’s state between *pressed* and *not pressed*.
103+
*
104+
* @param {HTMLElement} button
105+
*/
106+
function toggleButtonState(button) {
107+
var isAriaPressed = button.getAttribute('aria-pressed') === 'true';
108+
109+
button.setAttribute('aria-pressed', isAriaPressed ? 'false' : 'true');
110+
111+
var icon = button.querySelector('use');
112+
icon.setAttribute(
113+
'xlink:href',
114+
isAriaPressed ? ICON_SOUND_URL : ICON_MUTE_URL
115+
);
116+
}
117+
118+
window.onload = init;

0 commit comments

Comments
 (0)