71 lines
1.5 KiB
JavaScript
Raw Normal View History

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;
`;
2018-07-24 09:18:36 -04:00
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`
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,
onFocus,
2018-07-24 09:18:36 -04:00
onBlur,
2018-07-24 21:46:04 -04:00
className,
2018-07-24 09:18:36 -04:00
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}
2018-07-24 21:46:04 -04:00
className={className}
2018-07-06 18:56:28 -04:00
{...getElementTogglerProps()}
>
<Background isActive={on} />
<Handle isActive={on} />
2018-07-24 09:18:36 -04:00
</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 };