UNPKG

11.1 kBJavaScriptView Raw
1'use client';
2
3// @inheritedComponent IconButton
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import clsx from 'clsx';
7import refType from '@mui/utils/refType';
8import composeClasses from '@mui/utils/composeClasses';
9import { alpha, darken, lighten } from '@mui/system/colorManipulator';
10import capitalize from "../utils/capitalize.js";
11import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
12import SwitchBase from "../internal/SwitchBase.js";
13import { styled } from "../zero-styled/index.js";
14import memoTheme from "../utils/memoTheme.js";
15import { useDefaultProps } from "../DefaultPropsProvider/index.js";
16import switchClasses, { getSwitchUtilityClass } from "./switchClasses.js";
17import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
18const useUtilityClasses = ownerState => {
19 const {
20 classes,
21 edge,
22 size,
23 color,
24 checked,
25 disabled
26 } = ownerState;
27 const slots = {
28 root: ['root', edge && `edge${capitalize(edge)}`, `size${capitalize(size)}`],
29 switchBase: ['switchBase', `color${capitalize(color)}`, checked && 'checked', disabled && 'disabled'],
30 thumb: ['thumb'],
31 track: ['track'],
32 input: ['input']
33 };
34 const composedClasses = composeClasses(slots, getSwitchUtilityClass, classes);
35 return {
36 ...classes,
37 // forward the disabled and checked classes to the SwitchBase
38 ...composedClasses
39 };
40};
41const SwitchRoot = styled('span', {
42 name: 'MuiSwitch',
43 slot: 'Root',
44 overridesResolver: (props, styles) => {
45 const {
46 ownerState
47 } = props;
48 return [styles.root, ownerState.edge && styles[`edge${capitalize(ownerState.edge)}`], styles[`size${capitalize(ownerState.size)}`]];
49 }
50})({
51 display: 'inline-flex',
52 width: 34 + 12 * 2,
53 height: 14 + 12 * 2,
54 overflow: 'hidden',
55 padding: 12,
56 boxSizing: 'border-box',
57 position: 'relative',
58 flexShrink: 0,
59 zIndex: 0,
60 // Reset the stacking context.
61 verticalAlign: 'middle',
62 // For correct alignment with the text.
63 '@media print': {
64 colorAdjust: 'exact'
65 },
66 variants: [{
67 props: {
68 edge: 'start'
69 },
70 style: {
71 marginLeft: -8
72 }
73 }, {
74 props: {
75 edge: 'end'
76 },
77 style: {
78 marginRight: -8
79 }
80 }, {
81 props: {
82 size: 'small'
83 },
84 style: {
85 width: 40,
86 height: 24,
87 padding: 7,
88 [`& .${switchClasses.thumb}`]: {
89 width: 16,
90 height: 16
91 },
92 [`& .${switchClasses.switchBase}`]: {
93 padding: 4,
94 [`&.${switchClasses.checked}`]: {
95 transform: 'translateX(16px)'
96 }
97 }
98 }
99 }]
100});
101const SwitchSwitchBase = styled(SwitchBase, {
102 name: 'MuiSwitch',
103 slot: 'SwitchBase',
104 overridesResolver: (props, styles) => {
105 const {
106 ownerState
107 } = props;
108 return [styles.switchBase, {
109 [`& .${switchClasses.input}`]: styles.input
110 }, ownerState.color !== 'default' && styles[`color${capitalize(ownerState.color)}`]];
111 }
112})(memoTheme(({
113 theme
114}) => ({
115 position: 'absolute',
116 top: 0,
117 left: 0,
118 zIndex: 1,
119 // Render above the focus ripple.
120 color: theme.vars ? theme.vars.palette.Switch.defaultColor : `${theme.palette.mode === 'light' ? theme.palette.common.white : theme.palette.grey[300]}`,
121 transition: theme.transitions.create(['left', 'transform'], {
122 duration: theme.transitions.duration.shortest
123 }),
124 [`&.${switchClasses.checked}`]: {
125 transform: 'translateX(20px)'
126 },
127 [`&.${switchClasses.disabled}`]: {
128 color: theme.vars ? theme.vars.palette.Switch.defaultDisabledColor : `${theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[600]}`
129 },
130 [`&.${switchClasses.checked} + .${switchClasses.track}`]: {
131 opacity: 0.5
132 },
133 [`&.${switchClasses.disabled} + .${switchClasses.track}`]: {
134 opacity: theme.vars ? theme.vars.opacity.switchTrackDisabled : `${theme.palette.mode === 'light' ? 0.12 : 0.2}`
135 },
136 [`& .${switchClasses.input}`]: {
137 left: '-100%',
138 width: '300%'
139 }
140})), memoTheme(({
141 theme
142}) => ({
143 '&:hover': {
144 backgroundColor: theme.vars ? `rgba(${theme.vars.palette.action.activeChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette.action.active, theme.palette.action.hoverOpacity),
145 // Reset on touch devices, it doesn't add specificity
146 '@media (hover: none)': {
147 backgroundColor: 'transparent'
148 }
149 },
150 variants: [...Object.entries(theme.palette).filter(createSimplePaletteValueFilter(['light'])) // check all the used fields in the style below
151 .map(([color]) => ({
152 props: {
153 color
154 },
155 style: {
156 [`&.${switchClasses.checked}`]: {
157 color: (theme.vars || theme).palette[color].main,
158 '&:hover': {
159 backgroundColor: theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette[color].main, theme.palette.action.hoverOpacity),
160 '@media (hover: none)': {
161 backgroundColor: 'transparent'
162 }
163 },
164 [`&.${switchClasses.disabled}`]: {
165 color: theme.vars ? theme.vars.palette.Switch[`${color}DisabledColor`] : `${theme.palette.mode === 'light' ? lighten(theme.palette[color].main, 0.62) : darken(theme.palette[color].main, 0.55)}`
166 }
167 },
168 [`&.${switchClasses.checked} + .${switchClasses.track}`]: {
169 backgroundColor: (theme.vars || theme).palette[color].main
170 }
171 }
172 }))]
173})));
174const SwitchTrack = styled('span', {
175 name: 'MuiSwitch',
176 slot: 'Track',
177 overridesResolver: (props, styles) => styles.track
178})(memoTheme(({
179 theme
180}) => ({
181 height: '100%',
182 width: '100%',
183 borderRadius: 14 / 2,
184 zIndex: -1,
185 transition: theme.transitions.create(['opacity', 'background-color'], {
186 duration: theme.transitions.duration.shortest
187 }),
188 backgroundColor: theme.vars ? theme.vars.palette.common.onBackground : `${theme.palette.mode === 'light' ? theme.palette.common.black : theme.palette.common.white}`,
189 opacity: theme.vars ? theme.vars.opacity.switchTrack : `${theme.palette.mode === 'light' ? 0.38 : 0.3}`
190})));
191const SwitchThumb = styled('span', {
192 name: 'MuiSwitch',
193 slot: 'Thumb',
194 overridesResolver: (props, styles) => styles.thumb
195})(memoTheme(({
196 theme
197}) => ({
198 boxShadow: (theme.vars || theme).shadows[1],
199 backgroundColor: 'currentColor',
200 width: 20,
201 height: 20,
202 borderRadius: '50%'
203})));
204const Switch = /*#__PURE__*/React.forwardRef(function Switch(inProps, ref) {
205 const props = useDefaultProps({
206 props: inProps,
207 name: 'MuiSwitch'
208 });
209 const {
210 className,
211 color = 'primary',
212 edge = false,
213 size = 'medium',
214 sx,
215 ...other
216 } = props;
217 const ownerState = {
218 ...props,
219 color,
220 edge,
221 size
222 };
223 const classes = useUtilityClasses(ownerState);
224 const icon = /*#__PURE__*/_jsx(SwitchThumb, {
225 className: classes.thumb,
226 ownerState: ownerState
227 });
228 return /*#__PURE__*/_jsxs(SwitchRoot, {
229 className: clsx(classes.root, className),
230 sx: sx,
231 ownerState: ownerState,
232 children: [/*#__PURE__*/_jsx(SwitchSwitchBase, {
233 type: "checkbox",
234 icon: icon,
235 checkedIcon: icon,
236 ref: ref,
237 ownerState: ownerState,
238 ...other,
239 classes: {
240 ...classes,
241 root: classes.switchBase
242 }
243 }), /*#__PURE__*/_jsx(SwitchTrack, {
244 className: classes.track,
245 ownerState: ownerState
246 })]
247 });
248});
249process.env.NODE_ENV !== "production" ? Switch.propTypes /* remove-proptypes */ = {
250 // ┌────────────────────────────── Warning ──────────────────────────────┐
251 // │ These PropTypes are generated from the TypeScript type definitions. │
252 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
253 // └─────────────────────────────────────────────────────────────────────┘
254 /**
255 * If `true`, the component is checked.
256 */
257 checked: PropTypes.bool,
258 /**
259 * The icon to display when the component is checked.
260 */
261 checkedIcon: PropTypes.node,
262 /**
263 * Override or extend the styles applied to the component.
264 */
265 classes: PropTypes.object,
266 /**
267 * @ignore
268 */
269 className: PropTypes.string,
270 /**
271 * The color of the component.
272 * It supports both default and custom theme colors, which can be added as shown in the
273 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
274 * @default 'primary'
275 */
276 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
277 /**
278 * The default checked state. Use when the component is not controlled.
279 */
280 defaultChecked: PropTypes.bool,
281 /**
282 * If `true`, the component is disabled.
283 */
284 disabled: PropTypes.bool,
285 /**
286 * If `true`, the ripple effect is disabled.
287 * @default false
288 */
289 disableRipple: PropTypes.bool,
290 /**
291 * If given, uses a negative margin to counteract the padding on one
292 * side (this is often helpful for aligning the left or right
293 * side of the icon with content above or below, without ruining the border
294 * size and shape).
295 * @default false
296 */
297 edge: PropTypes.oneOf(['end', 'start', false]),
298 /**
299 * The icon to display when the component is unchecked.
300 */
301 icon: PropTypes.node,
302 /**
303 * The id of the `input` element.
304 */
305 id: PropTypes.string,
306 /**
307 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
308 */
309 inputProps: PropTypes.object,
310 /**
311 * Pass a ref to the `input` element.
312 */
313 inputRef: refType,
314 /**
315 * Callback fired when the state is changed.
316 *
317 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
318 * You can pull out the new value by accessing `event.target.value` (string).
319 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
320 */
321 onChange: PropTypes.func,
322 /**
323 * If `true`, the `input` element is required.
324 * @default false
325 */
326 required: PropTypes.bool,
327 /**
328 * The size of the component.
329 * `small` is equivalent to the dense switch styling.
330 * @default 'medium'
331 */
332 size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
333 /**
334 * The system prop that allows defining system overrides as well as additional CSS styles.
335 */
336 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
337 /**
338 * The value of the component. The DOM API casts this to a string.
339 * The browser uses "on" as the default value.
340 */
341 value: PropTypes.any
342} : void 0;
343export default Switch;
\No newline at end of file