-
Notifications
You must be signed in to change notification settings - Fork 421
Add switch example using HTML checkbox input #1895
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
Merged
Merged
Changes from 1 commit
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 02e388c
fixed spell checker issue
jongund 91aeb0b
updated documentation
jongund c00b665
add exception for the HTML validator
jongund 7f943cc
checked out menu-button_links.js to use same fix in add-jump-to branch
jongund 77abf1d
restored original test file
jongund a32ebb7
updated examples for on/off labels to be in content and not CSS
jongund 27f2ebe
updated CSS for example to make it simplier
jongund bf5ded6
updated code to include DIV with class example-header for code pen sp…
jongund 2d403bc
updated .vnurc file
jongund 218de1f
fixed label not to include visual on/off text
jongund 8e5f3be
Merge branch 'main' into switch-checkbox
jongund e73bc13
fixed label bug using aria-hidden=true on on and off text
jongund a7b41e3
identifed validator bug
jongund e7d7788
documented validator bug
jongund 1d86f3f
fixed code pen issue
jongund 6b61219
removed Enter key support
jongund 48851af
Revise title
mcking65 2ca24b8
Editorial revision to intro
mcking65 813cbf1
further clarification of intro
mcking65 02da47b
Editorial changes to accessibility features for clarity and brevity
mcking65 063e4a7
white space
mcking65 4b3f267
Merge remote-tracking branch 'origin/main' into switch-checkbox
mcking65 60ebb9f
Further editorial clarification of accessibility features documentation
mcking65 0308ff2
Revise state and property descriptions
mcking65 93d8996
Remove unnecessary list markup from kb table
mcking65 4456f7e
updated spacing discription
jongund 399fbc1
updated accessibility documentation
jongund 50cf98b
fixed linting error
jongund 85041af
fixed linting error
jongund 41a801c
updated accessibility documentation
jongund dd2535a
updated accessibility documentation
jongund 4372458
updated reference table
jongund 80e26b0
udpated documentation
jongund a60b782
updated accessibility documentation
jongund 197e98a
Merge branch 'main' into switch-checkbox
jongund b05cace
Merge branch 'main' of github.com:w3c/aria-practices
jongund bf32c6f
Merge branch 'main' into switch-checkbox
jongund e0b872f
removed statement about synchronizing with the input.checked property…
jongund ef6a773
Editorial clarification of accessibility features
mcking65 1b3f2c7
comment out links to similar examples that are not yet merged
mcking65 4334d0e
minor editorial change to intro
mcking65 499acde
Merge remote-tracking branch 'origin/main' into switch-checkbox
mcking65 c6e44db
Add link to example from main document
mcking65 66b6a55
Resolve conflicts with main and merge latest from main
mcking65 1a009fc
Add links to examples from main doc and between example pages
mcking65 6044a71
Merge branch 'main' of github.com:w3c/aria-practices
jongund 47041ce
Merge branch 'main' of github.com:w3c/aria-practices
jongund 13351af
Merge branch 'main' of github.com:w3c/aria-practices
jongund 7211c39
Merge in latest from main and resolve conflicts
mcking65 b761845
Add links among example pages for similar examples
mcking65 88733e1
Merge branch 'main' of github.com:w3c/aria-practices
jongund ac426b1
Merge branch 'main' of github.com:w3c/aria-practices
jongund 9c8fae3
Merge remote-tracking branch 'origin/main' into switch-checkbox
mcking65 b613279
Merge branch 'main' of github.com:w3c/aria-practices
jongund 5d41df7
Merge branch 'main' into switch-checkbox
jongund fcc1cc4
Change /> to >
mcking65 2b088d8
Change /> to >
mcking65 f31b97c
Change /> to >
mcking65 2e7ee8b
Change /> to >
mcking65 de13911
Merge branch 'main' of github.com:w3c/aria-practices
jongund 562fe03
Merge branch 'main' into switch-checkbox
jongund 13face8
updated CSS properties to remove -moz-user-select and -ms-user-select
jongund 558fc72
Remove more -moz- and -ms- properties
zcorpan 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
Next
Next commit
adding initial switch example using input[type=checkbox]
- Loading branch information
commit 97f74a951d3a33e6f7fc638909eb0b331f7a5e4d
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,74 @@ | ||
| 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; | ||
| } | ||
|
|
||
| label input[role="switch"] { | ||
| opacity: 0; | ||
| } | ||
|
|
||
| label input[role="switch"] ~ .state { | ||
| display: inline-block; | ||
| } | ||
|
|
||
| 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"] ~ .state::after { | ||
| padding-left: 4px; | ||
| content: "Off"; | ||
| } | ||
|
|
||
| label input[role="switch"]:checked ~ .state > .container > .position { | ||
| left: 20px; | ||
| border-color: green; | ||
| background: green; | ||
| opacity: 1; | ||
| } | ||
|
|
||
| label input[role="switch"]:checked ~ .state::after { | ||
| padding-left: 4px; | ||
| content: "On"; | ||
| } | ||
|
|
||
| label.focus, | ||
| label:hover { | ||
| padding: 2px 2px 4px 4px; | ||
| border-width: 2px; | ||
| outline: none; | ||
| background-color: #def; | ||
| } | ||
|
|
||
| label.focus span.container, | ||
| label:hover span.container { | ||
| background-color: white; | ||
| } |
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * 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('keydown', () => this.onKeydown(event)); | ||
| this.switchNode.addEventListener('focus', () => this.onFocus(event)); | ||
| this.switchNode.addEventListener('blur', () => this.onBlur(event)); | ||
| } | ||
|
|
||
| onKeydown(event) { | ||
| if (event.key === 'Enter') { | ||
| event.currentTarget.checked = !event.currentTarget.checked; | ||
| event.stopPropagation(); | ||
| event.preventDefault(); | ||
| } | ||
| } | ||
|
|
||
| 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)); | ||
| }); |
This file contains hidden or 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,209 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>Switch using input[type="checkbox"] 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-checkbox.css" rel="stylesheet"> | ||
| <script src="js/switch-checkbox.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 <code>input[type="checkbox"]</code> Example</h1> | ||
| <p> | ||
| This example implements the <a href="../../#switch">Switch design pattern</a> using <code>input[type="checkbox"]</code> elements and CSS borders 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-button.html">Switch example using the <code>button</code> element.</a>.</li> | ||
| </ul> | ||
|
|
||
| <section> | ||
| <h2 id="ex_label">Example</h2> | ||
jongund marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <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" >Accessibility Preferences</h3> | ||
| <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> | ||
| </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> | ||
| </label> | ||
| </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 accessibility preferences 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.</li> | ||
| <li>A visual text label 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 circle indicating the on and off position of the switch. There is 2 pixels of space between the container border and the border of the circle. 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> | ||
| </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-enter"> | ||
| <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>input[type="checkbox"]</code></td> | ||
| <td> | ||
| <ul> | ||
| <li>The <code>role="switch"</code> attribute identified the <code>input[type="checkbox"]</code> element as an ARIA <code>switch</code>.</li> | ||
| <li>The accessible name is defined using the <code>label</code> element.</li> | ||
| </ul> | ||
| </td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
| </section> | ||
|
|
||
| <section> | ||
| <h2>Javascript and CSS Source Code</h2> | ||
| <ul> | ||
| <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'); | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.