UNPKG

5.61 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 const {
44 slotProps = {},
45 slots = {}
46 } = props,
47 other = _objectWithoutPropertiesLoose(props, _excluded);
48 const {
49 getInputProps,
50 checked,
51 disabled,
52 focusVisible,
53 readOnly
54 } = useSwitch(props);
55 const ownerState = _extends({}, props, {
56 checked,
57 disabled,
58 focusVisible,
59 readOnly
60 });
61 const classes = useUtilityClasses(ownerState);
62 const Root = slots.root ?? 'span';
63 const rootProps = useSlotProps({
64 elementType: Root,
65 externalSlotProps: slotProps.root,
66 externalForwardedProps: other,
67 additionalProps: {
68 ref: forwardedRef
69 },
70 ownerState,
71 className: classes.root
72 });
73 const Thumb = slots.thumb ?? 'span';
74 const thumbProps = useSlotProps({
75 elementType: Thumb,
76 externalSlotProps: slotProps.thumb,
77 ownerState,
78 className: classes.thumb
79 });
80 const Input = slots.input ?? 'input';
81 const inputProps = useSlotProps({
82 elementType: Input,
83 getSlotProps: getInputProps,
84 externalSlotProps: slotProps.input,
85 ownerState,
86 className: classes.input
87 });
88 const Track = slots.track === null ? () => null : slots.track ?? 'span';
89 const trackProps = useSlotProps({
90 elementType: Track,
91 externalSlotProps: slotProps.track,
92 ownerState,
93 className: classes.track
94 });
95 return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, {
96 children: [/*#__PURE__*/_jsx(Track, _extends({}, trackProps)), /*#__PURE__*/_jsx(Thumb, _extends({}, thumbProps)), /*#__PURE__*/_jsx(Input, _extends({}, inputProps))]
97 }));
98});
99process.env.NODE_ENV !== "production" ? Switch.propTypes /* remove-proptypes */ = {
100 // ┌────────────────────────────── Warning ──────────────────────────────┐
101 // │ These PropTypes are generated from the TypeScript type definitions. │
102 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
103 // └─────────────────────────────────────────────────────────────────────┘
104 /**
105 * If `true`, the component is checked.
106 */
107 checked: PropTypes.bool,
108 /**
109 * Class name applied to the root element.
110 */
111 className: PropTypes.string,
112 /**
113 * The default checked state. Use when the component is not controlled.
114 */
115 defaultChecked: PropTypes.bool,
116 /**
117 * If `true`, the component is disabled.
118 */
119 disabled: PropTypes.bool,
120 /**
121 * @ignore
122 */
123 onBlur: PropTypes.func,
124 /**
125 * Callback fired when the state is changed.
126 *
127 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
128 * You can pull out the new value by accessing `event.target.value` (string).
129 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
130 */
131 onChange: PropTypes.func,
132 /**
133 * @ignore
134 */
135 onFocus: PropTypes.func,
136 /**
137 * @ignore
138 */
139 onFocusVisible: PropTypes.func,
140 /**
141 * If `true`, the component is read only.
142 */
143 readOnly: PropTypes.bool,
144 /**
145 * If `true`, the `input` element is required.
146 */
147 required: PropTypes.bool,
148 /**
149 * The props used for each slot inside the Switch.
150 * @default {}
151 */
152 slotProps: PropTypes.shape({
153 input: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
154 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
155 thumb: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
156 track: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
157 }),
158 /**
159 * The components used for each slot inside the Switch.
160 * Either a string to use a HTML element or a component.
161 * @default {}
162 */
163 slots: PropTypes /* @typescript-to-proptypes-ignore */.shape({
164 input: PropTypes.elementType,
165 root: PropTypes.elementType,
166 thumb: PropTypes.elementType,
167 track: PropTypes.oneOfType([PropTypes.elementType, PropTypes.oneOf([null])])
168 })
169} : void 0;
170export { Switch };
\No newline at end of file