Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add switch example using HTML checkbox input #1895

Merged
merged 64 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
97f74a9
adding initial switch example using input[type=checkbox]
jongund May 7, 2021
02e388c
fixed spell checker issue
jongund May 7, 2021
91aeb0b
updated documentation
jongund May 7, 2021
c00b665
add exception for the HTML validator
jongund May 7, 2021
7f943cc
checked out menu-button_links.js to use same fix in add-jump-to branch
jongund May 13, 2021
77abf1d
restored original test file
jongund May 13, 2021
a32ebb7
updated examples for on/off labels to be in content and not CSS
jongund May 27, 2021
27f2ebe
updated CSS for example to make it simplier
jongund May 28, 2021
bf5ded6
updated code to include DIV with class example-header for code pen sp…
jongund Jul 15, 2021
2d403bc
updated .vnurc file
jongund Jul 15, 2021
218de1f
fixed label not to include visual on/off text
jongund Jul 27, 2021
8e5f3be
Merge branch 'main' into switch-checkbox
jongund Jul 27, 2021
e73bc13
fixed label bug using aria-hidden=true on on and off text
jongund Jul 27, 2021
a7b41e3
identifed validator bug
jongund Jul 27, 2021
e7d7788
documented validator bug
jongund Jul 27, 2021
1d86f3f
fixed code pen issue
jongund Jul 27, 2021
6b61219
removed Enter key support
jongund Aug 3, 2021
48851af
Revise title
mcking65 Aug 29, 2021
2ca24b8
Editorial revision to intro
mcking65 Aug 29, 2021
813cbf1
further clarification of intro
mcking65 Aug 29, 2021
02da47b
Editorial changes to accessibility features for clarity and brevity
mcking65 Aug 29, 2021
063e4a7
white space
mcking65 Aug 29, 2021
4b3f267
Merge remote-tracking branch 'origin/main' into switch-checkbox
mcking65 Aug 29, 2021
60ebb9f
Further editorial clarification of accessibility features documentation
mcking65 Aug 29, 2021
0308ff2
Revise state and property descriptions
mcking65 Aug 29, 2021
93d8996
Remove unnecessary list markup from kb table
mcking65 Aug 29, 2021
4456f7e
updated spacing discription
jongund Sep 7, 2021
399fbc1
updated accessibility documentation
jongund Sep 7, 2021
50cf98b
fixed linting error
jongund Sep 7, 2021
85041af
fixed linting error
jongund Sep 7, 2021
41a801c
updated accessibility documentation
jongund Sep 8, 2021
dd2535a
updated accessibility documentation
jongund Sep 8, 2021
4372458
updated reference table
jongund Sep 21, 2021
80e26b0
udpated documentation
jongund Sep 21, 2021
a60b782
updated accessibility documentation
jongund Sep 21, 2021
197e98a
Merge branch 'main' into switch-checkbox
jongund Sep 22, 2021
b05cace
Merge branch 'main' of github.com:w3c/aria-practices
jongund Sep 28, 2021
bf32c6f
Merge branch 'main' into switch-checkbox
jongund Sep 28, 2021
e0b872f
removed statement about synchronizing with the input.checked property…
jongund Sep 28, 2021
ef6a773
Editorial clarification of accessibility features
mcking65 Oct 3, 2021
1b3f2c7
comment out links to similar examples that are not yet merged
mcking65 Oct 3, 2021
4334d0e
minor editorial change to intro
mcking65 Oct 3, 2021
499acde
Merge remote-tracking branch 'origin/main' into switch-checkbox
mcking65 Oct 3, 2021
c6e44db
Add link to example from main document
mcking65 Oct 3, 2021
66b6a55
Resolve conflicts with main and merge latest from main
mcking65 Oct 4, 2021
1a009fc
Add links to examples from main doc and between example pages
mcking65 Oct 4, 2021
6044a71
Merge branch 'main' of github.com:w3c/aria-practices
jongund Oct 5, 2021
47041ce
Merge branch 'main' of github.com:w3c/aria-practices
jongund Oct 18, 2021
13351af
Merge branch 'main' of github.com:w3c/aria-practices
jongund Oct 26, 2021
7211c39
Merge in latest from main and resolve conflicts
mcking65 Oct 29, 2021
b761845
Add links among example pages for similar examples
mcking65 Oct 29, 2021
88733e1
Merge branch 'main' of github.com:w3c/aria-practices
jongund Oct 29, 2021
ac426b1
Merge branch 'main' of github.com:w3c/aria-practices
jongund Oct 31, 2021
9c8fae3
Merge remote-tracking branch 'origin/main' into switch-checkbox
mcking65 Nov 1, 2021
b613279
Merge branch 'main' of github.com:w3c/aria-practices
jongund Nov 1, 2021
5d41df7
Merge branch 'main' into switch-checkbox
jongund Nov 1, 2021
fcc1cc4
Change /> to >
mcking65 Nov 7, 2021
2b088d8
Change /> to >
mcking65 Nov 7, 2021
f31b97c
Change /> to >
mcking65 Nov 7, 2021
2e7ee8b
Change /> to >
mcking65 Nov 7, 2021
de13911
Merge branch 'main' of github.com:w3c/aria-practices
jongund Nov 8, 2021
562fe03
Merge branch 'main' into switch-checkbox
jongund Nov 8, 2021
13face8
updated CSS properties to remove -moz-user-select and -ms-user-select
jongund Nov 8, 2021
558fc72
Remove more -moz- and -ms- properties
zcorpan Nov 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vnurc
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ Section lacks heading. Consider using “h2”-“h6” elements to add identify
# https://github.com/validator/validator/issues/1096
Bad value “none” for attribute “role” on element “svg”.
Bad value “presentation” for attribute “role” on element “svg”.
# https://github.com/validator/validator/issues/1122
Element “input” is missing required attribute “aria-checked”.
6 changes: 2 additions & 4 deletions aria-practices.html
Original file line number Diff line number Diff line change
Expand Up @@ -2581,11 +2581,9 @@ <h3>Switch</h3>
<section class="notoc">
<h4>Examples</h4>
<ul>
<li><a href="examples/switch/switch.html">Switch Example</a>: A switch based on a <code>div</code> element that turns a notification preference on and off.</li>
<li><a href="examples/switch/switch.html">Switch Example</a>: A switch based on a <code>div</code> element that turns a notification preference on and off.</li>
<li><a href="examples/switch/switch-button.html">Switch Example Using HTML Button</a>: A group of 2 switches based on HTML <code>button</code> elements that turn lights on and off.</li>
<!-- comment out examples not yet merged.
<li><a href="examples/switch/switch-checkbox.html">Switch using <code>input[type=&quot;checkbox&quot;]</code> element Example</a>: Demonstrates switches used for turning on/off accessibility preferences.</li>
-->
<li><a href="examples/switch/switch-checkbox.html">Switch Example Using HTML Checkbox Input</a>: A group of 2 switches based on HTML <code>input[type="checkbox"]</code> elements that turn accessibility preferences on and off.</li>
</ul>
</section>

Expand Down
2 changes: 2 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ <h2 id="examples_by_role_label">Examples by Role</h2>
<td>
<ul>
<li><a href="switch/switch-button.html">Switch Using HTML Button</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="switch/switch-checkbox.html">Switch Using HTML Checkbox Input</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="switch/switch.html">Switch</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
</ul>
</td>
Expand Down Expand Up @@ -624,6 +625,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<li><a href="slider/slider-temperature.html">Vertical Temperature Slider</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="spinbutton/datepicker-spinbuttons.html">Date Picker Spin Button</a></li>
<li><a href="switch/switch-button.html">Switch Using HTML Button</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="switch/switch-checkbox.html">Switch Using HTML Checkbox Input</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="switch/switch.html">Switch</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="table/sortable-table.html">Sortable Table</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="toolbar/toolbar.html">Toolbar</a></li>
Expand Down
87 changes: 87 additions & 0 deletions examples/switch/css/switch-checkbox.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
fieldset {
width: 22em;
}

legend {
font-size: 110%;
}

label {
display: block;
margin: 0.5em;
padding: 4px 4px 6px 6px;
border: 0 solid #005a9c;
border-radius: 5px;
width: 16em;
}

label .label {
display: inline-block;
width: 9em;
-webkit-user-select: none;
user-select: none;
}

label input[role="switch"] {
opacity: 0;
}

label input[role="switch"] ~ .state {
display: inline-block;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
zcorpan marked this conversation as resolved.
Show resolved Hide resolved
user-select: none;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't follow https://github.com/w3c/aria-practices/wiki/Code-Guide#prefixed-properties

Should we remove that section of the code guide?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zcorpan based on our Nov 2 discussion,I think we still need this for webkit. I think @jongund is looking into whether the others can be removed from this example without issue. Do we need a separate issue for conversation about what to do for prefix guidance in the code guide?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can track it in #2110

}

label input[role="switch"] ~ .state > .container {
position: relative;
top: 2px;
display: inline-block;
border: 2px solid black;
width: 40px;
height: 20px;
border-radius: 11px;
}

label input[role="switch"] ~ .state > .container > .position {
position: relative;
top: 1px;
left: 2px;
display: inline-block;
border: 2px solid black;
border-radius: 9px;
width: 14px;
height: 14px;
background: black;
opacity: 0.6;
}

label input[role="switch"]:not(:checked) ~ .state span.on {
display: none;
}

label input[role="switch"]:checked ~ .state > span.off {
display: none;
}

label input[role="switch"]:checked ~ .state > .container > .position {
left: 20px;
border-color: green;
background: green;
opacity: 1;
}

label.focus,
label:hover {
padding: 2px 2px 4px 4px;
border-width: 2px;
outline: none;
background-color: #def;
cursor: pointer;
}

label.focus span.container,
label:hover span.container {
background-color: white;
}
34 changes: 34 additions & 0 deletions examples/switch/js/switch-checkbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This content is licensed according to the W3C Software License at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*
* File: switch-checkbox.js
*
* Desc: Switch widget using input[type=checkbox] that implements ARIA Authoring Practices
*/

'use strict';

class CheckboxSwitch {
constructor(domNode) {
this.switchNode = domNode;
this.switchNode.addEventListener('focus', () => this.onFocus(event));
this.switchNode.addEventListener('blur', () => this.onBlur(event));
}

onFocus(event) {
event.currentTarget.parentNode.classList.add('focus');
}

onBlur(event) {
event.currentTarget.parentNode.classList.remove('focus');
}
}

// Initialize switches
window.addEventListener('load', function () {
// Initialize the Switch component on all matching DOM nodes
Array.from(
document.querySelectorAll('input[type=checkbox][role^=switch]')
).forEach((element) => new CheckboxSwitch(element));
});
2 changes: 1 addition & 1 deletion examples/switch/switch-button.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>Switch Example Using HTML Button</h1>
<p>Similar examples include: </p>
<ul>
<li><a href="switch.html">Switch Example</a>: A switch based on a <code>div</code> element that turns a notification preference on and off.</li>
<!-- <li><a href="switch-checkbox.html">Switch example using the <code>input[type=checkbox]</code> element.</a>.</li> -->
<li><a href="switch-checkbox.html">Switch Example Using HTML Checkbox Input</a>: A group of 2 switches based on HTML <code>input[type="checkbox"]</code> elements that turn accessibility preferences on and off.</li>
</ul>

<section>
Expand Down
208 changes: 208 additions & 0 deletions examples/switch/switch-checkbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Switch Example Using HTML Checkbox Input | WAI-ARIA Authoring Practices 1.2</title>

<!-- Core js and css shared by all examples; do not modify when using this template. -->
<link rel="stylesheet" href="https://www.w3.org/StyleSheets/TR/2016/base.css">
<link rel="stylesheet" href="../css/core.css">
<script src="../js/examples.js"></script>
<script src="../js/highlight.pack.js"></script>
<script src="../js/app.js"></script>

<!-- js and css for this example. -->
<link href="css/switch-checkbox.css" rel="stylesheet">
<script src="js/switch-checkbox.js"></script>
</head>
<body>
<nav aria-label="Related Links" class="feedback">
<ul>
<li><a href="../../#browser_and_AT_support">Browser and Assistive Technology Support</a></li>
<li><a href="https://github.com/w3c/aria-practices/issues/new">Report Issue</a></li>
<li><a href="https://github.com/w3c/aria-practices/projects/2">Related Issues</a></li>
<li><a href="../../#switch">Design Pattern</a></li>
</ul>
</nav>
<main>
<h1>Switch Example Using HTML Checkbox Input</h1>
<p>
This example illustrates implementing the <a href="../../#switch">Switch design pattern</a> with an HTML <code>input[type="checkbox"]</code> as the switch element and using CSS borders to provide graphical rendering of switch states.
It also demonstrates using the HTML <code>fieldset</code> and <code>legend</code> elements to present multiple switches in a labeled group.
</p>
<p>Similar examples include: </p>
<ul>
<li><a href="switch.html">Switch Example</a>: A switch based on a <code>div</code> element that turns a notification preference on and off.</li>
<li><a href="switch-button.html">Switch Example Using HTML Button</a>: A group of 2 switches based on HTML <code>button</code> elements that turn lights on and off.</li>
</ul>

<section>
<div class="example-header">
<h2 id="ex_label">Example</h2>
</div>
<div role="separator" id="ex_start_sep" aria-labelledby="ex_start_sep ex_label" aria-label="Start of"></div>
<div id="ex1">
<fieldset>
<legend>Accessibility Preferences</legend>
<label>
<span class="label">Reduced motion</span>
<input id="id-switch-1"
type="checkbox"
role="switch">
<span class="state">
<span class="container">
<span class="position">
</span>
</span>
<span class="on" aria-hidden="true">On</span>
<span class="off" aria-hidden="true">Off</span>
</span>
</label>

<label>
<span class="label">Show captions</span>
<input id="id-switch-2"
type="checkbox"
role="switch">
<span class="state">
<span class="container">
<span class="position">
</span>
</span>
<span class="on" aria-hidden="true">On</span>
<span class="off" aria-hidden="true">Off</span>
</span>
</label>
</fieldset>
</div>
<div role="separator" id="ex_end_sep" aria-labelledby="ex_end_sep ex_label" aria-label="End of"></div>
</section>

<section>
<h2>Accessibility Features</h2>
<ul>
<li>
To help assistive technology users understand that each switch belongs to a set of switches related to <q>Accessibility Preferences</q>, the switches are wrapped in a <code>fieldset</code> element labeled with a <code>legend</code> element.
</li>
<li>
To make understanding the state of the switch easier for users with visual or cognitive disabilities, a text equivalent of the state (<q>on</q> or <q>off</q>) is displayed adjacent to the graphical state indicator.
CSS attribute selectors ensure the label displayed is synchronized with the value of the <code>input</code>.<br>
<strong>NOTE:</strong> To prevent redundant announcement of the state by screen readers, the text indicators of state are hidden from assistive technologies with <code>aria-hidden</code>.
</li>
<li>Spacing, border widths and fill are important to ensure the graphical states are visible and discernible to people with visual impairments, including when browser or operating system high contrast settings are enabled:
<ul>
<li>To make the graphical representation of the state of a switch readily perceivable, two pixel borders are used for the switch state container and a solid color is used for the fill of the circles indicating the on and off states.</li>
<li>To ensure users can perceive the difference between the container and the circles used to indicate the state of the switch, there are two pixels of space between the container border and the circles.</li>
</ul>
</li>
<li>To enhance perceivability when operating the switches, visual keyboard focus and hover are styled using the CSS <code>:hover</code> pseudo-class and <code>onfocus</code> and <code>onblur</code> event handlers:
<ul>
<li>To make it easier to perceive focus and the relationship between a label and its associated switch, focus creates a border around both the switch and the label and also changes the background color.</li>
<li>To make it easier to perceive that clicking either the label or switch will activate the switch, the hover indicator is the same as the focus indicator.</li>
<li>To help people with visual impairments identify the switch as an interactive element, the cursor is changed to a pointer when hovering over the switch.</li>
<li>
Note: Because transparent borders are visible on some systems with operating system high contrast settings enabled, transparency cannot be used to create a visual difference between the element that is focused an other elements.
Instead of using transparency, the focused element has a thicker border and less padding.
When an element receives focus, its border changes from zero to two pixels and padding is reduced by two pixels.
When an element loses focus, its border changes from two pixels to two and padding is increased by two pixels.
</li>
</ul>
</li>
</ul>
</section>

<section>
<h2 id="kbd_label">Keyboard Support</h2>
<table aria-labelledby="kbd_label" class="def">
<thead>
<tr>
<th>Key</th>
<th>Function</th>
</tr>
</thead>
<tbody>
<tr data-test-id="key-tab">
<th><kbd>Tab</kbd></th>
<td>Moves keyboard focus to the <code>switch</code>.</td>
</tr>
<tr data-test-id="key-space">
<th><kbd>Space</kbd></th>
<td>Toggles the state of the switch between on and off.</td>
</tr>
</tbody>
</table>
</section>

<section>
<h2 id="rps_label">Role, Property, State, and Tabindex Attributes</h2>
<table aria-labelledby="rps_label" class="data attributes">
<thead>
<tr>
<th scope="col">Role</th>
<th scope="col">Attribute</th>
<th scope="col">Element</th>
<th scope="col">Usage</th>
</tr>
</thead>
<tbody>
<tr data-test-id="switch-role">
<th scope="row"><code>switch</code></th>
<td></td>
<td><code>input[type="checkbox"]</code></td>
<td>
<ul>
<li>Identifies the element as an ARIA <code>switch</code>.</li>
<li>The accessible name is defined using the <code>label</code> element.</li>
</ul>
</td>
</tr>
<tr data-test-id="aria-hidden">
<td></td>
<th scope="row"><code>aria-hidden="true"</code></th>
<td><code>span.on</code> and <code>span.off</code></td>
<td>
<ul>
<li>Removes the strings <q>on</q> and <q>off</q> that appear to the right of the switch from the accessible name of the switch.</li>
<li>These strings are included only for enhancing visual comprehension of the state; element states are not permitted in accessible names.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</section>

<section>
<h2>Javascript and CSS Source Code</h2>
<ul id="css_js_files">
<li>
CSS:
<a href="css/switch-checkbox.css" type="tex/css">switch-checkbox.css</a>
</li>
<li>
Javascript:
<a href="js/switch-checkbox.js" type="text/javascript">switch-checkbox.js</a>
</li>
</ul>
</section>

<section>
<h2 id="sc1_label">HTML Source Code</h2>
<div role="separator" id="sc1_start_sep" aria-labelledby="sc1_start_sep sc1_label" aria-label="Start of"></div>
<pre><code id="sc1"></code></pre>
<div role="separator" id="sc1_end_sep" aria-labelledby="sc1_end_sep sc1_label" aria-label="End of"></div>
<!--
The following script will show the reader the HTML source for the example that is in the div with ID 'ex1'.
It renders the HTML in the preceding pre element with ID 'sc1'.
If you change the ID of either the 'ex1' div or the 'sc1' pre, be sure to update the sourceCode.add function parameters.
-->
<script>
sourceCode.add('sc1', 'ex1', 'ex_label', 'css_js_files');
sourceCode.make();
</script>
</section>
</main>
<nav>
<a href="../../#switch">Switch Design Pattern in WAI-ARIA Authoring Practices 1.2</a>
</nav>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/switch/switch.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>Switch Example</h1>
<p>Similar examples include: </p>
<ul>
<li><a href="switch-button.html">Switch Example Using HTML Button</a>: A group of 2 switches based on HTML <code>button</code> elements that turn lights on and off.</li>
<!-- <li><a href="switch-checkbox.html">Switch example using the <code>input[type=checkbox]</code> element.</a>.</li> -->
<li><a href="switch-checkbox.html">Switch Example Using HTML Checkbox Input</a>: A group of 2 switches based on HTML <code>input[type="checkbox"]</code> elements that turn accessibility preferences on and off.</li>
</ul>

<section>
Expand Down
Loading