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

Switch Role: Example using the DIV element #1892

Merged
merged 55 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
3b972ac
Add basis code
Jul 17, 2017
198a699
Fix switch functionality
Jul 17, 2017
0ba2018
Add keyboard support
Jul 20, 2017
01babec
Add line about where to comment
Jul 20, 2017
24f2fc7
Remove enter key as an activation key
Jul 20, 2017
0aedd74
Merge branch 'master' into switch
mcking65 Jul 19, 2018
c6d2ddb
Add draft of switch design pattern (pull #788)
jongund Jul 19, 2018
6194469
Switch Pattern: add note with link to development issue
mcking65 Jul 19, 2018
fa3d3c3
Switch Example: Add header nav and make minor editorial revisions.
mcking65 Aug 13, 2018
a39fe5f
Merge branch 'master' into switch
mcking65 Jan 28, 2019
40d10e3
Merge branch 'master' into switch
mcking65 Feb 4, 2019
e37e753
Add basis code
Jul 17, 2017
55afcf7
Fix switch functionality
Jul 17, 2017
639bbda
Add keyboard support
Jul 20, 2017
32ae95a
Add line about where to comment
Jul 20, 2017
8ea5ed1
Remove enter key as an activation key
Jul 20, 2017
9c4da45
Add draft of switch design pattern (pull #788)
jongund Jul 19, 2018
09a4dc4
Switch Pattern: add note with link to development issue
mcking65 Jul 19, 2018
8a6c110
Switch Example: Add header nav and make minor editorial revisions.
mcking65 Aug 13, 2018
4830d6a
#243 Update Switch JS/CSS/HTML
Feb 4, 2019
ff76296
Merge branch 'switch' of github.com:w3c/aria-practices into switch
Feb 5, 2019
5fe0eff
Merge branch 'master' into switch
mcking65 Feb 27, 2019
146ae8a
Merge branch 'master' into switch
jongund Mar 2, 2021
da6f59f
Merge branch 'main' into switch
jongund May 4, 2021
278e3d2
initial implementation of a switch that supports HC
jongund May 5, 2021
e4a9fce
intial implementation of a switch example using a DIV element
jongund May 5, 2021
d7fa02f
fixed spelling issues
jongund May 5, 2021
5d90774
updated documentation and hover styling
jongund May 6, 2021
99058e9
update cspell
jongund May 6, 2021
b744849
update cspell
jongund May 6, 2021
dceedb0
update cspell
jongund May 6, 2021
252deff
fixed spelling error
jongund May 6, 2021
44f11fe
fixed spelling error and update cspell
jongund May 6, 2021
1b5dc1a
updated documentation
jongund May 7, 2021
d815a3e
updated documentation
jongund May 7, 2021
23ae014
updated documentation
jongund May 7, 2021
d76296a
fixed selection and moved on/off labels to content
jongund May 27, 2021
b59317d
updated code to include DIV with class example-header for code pen sp…
jongund Jul 15, 2021
9fba33f
fixed code pen issue and added pointer cursor on hover
jongund Jul 27, 2021
4372458
updated reference table
jongund Sep 21, 2021
2cac238
updated documentation for switch example and added aria-hidden
jongund Sep 21, 2021
9483e3a
fixed linting bug
jongund Sep 21, 2021
24655f1
updated documentation
jongund Sep 22, 2021
7ecbbd9
Merge branch 'main' into switch
jongund Sep 22, 2021
0a58b73
Resolve conflicts in aria-practices.html
mcking65 Oct 3, 2021
63f35d5
Merge remote-tracking branch 'origin/main' into switch
mcking65 Oct 3, 2021
0720a1d
Revise intro
mcking65 Oct 3, 2021
b243661
Remove extraneous section tags
mcking65 Oct 3, 2021
cce4836
Revise accessibility features documentation for clarity and consistency
mcking65 Oct 3, 2021
34197b6
Minor update to kbd doc
mcking65 Oct 3, 2021
0069358
Editorial clean up of kb documentation section
mcking65 Oct 3, 2021
a4fe905
white space consistency
mcking65 Oct 3, 2021
192f8a5
updated example to have just one switch and corrected the accessibili…
jongund Oct 4, 2021
f73aca5
Merge branch 'main' into switch
mcking65 Oct 4, 2021
2a5ad2f
Add link to example from main doc
mcking65 Oct 4, 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
42 changes: 42 additions & 0 deletions aria-practices.html
Original file line number Diff line number Diff line change
Expand Up @@ -2555,6 +2555,48 @@ <h4>WAI-ARIA Roles, States, and Properties</h4>
</section>
</section>

<section class="widget" id="switch">
<h3>Switch</h3>
<p>
This pattern is work in progress.
Please provide feedback in
<a href="https://github.com/w3c/aria-practices/issues/162">issue 162.</a>
</p>
<p>
A switch is similar to a checkbox, but instead of being checked or unchecked its states are <q>on</q> or <q>off</q>.
The switch provides assistive technologies the opportunity to render the state of the switch consistent with the graphical rendering (e.g., announcing "on" and "off").
Switches are often found in user preference and profile pages.
</p>

<section class="notoc">
<h4>Example</h4>
<p><a href="examples/switch/switch.html">Switch Example</a>: Demonstrates simple on/off switches.</p>
</section>

<section class="notoc">
<h4>Keyboard Interaction</h4>
<p>When the switch has focus, pressing the <kbd>Space</kbd> key changes the state of the switch.</p>
</section>

<section class="notoc">
<h4>WAI-ARIA Roles, States, and Properties</h4>
<ul>
<li>The switch has role <a href="#switch" class="role-reference">switch</a>.</li>
<li>
The switch has an accessible label provided by one of the following:
<ul>
<li>Visible text content contained within the element with role <code>switch</code>.</li>
<li>A visible label referenced by the value of <a href="#aria-labelledby" class="property-reference" >aria-labelledby</a> set on the element with role <code>switch</code>.</li>
<li><a href="#aria-label" class="property-reference">aria-label</a> set on the element with role <code>switch</code>.</li>
</ul>
</li>
<li>When <q>on</q>, the switch element has state <a href="#aria-checked" class="state-reference" >aria-checked</a> set to <code>true</code>.</li>
<li>When <q>off</q>, it has state <a href="#aria-checked" class="state-reference" >aria-checked</a> set to <code>false</code>.</li>
<li>If the state <a href="#aria-checked" class="state-reference" >aria-checked</a> is set to <code>mixed</code>, it will be treated <code>false</code>.</li>
</ul>
</section>
</section>

<section class="widget" id="table">
<h3>Table</h3>
<p>
Expand Down
6 changes: 6 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ <h2 id="examples_by_role_label">Examples by Role</h2>
<li><a href="menubar/menubar-editor.html">Editor Menubar</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="slider/slider-color-viewer.html">Color Viewer 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.html">Switch</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="treeview/treeview-1/treeview-1a.html">File Directory Treeview Using Computed Properties</a></li>
<li><a href="treeview/treeview-1/treeview-1b.html">File Directory Treeview Using Declared Properties</a></li>
<li><a href="treeview/treeview-navigation.html">Navigation Treeview</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
Expand Down Expand Up @@ -356,6 +357,10 @@ <h2 id="examples_by_role_label">Examples by Role</h2>
</ul>
</td>
</tr>
<tr>
<td><code>switch</code></td>
<td><a href="switch/switch.html">Switch</a></td>
</tr>
<tr>
<td><code>tab</code></td>
<td>
Expand Down Expand Up @@ -478,6 +483,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<li><a href="menubar/menubar-editor.html">Editor Menubar</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="radio/radio-activedescendant.html">Radio Group Using aria-activedescendant</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="radio/radio.html">Radio Group Using Roving tabindex</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="toolbar/toolbar.html">Toolbar</a></li>
</ul>
</td>
Expand Down
72 changes: 72 additions & 0 deletions examples/switch/css/switch.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[role="switch"] {
margin: 2px;
padding: 4px 4px 8px 8px;
border: 0 solid #005a9c;
border-radius: 5px;
width: 15em;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

[role="switch"] .label {
display: inline-block;
width: 8em;
}

[role="switch"] .switch {
position: relative;
display: inline-block;
top: 6px;
border: 2px solid black;
border-radius: 12px;
height: 20px;
width: 40px;
}

[role="switch"] .switch span {
position: absolute;
top: 2px;
left: 2px;
display: inline-block;
border: 2px solid black;
border-radius: 8px;
height: 12px;
width: 12px;
background: black;
}

[role="switch"][aria-checked="true"] .switch span {
left: 21px;
background: green;
border-color: green;
}

[role="switch"] .on {
display: none;
}

[role="switch"] .off {
display: inline;
}

[role="switch"][aria-checked="true"] .on {
display: inline;
}

[role="switch"][aria-checked="true"] .off {
display: none;
}

[role="switch"]:focus,
[role="switch"]:hover {
padding: 2px 2px 6px 6px;
border-width: 2px;
outline: none;
background-color: #def;
}

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

'use strict';

class Switch {
constructor(domNode) {
this.switchNode = domNode;
this.switchNode.addEventListener('click', () => this.toggleStatus());
this.switchNode.addEventListener('keydown', (event) =>
this.handleKeydown(event)
);
}

handleKeydown(event) {
// Only do something when space or return is pressed
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
this.toggleStatus();
}
}

// Switch state of a switch
toggleStatus() {
const currentState =
this.switchNode.getAttribute('aria-checked') === 'true';
const newState = String(!currentState);

this.switchNode.setAttribute('aria-checked', newState);
}
}

// Initialize switches
window.addEventListener('load', function () {
// Initialize the Switch component on all matching DOM nodes
Array.from(document.querySelectorAll('[role^=switch]')).forEach(
(element) => new Switch(element)
);
});
Loading