92 lines
1.9 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
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';
const ToggleContainer = styled.button`
2018-07-24 09:18:36 -04:00
display: inline-flex;
align-items: center;
justify-content: center;
position: relative;
width: 40px;
height: 20px;
cursor: pointer;
border: none;
padding: 0;
margin: 0;
background: transparent;
`;
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 = ({
id,
2018-07-06 18:56:28 -04:00
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, getTogglerProps }) => (
2018-07-24 09:18:36 -04:00
<Container
id={id}
2018-07-06 18:56:28 -04:00
onFocus={onFocus}
onBlur={onBlur}
2018-07-24 21:46:04 -04:00
className={className}
{...getTogglerProps({
role: 'switch',
'aria-checked': on.toString(),
'aria-expanded': null,
})}
2018-07-06 18:56:28 -04:00
>
<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>
);
Toggle.propTypes = {
id: PropTypes.string,
active: PropTypes.bool,
onChange: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
className: PropTypes.string,
Container: PropTypes.func,
Background: PropTypes.func,
Handle: PropTypes.func,
};
2018-07-24 09:18:36 -04:00
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 };