Accessible toggle-style checkbox
by SriSri M
HTML
<h1 style="margin-top: 0;">Accessible toggle-style checkbox</h1>
<p>No javascript required! Customize the branding easily by changing the values in the mixins.</p>
<div class="can-toggle">
<input id="a" type="checkbox">
<label for="a">
<div class="can-toggle__switch" data-checked="On" data-unchecked="Off"></div>
</label>
</div>
SCSS
body { padding: 30px; font-family: Arial; }
@mixin can-toggle-branding(
$can-toggle-off-color: #777,
$can-toggle-on-color: #5fc054,
$can-toggle-inactive-text: rgba(white, 0.5),
$can-toggle-transition: cubic-bezier(0,1,0.5,1)
) {
input[type="checkbox"] {
&[disabled] ~ label {
color: rgba($can-toggle-off-color, 0.5);
}
&:focus ~ label, &:hover ~ label {
.can-toggle__switch {
background-color: $can-toggle-off-color;
&:after { color: darken($can-toggle-off-color, 10%); }
}
}
&:hover ~label { color: darken($can-toggle-off-color, 5%); }
&:checked {
~ label {
&:hover { color: darken($can-toggle-on-color, 3%); }
.can-toggle__switch {
background-color: lighten($can-toggle-on-color, 5%);
&:after { color: darken($can-toggle-on-color, 5%); }
}
}
&:focus, &:hover {
~ label {
.can-toggle__switch {
background-color: $can-toggle-on-color;
&:after { color: darken($can-toggle-on-color, 10%); }
}
}
}
}
}
label {
.can-toggle__label-text { flex: 1; }
.can-toggle__switch {
transition: background-color 0.3s $can-toggle-transition;
background: lighten($can-toggle-off-color, 5%);
&:before { color: $can-toggle-inactive-text; }
&:after {
// Autoprefixer choked here, so making the prefixes explicit
-webkit-transition: -webkit-transform 0.3s $can-toggle-transition;
transition: transform 0.3s $can-toggle-transition;
color: $can-toggle-off-color;
}
}
}
}
@mixin can-toggle-appearance
(
$can-toggle-width: 134px,
$can-toggle-height: 36px,
$can-toggle-border-radius: 4px,
$can-toggle-offset: 2px,
$can-toggle-label-font-size: 14px,
$can-toggle-switch-font-size: 12px,
$can-toggle-shadow: 0 3px 3px rgba(black, 0.4)
){
$can-toggle-switch-width:...