-
Notifications
You must be signed in to change notification settings - Fork 333
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 button Element #1893
Merged
Merged
Changes from 15 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
e8278c9
initial switch example using button
jongund fe5d0a1
fixed button type issue
jongund 49e56c1
fixed spelling issues
jongund c944f36
updated documenation and hover styling
jongund eac87ca
fixed spelling error and update cspell
jongund 270e588
fixed spelling issue
jongund 785033f
updated focus and hover styling for container rect
jongund cc5a247
updated documentation
jongund 2bfbe7e
updated documentation
jongund 5cdf2f8
updated width for label for Safari
jongund 576ec83
added screen shot for list of landmarks for Orca
jongund c5c0b37
removed uwanted changes
jongund c96434f
updated examples for on/off labels to be in content and not CSS
jongund d7827ac
updated code to include DIV with class example-header for code pen sp…
jongund c59ded0
fixed code pen issue and added pointer cursor on hover
jongund af21f03
added place holder for focus styling
jongund 0fc895d
added place holder for focus styling
jongund 4372458
updated reference table
jongund 3c99b94
updated switch button example
jongund 86c9c32
updated accessibility documentation
jongund b16f2b3
updated documentation
jongund a1a977b
updated documentation
jongund 6356087
Merge branch 'main' into switch-button
jongund b794a71
updated documentation
jongund 1650613
initial switch example using button
jongund 117cabc
fixed button type issue
jongund a393f12
fixed spelling issues
jongund 7858793
updated documenation and hover styling
jongund bb5dab8
fixed spelling error and update cspell
jongund be1e660
fixed spelling issue
jongund 4c9f477
updated focus and hover styling for container rect
jongund 4af880b
updated documentation
jongund c3d2795
updated documentation
jongund 0fafcb1
updated width for label for Safari
jongund fab7ea5
added screen shot for list of landmarks for Orca
jongund 4d01157
removed uwanted changes
jongund e1c238c
updated examples for on/off labels to be in content and not CSS
jongund 28ea6e1
updated code to include DIV with class example-header for code pen sp…
jongund ecb288b
fixed code pen issue and added pointer cursor on hover
jongund 260e024
added place holder for focus styling
jongund 4b7b65d
added place holder for focus styling
jongund 91de552
updated switch button example
jongund 40650b4
updated accessibility documentation
jongund 9d02e9e
updated documentation
jongund 1cb1614
updated documentation
jongund dfe145e
updated documentation
jongund 0fdb708
Revise title
mcking65 b975a36
Minor editorial revisions to intro for consistency
mcking65 4790872
Remove excess section container
mcking65 d18cc06
Editorial revisions to roles, states, and props documentation for cla…
mcking65 dc88351
White space consistency
mcking65 13d6d2e
Merge and resolve conflicts with main
mcking65 b70f65d
updated index
jongund 97ebd33
updated accessibility features documentation
jongund 14edf15
fixed linting bug
jongund 3edf83d
fixed regression test error messages
jongund 1679639
Merge remote-tracking branch 'origin/main' into switch-button
mcking65 ecc4adc
Add links from main doc and other example
mcking65 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
button[role="switch"] { | ||
display: block; | ||
margin: 2px; | ||
padding: 4px 4px 8px 8px; | ||
border: 0 solid #005a9c; | ||
border-radius: 5px; | ||
width: 17em; | ||
text-align: left; | ||
background-color: white; | ||
} | ||
|
||
button[role="switch"] .label { | ||
position: relative; | ||
top: -3px; | ||
display: inline-block; | ||
padding: 0; | ||
margin: 0; | ||
width: 10em; | ||
vertical-align: middle; | ||
} | ||
|
||
button[role="switch"] svg { | ||
forced-color-adjust: auto; | ||
position: relative; | ||
top: 4px; | ||
} | ||
|
||
button[role="switch"] svg rect { | ||
fill-opacity: 0; | ||
stroke-width: 2; | ||
stroke: currentColor; | ||
} | ||
|
||
button[role="switch"] svg rect.container { | ||
forced-color-adjust: none; | ||
fill-opacity: 1; | ||
} | ||
|
||
button[role="switch"] svg rect.off { | ||
display: block; | ||
stroke: currentColor; | ||
fill: currentColor; | ||
fill-opacity: 1; | ||
} | ||
|
||
button[role="switch"][aria-checked="true"] svg rect.off { | ||
display: none; | ||
} | ||
|
||
button[role="switch"] svg rect.on { | ||
display: none; | ||
} | ||
|
||
button[role="switch"][aria-checked="true"] svg rect.on { | ||
color: green; | ||
display: block; | ||
stroke-color: currentColor; | ||
fill: currentColor; | ||
fill-opacity: 1; | ||
} | ||
|
||
button[role="switch"] span.off { | ||
display: inline; | ||
} | ||
|
||
button[role="switch"] span.on { | ||
display: none; | ||
} | ||
|
||
button[role="switch"][aria-checked="true"] span.off { | ||
display: none; | ||
} | ||
|
||
button[role="switch"][aria-checked="true"] span.on { | ||
display: inline; | ||
} | ||
|
||
button[role="switch"]:focus, | ||
button[role="switch"]:hover { | ||
padding: 2px 2px 6px 6px; | ||
border-width: 2px; | ||
outline: none; | ||
background-color: #def; | ||
cursor: pointer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 ButtonSwitch { | ||
constructor(domNode) { | ||
this.switchNode = domNode; | ||
this.switchNode.addEventListener('click', () => this.toggleStatus()); | ||
|
||
// Set background color for the SVG container Rect | ||
var color = getComputedStyle(this.switchNode).getPropertyValue( | ||
'background-color' | ||
); | ||
var containerNode = this.switchNode.querySelector('rect.container'); | ||
containerNode.setAttribute('fill', color); | ||
} | ||
|
||
// 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('button[role^=switch]')).forEach( | ||
(element) => new ButtonSwitch(element) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Switch using Button Example | 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-button.css" rel="stylesheet"> | ||
<script src="js/switch-button.js" type="text/javascript"></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 using Button Example</h1> | ||
<p> | ||
This example implements the <a href="../../#switch">Switch design pattern</a> using <code>button</code> elements and SVG to implement the graphical rendering of the state of the switch. Switches are similar to checkboxes, but should be rendered by assistive technologies as "on" and "off", instead of "checked" and "unchecked". | ||
</p> | ||
<p>Similar examples include: </p> | ||
<ul> | ||
<li><a href="switch.html">Switch example using the <code>div</code> element.</a></li> | ||
<li><a href="switch-checkbox.html">Switch example using the <code>input[type=checkbox]</code> element.</a>.</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"> | ||
<div role="group" aria-labelledby="id-group-label"> | ||
<h3 id="id-group-label" >Environmental Controls</h3> | ||
<button | ||
type="button" | ||
role="switch" | ||
aria-checked="false"> | ||
<span class="label">Living Room Lights</span> | ||
<svg xmlns='http://www.w3.org/2000/svg' height='20' width='36'> | ||
<rect class="container" x="1" y="1" width="34" height="18" rx="4" /> | ||
<rect class="off" x="4" y="4" width="12" height="12" rx="4" /> | ||
<rect class="on" x="20" y="4" width="12" height="12" rx="4" /> | ||
</svg> | ||
<span class="on">On</span> | ||
<span class="off">Off</span> | ||
</button> | ||
|
||
<button | ||
type="button" | ||
role="switch" | ||
aria-checked="false"> | ||
<span class="label">Outdoor Lights</span> | ||
<svg xmlns='http://www.w3.org/2000/svg' height='20' width='36'> | ||
<rect class="container" x="1" y="1" width="34" height="18" rx="4" /> | ||
<rect class="off" x="4" y="4" width="12" height="12" rx="4" /> | ||
<rect class="on" x="20" y="4" width="12" height="12" rx="4" /> | ||
</svg> | ||
<span class="on">On</span> | ||
<span class="off">Off</span> | ||
</button> | ||
</div> | ||
</div> | ||
<div role="separator" id="ex_end_sep" aria-labelledby="ex_end_sep ex_label" aria-label="End of"></div> | ||
</section> | ||
|
||
<section> | ||
<section> | ||
<h2>Accessibility Features</h2> | ||
<ul> | ||
<li>To help assistive technology users understand that the environmental control options are made of a group of two switches, the switches are wrapped in a <code>group</code> labeled by the heading that labels the notification options.</li> | ||
<li>To provide a visual indication the labels can also be used to change the state of a switch, the background color is changed and a border appears when the pointer moves over the switch or the label. Including the border insures people with visual impairments can perceive the target area in high contrast modes.</li> | ||
<li>A visual text label (i.e. "on" and "off") of the state of the switch is placed after the graphic indication of the state to make it easier for users with visual or cognitive impairments to understand the current state of the switch.</li> | ||
<li> | ||
The graphical rendering of the switch state uses 2 pixel borders for the container, and a 2 pixel border on the rectangle indicating the on and off position of the switch. There is 2 pixels of space between the container border and the border of the rectangle. The use of spacing and borders insures the graphic will be visible and discernible in high contrast modes used by people with visual impairments. | ||
</li> | ||
<li> | ||
To ensure the SVG graphics have sufficient contrast with the background when high contrast settings invert colors, the CSS <code>currentColor</code> value for the <code>stroke</code> and <code>fill</code> properties is used to synchronize the colors with text content. | ||
If specific colors were used to specify the <code>stroke</code> and <code>fill</code> properties, the color of these elements would remain the same in high contrast mode, which could lead to insufficient contrast between them and their background or even make them invisible if their color were to match the high contrast mode background. | ||
<br/>NOTE: The SVG elements need to set the CSS <code>forced-color-adjust</code> property to <code>auto</code> for some browsers to support the <code>currentColor</code> value. The fill color of the container <code>rect</code> is set on initialization to use the initial background color of the <code>button</code> element for the fill color to keep the background color constant when the button is focused or hovered. | ||
</li> | ||
</ul> | ||
</section> | ||
</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> | ||
<ul> | ||
<li>Moves keyboard focus to the <code>switch</code>.</li> | ||
</ul> | ||
</td> | ||
</tr> | ||
<tr data-test-id="key-space"> | ||
<th><kbd>Space</kbd>, <kbd>Enter</kbd></th> | ||
<td> | ||
<ul> | ||
<li>Toggle switch between on and off.</li> | ||
</ul> | ||
</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="h3"> | ||
<th scope="row"></th> | ||
<td></td> | ||
<td><code>h3</code></td> | ||
<td> | ||
<ul> | ||
<li>Provides a grouping label for the group of switches.</li> | ||
</ul> | ||
</td> | ||
</tr> | ||
<tr data-test-id="group-role"> | ||
<th scope="row"><code>group</code></th> | ||
<td></td> | ||
<td><code>div</code></td> | ||
<td> | ||
<ul> | ||
<li>Identifies the <code>div</code> element as a <code>group</code> container for the switches.</li> | ||
</ul> | ||
</td> | ||
</tr> | ||
<tr data-test-id="group-aria-labelledby"> | ||
<th scope="row"></th> | ||
<td><code>aria-labelledby</code></td> | ||
<td><code>div</code></td> | ||
<td> | ||
<ul> | ||
<li>The <code>aria-labelledby</code> attribute references the <code>id</code> attribute of the <code>h3</code> element to define the accessible name for the group of switches.</li> | ||
</ul> | ||
</td> | ||
</tr> | ||
<tr data-test-id="switch-role"> | ||
<th scope="row"><code>switch</code></th> | ||
<td></td> | ||
<td><code>button</code></td> | ||
<td> | ||
The <code>role="switch"</code> attribute identified the <code>button</code> element as an ARIA <code>switch</code>. | ||
</td> | ||
</tr> | ||
<tr data-test-id="switch-aria-checked"> | ||
<td></td> | ||
<th scope="row"><code>aria-checked="false"</code></th> | ||
<td><code>button</code></td> | ||
<td> | ||
<ul> | ||
<li>Identifies the <code>switch</code> button as off.</li> | ||
<li>CSS attribute selectors (e.g. <code>[aria-checked="false"]</code>) are used to synchronize the visual states with the value of the <code>aria-checked</code> attribute.</li> | ||
</ul> | ||
</td> | ||
</tr> | ||
<tr data-test-id="switch-aria-checked"> | ||
<td></td> | ||
<th scope="row"><code>aria-checked="true"</code></th> | ||
<td><code>button</code></td> | ||
<td> | ||
<ul> | ||
<li>Identifies the <code>switch</code> as on.</li> | ||
<li>CSS attribute selectors (e.g. <code>[aria-checked="true"]</code>) are used to synchronize the visual states with the value of the <code>aria-checked</code> attribute.</li> | ||
</ul> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</section> | ||
|
||
<section> | ||
<h2>Javascript and CSS Source Code</h2> | ||
<!-- After the js and css files are named with the name of this example, change the href and text of the following 2 links to refer to the appropriate js and css files. --> | ||
<ul id="css_js_files"> | ||
<li> | ||
CSS: | ||
<a href="css/switch-button.css" type="tex/css">switch-button.css</a> | ||
</li> | ||
<li> | ||
Javascript: | ||
<a href="js/switch-button.js" type="text/javascript">switch-button.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> | ||
<!-- Update the pattern_ID parameter of this link to refer to the APG design pattern section related to this example. --> | ||
<a href="../../#switch">Switch Design Pattern in WAI-ARIA Authoring Practices 1.2</a> | ||
</nav> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jesdaigle what are you referring to when you ask about "this"? Asking about the presence of the first cell in the row? The first ceel is for role, which is blank in this row b/c this is a row describing attributes of the row for the switch role.