UNPKG

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