1 | 'use client';
|
2 |
|
3 |
|
4 | import * as React from 'react';
|
5 | import PropTypes from 'prop-types';
|
6 | import clsx from 'clsx';
|
7 | import refType from '@mui/utils/refType';
|
8 | import composeClasses from '@mui/utils/composeClasses';
|
9 | import { alpha, darken, lighten } from '@mui/system/colorManipulator';
|
10 | import capitalize from "../utils/capitalize.js";
|
11 | import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
|
12 | import SwitchBase from "../internal/SwitchBase.js";
|
13 | import { styled } from "../zero-styled/index.js";
|
14 | import memoTheme from "../utils/memoTheme.js";
|
15 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
16 | import switchClasses, { getSwitchUtilityClass } from "./switchClasses.js";
|
17 | import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
18 | const 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 |
|
38 | ...composedClasses
|
39 | };
|
40 | };
|
41 | const 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 |
|
61 | verticalAlign: 'middle',
|
62 |
|
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 | });
|
101 | const 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 |
|
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 |
|
146 | '@media (hover: none)': {
|
147 | backgroundColor: 'transparent'
|
148 | }
|
149 | },
|
150 | variants: [...Object.entries(theme.palette).filter(createSimplePaletteValueFilter(['light']))
|
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 | })));
|
174 | const 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 | })));
|
191 | const 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 | })));
|
204 | const Switch = 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 = _jsx(SwitchThumb, {
|
225 | className: classes.thumb,
|
226 | ownerState: ownerState
|
227 | });
|
228 | return _jsxs(SwitchRoot, {
|
229 | className: clsx(classes.root, className),
|
230 | sx: sx,
|
231 | ownerState: ownerState,
|
232 | children: [_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 | }), _jsx(SwitchTrack, {
|
244 | className: classes.track,
|
245 | ownerState: ownerState
|
246 | })]
|
247 | });
|
248 | });
|
249 | process.env.NODE_ENV !== "production" ? Switch.propTypes = {
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | |
255 |
|
256 |
|
257 | checked: PropTypes.bool,
|
258 | |
259 |
|
260 |
|
261 | checkedIcon: PropTypes.node,
|
262 | |
263 |
|
264 |
|
265 | classes: PropTypes.object,
|
266 | |
267 |
|
268 |
|
269 | className: PropTypes.string,
|
270 | |
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | color: PropTypes .oneOfType([PropTypes.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
|
277 | |
278 |
|
279 |
|
280 | defaultChecked: PropTypes.bool,
|
281 | |
282 |
|
283 |
|
284 | disabled: PropTypes.bool,
|
285 | |
286 |
|
287 |
|
288 |
|
289 | disableRipple: PropTypes.bool,
|
290 | |
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | edge: PropTypes.oneOf(['end', 'start', false]),
|
298 | |
299 |
|
300 |
|
301 | icon: PropTypes.node,
|
302 | |
303 |
|
304 |
|
305 | id: PropTypes.string,
|
306 | |
307 |
|
308 |
|
309 | inputProps: PropTypes.object,
|
310 | |
311 |
|
312 |
|
313 | inputRef: refType,
|
314 | |
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | onChange: PropTypes.func,
|
322 | |
323 |
|
324 |
|
325 |
|
326 | required: PropTypes.bool,
|
327 | |
328 |
|
329 |
|
330 |
|
331 |
|
332 | size: PropTypes .oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
|
333 | |
334 |
|
335 |
|
336 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
337 | |
338 |
|
339 |
|
340 |
|
341 | value: PropTypes.any
|
342 | } : void 0;
|
343 | export default Switch; |
\ | No newline at end of file |