68 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-07-06 18:56:28 -04:00
import React from 'react'
2018-07-24 09:18:36 -04:00
import styled, { css } from 'react-emotion';
2018-07-06 18:56:28 -04:00
import ReactToggled from 'react-toggled';
import { colors, colorsRaw, shadows, transitions } from './styles';
2018-07-24 09:18:36 -04:00
const ToggleContainer = styled.span`
display: inline-flex;
align-items: center;
justify-content: center;
position: relative;
width: 40px;
height: 20px;
cursor: pointer;
`
const ToggleHandle = styled.span`
${shadows.dropDeep};
position: absolute;
left: 0;
top: 0;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: ${colorsRaw.white};
transition: transform ${transitions.main};
${props => props.isActive && css`
2018-07-06 18:56:28 -04:00
transform: translateX(20px);
2018-07-24 09:18:36 -04:00
`}
`
const ToggleBackground = styled.span`
width: 34px;
height: 14px;
border-radius: 10px;
background-color: ${colors.active};
`
2018-07-06 18:56:28 -04:00
const Toggle = ({
active,
onChange,
2018-07-24 09:18:36 -04:00
renderBackground,
2018-07-06 18:56:28 -04:00
onFocus,
2018-07-24 09:18:36 -04:00
onBlur,
Container = ToggleContainer,
Background = ToggleBackground,
Handle = ToggleHandle,
}) => (
2018-07-06 18:56:28 -04:00
<ReactToggled on={active} onToggle={onChange}>
{({on, getElementTogglerProps}) => (
2018-07-24 09:18:36 -04:00
<Container
2018-07-06 18:56:28 -04:00
role="switch"
aria-checked={on.toString()}
onFocus={onFocus}
onBlur={onBlur}
{...getElementTogglerProps()}
>
2018-07-24 09:18:36 -04:00
<Background isActive={on}/>
<Handle isActive={on}/>
</Container>
2018-07-06 18:56:28 -04:00
)}
2018-07-24 09:18:36 -04:00
</ReactToggled>
);
const StyledToggle = styled(Toggle)``;
2018-07-06 18:56:28 -04:00
2018-07-24 09:18:36 -04:00
export { StyledToggle as default, ToggleContainer, ToggleBackground, ToggleHandle };