UNPKG

5.83 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5const _excluded = ["checked", "defaultChecked", "disabled", "onBlur", "onChange", "onFocus", "onFocusVisible", "readOnly", "required", "slotProps", "slots"];
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import { unstable_composeClasses as composeClasses } from '../composeClasses';
9import { useSwitch } from '../useSwitch';
10import { useSlotProps } from '../utils';
11import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
12import { getSwitchUtilityClass } from './switchClasses';
13import { jsx as _jsx } from "react/jsx-runtime";
14import { jsxs as _jsxs } from "react/jsx-runtime";
15const useUtilityClasses = ownerState => {
16 const {
17 checked,
18 disabled,
19 focusVisible,
20 readOnly
21 } = ownerState;
22 const slots = {
23 root: ['root', checked && 'checked', disabled && 'disabled', focusVisible && 'focusVisible', readOnly && 'readOnly'],
24 thumb: ['thumb'],
25 input: ['input'],
26 track: ['track']
27 };
28 return composeClasses(slots, useClassNamesOverride(getSwitchUtilityClass));
29};
30
31/**
32 * The foundation for building custom-styled switches.
33 *
34 * Demos:
35 *
36 * - [Switch](https://mui.com/base-ui/react-switch/)
37 *
38 * API:
39 *
40 * - [Switch API](https://mui.com/base-ui/react-switch/components-api/#switch)
41 */
42const Switch = /*#__PURE__*/React.forwardRef(function Switch(props, forwardedRef) {
43 var _slots$root, _slots$thumb, _slots$input, _slots$track;
44 const {
45 slotProps = {},
46 slots = {}
47 } = props,
48 other = _objectWithoutPropertiesLoose(props, _excluded);
49 const {
50 getInputProps,
51 checked,
52 disabled,
53 focusVisible,
54 readOnly
55 } = useSwitch(props);
56 const ownerState = _extends({}, props, {
57 checked,
58 disabled,
59 focusVisible,
60 readOnly
61 });
62 const classes = useUtilityClasses(ownerState);
63 const Root = (_slots$root = slots.root) != null ? _slots$root : 'span';
64 const rootProps = useSlotProps({
65 elementType: Root,
66 externalSlotProps: slotProps.root,
67 externalForwardedProps: other,
68 additionalProps: {
69 ref: forwardedRef
70 },
71 ownerState,
72 className: classes.root
73 });
74 const Thumb = (_slots$thumb = slots.thumb) != null ? _slots$thumb : 'span';
75 const thumbProps = useSlotProps({
76 elementType: Thumb,
77 externalSlotProps: slotProps.thumb,
78 ownerState,
79 className: classes.thumb
80 });
81 const Input = (_slots$input = slots.input) != null ? _slots$input : 'input';
82 const inputProps = useSlotProps({
83 elementType: Input,
84 getSlotProps: getInputProps,
85 externalSlotProps: slotProps.input,
86 ownerState,
87 className: classes.input
88 });
89 const Track = slots.track === null ? () => null : (_slots$track = slots.track) != null ? _slots$track : 'span';
90 const trackProps = useSlotProps({
91 elementType: Track,
92 externalSlotProps: slotProps.track,
93 ownerState,
94 className: classes.track
95 });
96 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
97 children: [/*#__PURE__*/_jsx(Track, _extends({}, trackProps)), /*#__PURE__*/_jsx(Thumb, _extends({}, thumbProps)), /*#__PURE__*/_jsx(Input, _extends({}, inputProps))]
98 }));
99});
100process.env.NODE_ENV !== "production" ? Switch.propTypes /* remove-proptypes */ = {
101 // ┌────────────────────────────── Warning ──────────────────────────────┐
102 // │ These PropTypes are generated from the TypeScript type definitions. │
103 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
104 // └─────────────────────────────────────────────────────────────────────┘
105 /**
106 * If `true`, the component is checked.
107 */
108 checked: PropTypes.bool,
109 /**
110 * Class name applied to the root element.
111 */
112 className: PropTypes.string,
113 /**
114 * The default checked state. Use when the component is not controlled.
115 */
116 defaultChecked: PropTypes.bool,
117 /**
118 * If `true`, the component is disabled.
119 */
120 disabled: PropTypes.bool,
121 /**
122 * @ignore
123 */
124 onBlur: PropTypes.func,
125 /**
126 * Callback fired when the state is changed.
127 *
128 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
129 * You can pull out the new value by accessing `event.target.value` (string).
130 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
131 */
132 onChange: PropTypes.func,
133 /**
134 * @ignore
135 */
136 onFocus: PropTypes.func,
137 /**
138 * @ignore
139 */
140 onFocusVisible: PropTypes.func,
141 /**
142 * If `true`, the component is read only.
143 */
144 readOnly: PropTypes.bool,
145 /**
146 * If `true`, the `input` element is required.
147 */
148 required: PropTypes.bool,
149 /**
150 * The props used for each slot inside the Switch.
151 * @default {}
152 */
153 slotProps: PropTypes.shape({
154 input: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
155 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
156 thumb: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
157 track: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
158 }),
159 /**
160 * The components used for each slot inside the Switch.
161 * Either a string to use a HTML element or a component.
162 * @default {}
163 */
164 slots: PropTypes /* @typescript-to-proptypes-ignore */.shape({
165 input: PropTypes.elementType,
166 root: PropTypes.elementType,
167 thumb: PropTypes.elementType,
168 track: PropTypes.oneOfType([PropTypes.elementType, PropTypes.oneOf([null])])
169 })
170} : void 0;
171export { Switch };
\No newline at end of file