forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggle.jsx
More file actions
78 lines (66 loc) · 1.76 KB
/
Toggle.jsx
File metadata and controls
78 lines (66 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { connect } from 'react-redux';
import { toggle } from '../actions/toggle';
import { getJS, getMode } from '../selectors';
import SVG from './Svg';
const StyledToggle = styled.button`
background-color: ${props => props.theme.toggleBackground};
border: 1px solid ${props => props.theme.toggleBorder};
border-radius: 50%;
cursor: pointer;
height: 2.5rem;
margin: 0.5rem 1.5rem 0 0;
outline: 0;
width: 2.5rem;
z-index: 10;
@media (min-width: 769px) {
margin: 0 0 0 1.5rem;
}
& svg,
& g {
fill: ${props => props.theme.toggleFill};
}
&:hover {
background-color: ${props => props.theme.toggleBackgroundHover};
& svg,
& g {
fill: ${props => props.theme.toggleFillHover};
}
}
&.active {
background-color: ${props => props.theme.toggleActiveBackground};
border-color: ${props => props.theme.toggleActiveBorder};
& svg,
& g {
fill: ${props => props.theme.toggleActiveFill};
}
}
`;
export const Toggle = props => {
const { onToggle, control, js, mode } = props;
let isActive;
if (control === 'js' && js === 'es6') isActive = 'active';
if (control === 'mode' && mode === 'light') isActive = 'active';
return (
<StyledToggle onClick={() => onToggle(control)} className={isActive} data-cy={control}>
<SVG control={control} />
</StyledToggle>
);
};
Toggle.propTypes = {
onToggle: PropTypes.func.isRequired,
control: PropTypes.string.isRequired,
mode: PropTypes.string.isRequired,
js: PropTypes.string.isRequired
};
export default connect(
state => ({
js: getJS(state),
mode: getMode(state)
}),
{
onToggle: control => toggle(control)
}
)(Toggle);