Created
August 9, 2018 09:44
-
-
Save gucheen/609ec145b43ef9b4462916e0438a850f to your computer and use it in GitHub Desktop.
Radio Toggle Switch
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
<fieldset class="radio-switch"> | |
<legend> | |
Settings | |
</legend> | |
<input type="radio" name="lol" id="public"> | |
<label for="public"> | |
On | |
</label> | |
<input type="radio" name="lol" id="private"> | |
<label for="private"> | |
Off | |
</label> | |
</fieldset> | |
<!-- | |
Note: | |
The problem with this implementation, specifically the use of pseudo elements to make the Switch UI, is that the UI itself is split down the middle in where a user can click to toggle the current selection. | |
Ideally, the text "on" or "off" could be clicked to update the selected option...but it'd be nicer if the full toggle UI could also be clicked to toggle the options back and forth. I'm sure this could still be done with this implemetnation, but as it stands right now, it doesn't fully allow that. | |
--> |
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
/** | |
Focus within pollyfill | |
Credit: https://gist.github.com/aFarkas/a7e0d85450f323d5e164 | |
*/ | |
(function(window, document){ | |
'use strict'; | |
var slice = [].slice; | |
var removeClass = function(elem){ | |
elem.classList.remove('focus-within'); | |
}; | |
var update = (function(){ | |
var running, last; | |
var action = function(){ | |
var element = document.activeElement; | |
running = false; | |
if(last !== element){ | |
last = element; | |
slice.call(document.getElementsByClassName('focus-within')).forEach(removeClass); | |
while(element && element.classList){ | |
element.classList.add('focus-within'); | |
element = element.parentNode; | |
} | |
} | |
}; | |
return function(){ | |
if(!running){ | |
requestAnimationFrame(action); | |
running = true; | |
} | |
}; | |
})(); | |
document.addEventListener('focus', update, true); | |
document.addEventListener('blur', update, true); | |
update(); | |
})(window, document); |
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
/* | |
get rid of the fieldset styling and keep | |
this all on a single line | |
*/ | |
.radio-switch { | |
border: none; | |
padding: 0; | |
white-space: nowrap; | |
} | |
/* | |
radio button groups often benefit from a legend to | |
provide context as to what the different | |
options pertain to. Ideally this would be visible to all | |
users, but you know... | |
*/ | |
.radio-switch legend { | |
font-size: 2px; | |
opacity: 0; | |
position: absolute; | |
} | |
/* | |
relative labels to help position the pseudo elements | |
the z-index will be handy later, when the labels that | |
overlap the visual switch UI need to be adjusted | |
to allow for a user to toggle the switch without | |
having to move their mouse/finger to the different | |
sides of the UI | |
*/ | |
.radio-switch label { | |
display: inline-block; | |
line-height: 2; | |
position: relative; | |
z-index: 2; | |
} | |
/* | |
inputs set to opcacity 0 are still accessible. | |
Apparently there can be issues targetting inputs with | |
Dragon speech recognition software if you use the typical | |
'visually-hidden' class...so might as well just avoid that issue... | |
*/ | |
.radio-switch input { | |
opacity: 0; | |
position: absolute; | |
} | |
/* | |
a 2 option toggle can only have 2 options...so instead of | |
adding more classes, i'm just going to use some | |
structural pseudo-classes to target them... | |
cause why let all that good work go to waste?! | |
the large padding is used to position the labels | |
on top of the visual UI, so the switch UI itself | |
can be mouse clicked or finger tapped to toggle | |
the current option | |
*/ | |
.radio-switch label:first-of-type { | |
padding-right: 5em; | |
} | |
.radio-switch label:last-child { | |
margin-left: -4.25em; | |
padding-left: 5em; | |
} | |
/* | |
oh focus within, I can't wait for you to have even more support. | |
But you'll never be in IE11, so we're going to need a | |
polyfill for you for a bit... | |
*/ | |
.radio-switch:focus-within label:first-of-type:after { | |
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #2196f3; | |
} | |
/* polyfill class*/ | |
.radio-switch.focus-within label:first-of-type:after { | |
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #2196f3; | |
} | |
/* making the switch UI. */ | |
.radio-switch label:first-of-type:before, | |
.radio-switch label:first-of-type:after { | |
border: 1px solid #aaa; | |
content: ""; | |
height: 2em; | |
overflow: hidden; | |
pointer-events: none; | |
position: absolute; | |
vertical-align: middle; | |
} | |
.radio-switch label:first-of-type:before { | |
background: #fff; | |
border: 1px solid #aaa; | |
border-radius: 100%; | |
position: absolute; | |
right: -.075em; | |
transform: translateX(0em); | |
transition: transform .2s ease-in-out; | |
width: 2em; | |
z-index: 2; | |
} | |
.radio-switch label:first-of-type:after { | |
background: #222; | |
border-radius: 1em; | |
margin: 0 1em; | |
transition: background .2s ease-in-out; | |
width: 4em; | |
} | |
/* | |
Visually change the switch UI to match the | |
checked state of the first radio button | |
*/ | |
.radio-switch input:first-of-type:checked ~ label:first-of-type:after { | |
background: #2196f3; | |
} | |
.radio-switch input:first-of-type:checked ~ label:first-of-type:before { | |
transform: translateX(-2em); | |
} | |
/* Move the 2nd label to have a lower z-index, so when that | |
option is toggled, the first label will overlay on top of the | |
Switch ui, and the switch can be pressed again to toggle back | |
to the prevoius state. */ | |
.radio-switch input:last-of-type:checked ~ label:last-of-type { | |
z-index: 1; | |
} | |
/* | |
system defaut fonts sure do load quick... | |
*/ | |
body { | |
padding: 3em; | |
font-family: arial; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment