UNPKG

9.56 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = exports.styles = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _react = _interopRequireDefault(require("react"));
15
16var _propTypes = _interopRequireDefault(require("prop-types"));
17
18var _clsx = _interopRequireDefault(require("clsx"));
19
20var _utils = require("@material-ui/utils");
21
22var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
23
24var _colorManipulator = require("../styles/colorManipulator");
25
26var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
27
28var _SwitchBase = _interopRequireDefault(require("../internal/SwitchBase"));
29
30// @inheritedComponent IconButton
31var styles = function styles(theme) {
32 return {
33 /* Styles applied to the root element. */
34 root: {
35 display: 'inline-flex',
36 width: 34 + 12 * 2,
37 height: 14 + 12 * 2,
38 overflow: 'hidden',
39 padding: 12,
40 boxSizing: 'border-box',
41 position: 'relative',
42 flexShrink: 0,
43 zIndex: 0,
44 // Reset the stacking context.
45 verticalAlign: 'middle' // For correct alignment with the text.
46
47 },
48
49 /* Styles applied to the root element if `edge="start"`. */
50 edgeStart: {
51 marginLeft: -8
52 },
53
54 /* Styles applied to the root element if `edge="end"`. */
55 edgeEnd: {
56 marginRight: -8
57 },
58
59 /* Styles applied to the internal `SwitchBase` component's `root` class. */
60 switchBase: {
61 position: 'absolute',
62 top: 0,
63 left: 0,
64 zIndex: 1,
65 // Render above the focus ripple.
66 color: theme.palette.type === 'light' ? theme.palette.grey[50] : theme.palette.grey[400],
67 transition: theme.transitions.create(['left', 'transform'], {
68 duration: theme.transitions.duration.shortest
69 }),
70 '&$checked': {
71 transform: 'translateX(20px)'
72 },
73 '&$disabled': {
74 color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800]
75 },
76 '&$checked + $track': {
77 opacity: 0.5
78 },
79 '&$disabled + $track': {
80 opacity: theme.palette.type === 'light' ? 0.12 : 0.1
81 }
82 },
83
84 /* Styles applied to the internal SwitchBase component's root element if `color="primary"`. */
85 colorPrimary: {
86 '&$checked': {
87 color: theme.palette.primary.main,
88 '&:hover': {
89 backgroundColor: (0, _colorManipulator.fade)(theme.palette.primary.main, theme.palette.action.hoverOpacity),
90 '@media (hover: none)': {
91 backgroundColor: 'transparent'
92 }
93 }
94 },
95 '&$disabled': {
96 color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800]
97 },
98 '&$checked + $track': {
99 backgroundColor: theme.palette.primary.main
100 },
101 '&$disabled + $track': {
102 backgroundColor: theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white
103 }
104 },
105
106 /* Styles applied to the internal SwitchBase component's root element if `color="secondary"`. */
107 colorSecondary: {
108 '&$checked': {
109 color: theme.palette.secondary.main,
110 '&:hover': {
111 backgroundColor: (0, _colorManipulator.fade)(theme.palette.secondary.main, theme.palette.action.hoverOpacity),
112 '@media (hover: none)': {
113 backgroundColor: 'transparent'
114 }
115 }
116 },
117 '&$disabled': {
118 color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800]
119 },
120 '&$checked + $track': {
121 backgroundColor: theme.palette.secondary.main
122 },
123 '&$disabled + $track': {
124 backgroundColor: theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white
125 }
126 },
127
128 /* Styles applied to the root element if `size="small"`. */
129 sizeSmall: {
130 width: 40,
131 height: 24,
132 padding: 7,
133 '& $thumb': {
134 width: 16,
135 height: 16
136 },
137 '& $switchBase': {
138 padding: 4,
139 '&$checked': {
140 transform: 'translateX(16px)'
141 }
142 }
143 },
144
145 /* Pseudo-class applied to the internal `SwitchBase` component's `checked` class. */
146 checked: {},
147
148 /* Pseudo-class applied to the internal SwitchBase component's disabled class. */
149 disabled: {},
150
151 /* Styles applied to the internal SwitchBase component's input element. */
152 input: {
153 left: '-100%',
154 width: '300%'
155 },
156
157 /* Styles used to create the thumb passed to the internal `SwitchBase` component `icon` prop. */
158 thumb: {
159 boxShadow: theme.shadows[1],
160 backgroundColor: 'currentColor',
161 width: 20,
162 height: 20,
163 borderRadius: '50%'
164 },
165
166 /* Styles applied to the track element. */
167 track: {
168 height: '100%',
169 width: '100%',
170 borderRadius: 14 / 2,
171 zIndex: -1,
172 transition: theme.transitions.create(['opacity', 'background-color'], {
173 duration: theme.transitions.duration.shortest
174 }),
175 backgroundColor: theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
176 opacity: theme.palette.type === 'light' ? 0.38 : 0.3
177 }
178 };
179};
180
181exports.styles = styles;
182
183var Switch = _react.default.forwardRef(function Switch(props, ref) {
184 var classes = props.classes,
185 className = props.className,
186 _props$color = props.color,
187 color = _props$color === void 0 ? 'secondary' : _props$color,
188 _props$disabled = props.disabled,
189 disabled = _props$disabled === void 0 ? false : _props$disabled,
190 _props$edge = props.edge,
191 edge = _props$edge === void 0 ? false : _props$edge,
192 _props$size = props.size,
193 size = _props$size === void 0 ? 'medium' : _props$size,
194 other = (0, _objectWithoutProperties2.default)(props, ["classes", "className", "color", "disabled", "edge", "size"]);
195
196 var icon = _react.default.createElement("span", {
197 className: classes.thumb
198 });
199
200 return _react.default.createElement("span", {
201 className: (0, _clsx.default)(classes.root, className, {
202 start: classes.edgeStart,
203 end: classes.edgeEnd
204 }[edge], {
205 small: classes["size".concat((0, _capitalize.default)(size))]
206 }[size])
207 }, _react.default.createElement(_SwitchBase.default, (0, _extends2.default)({
208 type: "checkbox",
209 icon: icon,
210 checkedIcon: icon,
211 classes: {
212 root: (0, _clsx.default)(classes.switchBase, classes["color".concat((0, _capitalize.default)(color))]),
213 input: classes.input,
214 checked: classes.checked,
215 disabled: classes.disabled
216 },
217 ref: ref,
218 disabled: disabled
219 }, other)), _react.default.createElement("span", {
220 className: classes.track
221 }));
222});
223
224process.env.NODE_ENV !== "production" ? Switch.propTypes = {
225 /**
226 * If `true`, the component is checked.
227 */
228 checked: _propTypes.default.bool,
229
230 /**
231 * The icon to display when the component is checked.
232 */
233 checkedIcon: _propTypes.default.node,
234
235 /**
236 * Override or extend the styles applied to the component.
237 * See [CSS API](#css) below for more details.
238 */
239 classes: _propTypes.default.object.isRequired,
240
241 /**
242 * @ignore
243 */
244 className: _propTypes.default.string,
245
246 /**
247 * The color of the component. It supports those theme colors that make sense for this component.
248 */
249 color: _propTypes.default.oneOf(['primary', 'secondary', 'default']),
250
251 /**
252 * @ignore
253 */
254 defaultChecked: _propTypes.default.bool,
255
256 /**
257 * If `true`, the switch will be disabled.
258 */
259 disabled: _propTypes.default.bool,
260
261 /**
262 * If `true`, the ripple effect will be disabled.
263 */
264 disableRipple: _propTypes.default.bool,
265
266 /**
267 * If given, uses a negative margin to counteract the padding on one
268 * side (this is often helpful for aligning the left or right
269 * side of the icon with content above or below, without ruining the border
270 * size and shape).
271 */
272 edge: _propTypes.default.oneOf(['start', 'end', false]),
273
274 /**
275 * The icon to display when the component is unchecked.
276 */
277 icon: _propTypes.default.node,
278
279 /**
280 * The id of the `input` element.
281 */
282 id: _propTypes.default.string,
283
284 /**
285 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
286 */
287 inputProps: _propTypes.default.object,
288
289 /**
290 * Pass a ref to the `input` element.
291 */
292 inputRef: _utils.refType,
293
294 /**
295 * Callback fired when the state is changed.
296 *
297 * @param {object} event The event source of the callback.
298 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
299 */
300 onChange: _propTypes.default.func,
301
302 /**
303 * If `true`, the `input` element will be required.
304 */
305 required: _propTypes.default.bool,
306
307 /**
308 * The size of the switch.
309 * `small` is equivalent to the dense switch styling.
310 */
311 size: _propTypes.default.oneOf(['small', 'medium']),
312
313 /**
314 * The input component prop `type`.
315 */
316 type: _propTypes.default.string,
317
318 /**
319 * The value of the component. The DOM API casts this to a string.
320 */
321 value: _propTypes.default.any
322} : void 0;
323
324var _default = (0, _withStyles.default)(styles, {
325 name: 'MuiSwitch'
326})(Switch);
327
328exports.default = _default;
\No newline at end of file