Skip to content

Commit

Permalink
Add switch example using HTML checkbox input (pull #1895)
Browse files Browse the repository at this point in the history
* Added switch example using input[type=checkbox]
* Updated accessibility documentation
  • Loading branch information
jongund committed Nov 10, 2021
1 parent b0c809b commit fcaaba2
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 6 deletions.
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
85 changes: 85 additions & 0 deletions examples/switch/css/switch-checkbox.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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;
user-select: none;
}

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

0 comments on commit fcaaba2

Please sign in to comment.