-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Switch.js
274 lines (267 loc) · 7.63 KB
/
Switch.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// @inheritedComponent IconButton
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { refType } from '@material-ui/utils';
import withStyles from '../styles/withStyles';
import { fade } from '../styles/colorManipulator';
import capitalize from '../utils/capitalize';
import SwitchBase from '../internal/SwitchBase';
export const styles = theme => ({
/* Styles applied to the root element. */
root: {
display: 'inline-flex',
width: 34 + 12 * 2,
height: 14 + 12 * 2,
overflow: 'hidden',
padding: 12,
boxSizing: 'border-box',
position: 'relative',
flexShrink: 0,
zIndex: 0, // Reset the stacking context.
verticalAlign: 'middle', // For correct alignment with the text.
},
/* Styles applied to the root element if `edge="start"`. */
edgeStart: {
marginLeft: -8,
},
/* Styles applied to the root element if `edge="end"`. */
edgeEnd: {
marginRight: -8,
},
/* Styles applied to the internal `SwitchBase` component's `root` class. */
switchBase: {
position: 'absolute',
top: 0,
left: 0,
zIndex: 1, // Render above the focus ripple.
color: theme.palette.type === 'light' ? theme.palette.grey[50] : theme.palette.grey[400],
transition: theme.transitions.create(['left', 'transform'], {
duration: theme.transitions.duration.shortest,
}),
'&$checked': {
transform: 'translateX(20px)',
},
'&$disabled': {
color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800],
},
'&$checked + $track': {
opacity: 0.5,
},
'&$disabled + $track': {
opacity: theme.palette.type === 'light' ? 0.12 : 0.1,
},
},
/* Styles applied to the internal SwitchBase component's root element if `color="primary"`. */
colorPrimary: {
'&$checked': {
color: theme.palette.primary.main,
'&:hover': {
backgroundColor: fade(theme.palette.primary.main, theme.palette.action.hoverOpacity),
'@media (hover: none)': {
backgroundColor: 'transparent',
},
},
},
'&$disabled': {
color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800],
},
'&$checked + $track': {
backgroundColor: theme.palette.primary.main,
},
'&$disabled + $track': {
backgroundColor:
theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
},
},
/* Styles applied to the internal SwitchBase component's root element if `color="secondary"`. */
colorSecondary: {
'&$checked': {
color: theme.palette.secondary.main,
'&:hover': {
backgroundColor: fade(theme.palette.secondary.main, theme.palette.action.hoverOpacity),
'@media (hover: none)': {
backgroundColor: 'transparent',
},
},
},
'&$disabled': {
color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800],
},
'&$checked + $track': {
backgroundColor: theme.palette.secondary.main,
},
'&$disabled + $track': {
backgroundColor:
theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
},
},
/* Styles applied to the root element if `size="small"`. */
sizeSmall: {
width: 40,
height: 24,
padding: 7,
'& $thumb': {
width: 16,
height: 16,
},
'& $switchBase': {
padding: 4,
'&$checked': {
transform: 'translateX(16px)',
},
},
},
/* Pseudo-class applied to the internal `SwitchBase` component's `checked` class. */
checked: {},
/* Pseudo-class applied to the internal SwitchBase component's disabled class. */
disabled: {},
/* Styles applied to the internal SwitchBase component's input element. */
input: {
left: '-100%',
width: '300%',
},
/* Styles used to create the thumb passed to the internal `SwitchBase` component `icon` prop. */
thumb: {
boxShadow: theme.shadows[1],
backgroundColor: 'currentColor',
width: 20,
height: 20,
borderRadius: '50%',
},
/* Styles applied to the track element. */
track: {
height: '100%',
width: '100%',
borderRadius: 14 / 2,
zIndex: -1,
transition: theme.transitions.create(['opacity', 'background-color'], {
duration: theme.transitions.duration.shortest,
}),
backgroundColor:
theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
opacity: theme.palette.type === 'light' ? 0.38 : 0.3,
},
});
const Switch = React.forwardRef(function Switch(props, ref) {
const {
classes,
className,
color = 'secondary',
edge = false,
size = 'medium',
...other
} = props;
const icon = <span className={classes.thumb} />;
return (
<span
className={clsx(
classes.root,
{
[classes.edgeStart]: edge === 'start',
[classes.edgeEnd]: edge === 'end',
[classes[`size${capitalize(size)}`]]: size !== 'medium',
},
className,
)}
>
<SwitchBase
type="checkbox"
icon={icon}
checkedIcon={icon}
classes={{
root: clsx(classes.switchBase, classes[`color${capitalize(color)}`]),
input: classes.input,
checked: classes.checked,
disabled: classes.disabled,
}}
ref={ref}
{...other}
/>
<span className={classes.track} />
</span>
);
});
Switch.propTypes = {
/**
* If `true`, the component is checked.
*/
checked: PropTypes.bool,
/**
* The icon to display when the component is checked.
*/
checkedIcon: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
*/
color: PropTypes.oneOf(['primary', 'secondary', 'default']),
/**
* @ignore
*/
defaultChecked: PropTypes.bool,
/**
* If `true`, the switch will be disabled.
*/
disabled: PropTypes.bool,
/**
* If `true`, the ripple effect will be disabled.
*/
disableRipple: PropTypes.bool,
/**
* If given, uses a negative margin to counteract the padding on one
* side (this is often helpful for aligning the left or right
* side of the icon with content above or below, without ruining the border
* size and shape).
*/
edge: PropTypes.oneOf(['start', 'end', false]),
/**
* The icon to display when the component is unchecked.
*/
icon: PropTypes.node,
/**
* The id of the `input` element.
*/
id: PropTypes.string,
/**
* [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
*/
inputProps: PropTypes.object,
/**
* Pass a ref to the `input` element.
*/
inputRef: refType,
/**
* Callback fired when the state is changed.
*
* @param {object} event The event source of the callback.
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
*/
onChange: PropTypes.func,
/**
* If `true`, the `input` element will be required.
*/
required: PropTypes.bool,
/**
* The size of the switch.
* `small` is equivalent to the dense switch styling.
*/
size: PropTypes.oneOf(['small', 'medium']),
/**
* The input component prop `type`.
*/
type: PropTypes.string,
/**
* The value of the component. The DOM API casts this to a string.
*/
value: PropTypes.any,
};
export default withStyles(styles, { name: 'MuiSwitch' })(Switch);