-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Switch Example using HTML button Element (pull #1893)
Co-authored-by: Matt King <a11yThinker@gmail.com>
- Loading branch information
Showing
7 changed files
with
570 additions
and
6 deletions.
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
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,80 @@ | ||
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.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) | ||
); | ||
}); |
Oops, something went wrong.