Skip to content

Instantly share code, notes, and snippets.

@mminer
Created August 23, 2016 03:15
Show Gist options
  • Save mminer/0e05acbf47b89b8be2d250d03e69a053 to your computer and use it in GitHub Desktop.
Save mminer/0e05acbf47b89b8be2d250d03e69a053 to your computer and use it in GitHub Desktop.
An iOS-style toggle switch.
/*
<label className="switch">
<input type="checkbox" />
<div className="checkbox"></div>
</label>
*/
.switch {
$active-color: #27c940;
$switch-height: 24px;
$switch-padding: 2px;
$switch-width: $switch-height * 1.625;
$switch-radius: $switch-height;
$knob-size: $switch-height - ($switch-padding * 2);
$knob-radius: $switch-height - ($switch-padding * 2);
$switch-bg: white;
$transition-duration: 0.3s;
cursor: pointer;
display: inline-block;
.checkbox {
background-color: darken($switch-bg, 9%);
border-radius: $switch-radius;
border: none;
height: $switch-height;
position: relative;
transition: background-color $transition-duration;
width: $switch-width;
z-index: 0;
&::before {
background-color: $switch-bg;
border-radius: $switch-radius;
bottom: 0;
content: "";
height: $knob-radius;
left: 2px;
position: absolute;
right: 0;
top: 2px;
transform: scale(1);
transition: all $transition-duration;
width: $switch-width - ($switch-padding * 2);
z-index: 1;
}
&::after {
background-color: $switch-bg;
border-radius: $knob-radius;
bottom: 0;
box-shadow: 0 1px 2px transparentize(black, 0.6);
content: "";
height: $knob-size;
left: 2px;
position: absolute;
right: 0;
top: 2px;
transition: all $transition-duration;
width: $knob-size;
z-index: 2;
}
}
input[type="checkbox"] {
display: none;
&:checked + .checkbox {
background-color: $active-color;
&::before {
transform: scale(0);
}
&::after {
left: $switch-width - $knob-size - $switch-padding;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment