Skip to content

Instantly share code, notes, and snippets.

@brandonkoopa
Last active March 6, 2019 20:37
Show Gist options
  • Save brandonkoopa/534259a6518cc1b38e16453ac46f9f21 to your computer and use it in GitHub Desktop.
Save brandonkoopa/534259a6518cc1b38e16453ac46f9f21 to your computer and use it in GitHub Desktop.
Switch React Component
import styled from 'styled-components';
// Example usage:
/*
<StyledSwitch>
<input type="checkbox" name="isSwitched" checked={this.state.isSwitched} onChange={(e) => console.log('update isSwitched')} />
<div className="slider round"/>
</StyledSwitch>
*/
const Switch = styled.label`
position: relative;
display: inline-block;
width: 34px;
height: 14px;
background-color: rgba(34, 31, 31, 0.26);
border-radius: 7px;
margin-top: 4px;
input {
display: none;
&:checked + .slider {
background-color: #9ccefa;
}
&:focus + .slider {
box-shadow: 0 0 1px #9ccefa;
}
&:checked + .slider:before {
transform: translateX(26px);
background-color: #359cfa;
}
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
&:before {
position: absolute;
content: "";
left: -4px;
bottom: -2px;
width: 20px;
height: 20px;
background-color: #f1f1f1;
box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.24), 0 0 1px 0 rgba(0, 0, 0, 0.12);
border: 0.5px solid;
border-image-source: linear-gradient(to bottom, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0.06) 20%, rgba(255, 255, 255, 0));
border-image-slice: 1;
transition: .4s;
}
&.round {
border-radius: 34px;
}
&.round:before {
border-radius: 50%;
}
}
`;
export default Switch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment