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 44 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 @@ -2557,6 +2557,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
21 changes: 14 additions & 7 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 All @@ -184,7 +185,7 @@ <h2 id="examples_by_role_label">Examples by Role</h2>
<li><a href="combobox/combobox-autocomplete-list.html">Editable Combobox With List Autocomplete</a></li>
<li><a href="combobox/combobox-autocomplete-none.html">Editable Combobox without Autocomplete</a></li>
<li><a href="combobox/combobox-select-only.html">Select-Only Combobox</a></li>
<li><a href="listbox/listbox-collapsible.html">Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-collapsible.html">(Deprecated) Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-grouped.html">Listbox with Grouped Options</a></li>
<li><a href="listbox/listbox-rearrangeable.html">Listboxes with Rearrangeable Options</a></li>
<li><a href="listbox/listbox-scrollable.html">Scrollable Listbox</a></li>
Expand Down Expand Up @@ -276,7 +277,7 @@ <h2 id="examples_by_role_label">Examples by Role</h2>
<li><a href="combobox/combobox-autocomplete-list.html">Editable Combobox With List Autocomplete</a></li>
<li><a href="combobox/combobox-autocomplete-none.html">Editable Combobox without Autocomplete</a></li>
<li><a href="combobox/combobox-select-only.html">Select-Only Combobox</a></li>
<li><a href="listbox/listbox-collapsible.html">Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-collapsible.html">(Deprecated) Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-grouped.html">Listbox with Grouped Options</a></li>
<li><a href="listbox/listbox-rearrangeable.html">Listboxes with Rearrangeable Options</a></li>
<li><a href="listbox/listbox-scrollable.html">Scrollable Listbox</a></li>
Expand Down Expand Up @@ -359,6 +360,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 @@ -443,7 +448,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<li><a href="combobox/combobox-autocomplete-none.html">Editable Combobox without Autocomplete</a></li>
<li><a href="combobox/combobox-select-only.html">Select-Only Combobox</a></li>
<li><a href="combobox/grid-combo.html">Editable Combobox with Grid Popup</a></li>
<li><a href="listbox/listbox-collapsible.html">Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-collapsible.html">(Deprecated) Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-grouped.html">Listbox with Grouped Options</a></li>
<li><a href="listbox/listbox-rearrangeable.html">Listboxes with Rearrangeable Options</a></li>
<li><a href="listbox/listbox-scrollable.html">Scrollable Listbox</a></li>
Expand Down Expand Up @@ -481,6 +486,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 Expand Up @@ -569,7 +575,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<li><a href="disclosure/disclosure-img-long-description.html">Disclosure (Show/Hide) for Image Description</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="disclosure/disclosure-navigation-hybrid.html">Disclosure Navigation Menu with Top-Level Links</a></li>
<li><a href="disclosure/disclosure-navigation.html">Disclosure Navigation Menu</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="listbox/listbox-collapsible.html">Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-collapsible.html">(Deprecated) Collapsible Dropdown Listbox</a></li>
<li><a href="menu-button/menu-button-actions-active-descendant.html">Actions Menu Button Using aria-activedescendant</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="menu-button/menu-button-actions.html">Actions Menu Button Using element.focus()</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="menu-button/menu-button-links.html">Navigation Menu Button</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
Expand All @@ -589,7 +595,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<ul>
<li><a href="combobox/combobox-datepicker.html">Date Picker Combobox</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="combobox/grid-combo.html">Editable Combobox with Grid Popup</a></li>
<li><a href="listbox/listbox-collapsible.html">Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-collapsible.html">(Deprecated) Collapsible Dropdown Listbox</a></li>
<li><a href="menu-button/menu-button-actions-active-descendant.html">Actions Menu Button Using aria-activedescendant</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="menu-button/menu-button-actions.html">Actions Menu Button Using element.focus()</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="menu-button/menu-button-links.html">Navigation Menu Button</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
Expand All @@ -609,6 +615,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<li><a href="slider/slider-seek.html">Media Seek Slider</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<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.html">Switch</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="toolbar/toolbar.html">Toolbar</a></li>
</ul>
</td>
Expand Down Expand Up @@ -651,7 +658,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<li><a href="feed/feed.html">Feed</a></li>
<li><a href="grid/dataGrids.html">Data Grid</a></li>
<li><a href="grid/LayoutGrids.html">Layout Grid</a></li>
<li><a href="listbox/listbox-collapsible.html">Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-collapsible.html">(Deprecated) Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-grouped.html">Listbox with Grouped Options</a></li>
<li><a href="listbox/listbox-rearrangeable.html">Listboxes with Rearrangeable Options</a></li>
<li><a href="listbox/listbox-scrollable.html">Scrollable Listbox</a></li>
Expand Down Expand Up @@ -784,7 +791,7 @@ <h2 id="examples_by_props_label">Examples By Properties and States</h2>
<li><a href="combobox/combobox-select-only.html">Select-Only Combobox</a></li>
<li><a href="combobox/grid-combo.html">Editable Combobox with Grid Popup</a></li>
<li><a href="dialog-modal/datepicker-dialog.html">Date Picker Dialog</a> (<abbr title="High Contrast Support">HC</abbr>)</li>
<li><a href="listbox/listbox-collapsible.html">Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-collapsible.html">(Deprecated) Collapsible Dropdown Listbox</a></li>
<li><a href="listbox/listbox-grouped.html">Listbox with Grouped Options</a></li>
<li><a href="listbox/listbox-rearrangeable.html">Listboxes with Rearrangeable Options</a></li>
<li><a href="listbox/listbox-scrollable.html">Scrollable Listbox</a></li>
Expand Down
73 changes: 73 additions & 0 deletions examples/switch/css/switch.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[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;
cursor: pointer;
}

[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