@mui/base
Version:
MUI Base is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
189 lines (187 loc) • 6.13 kB
JavaScript
"use strict";
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Switch = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _composeClasses = require("../composeClasses");
var _useSwitch = require("../useSwitch");
var _utils = require("../utils");
var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
var _switchClasses = require("./switchClasses");
var _jsxRuntime = require("react/jsx-runtime");
const useUtilityClasses = ownerState => {
const {
checked,
disabled,
focusVisible,
readOnly
} = ownerState;
const slots = {
root: ['root', checked && 'checked', disabled && 'disabled', focusVisible && 'focusVisible', readOnly && 'readOnly'],
thumb: ['thumb'],
input: ['input'],
track: ['track']
};
return (0, _composeClasses.unstable_composeClasses)(slots, (0, _ClassNameConfigurator.useClassNamesOverride)(_switchClasses.getSwitchUtilityClass));
};
/**
* The foundation for building custom-styled switches.
*
* Demos:
*
* - [Switch](https://mui.com/base-ui/react-switch/)
*
* API:
*
* - [Switch API](https://mui.com/base-ui/react-switch/components-api/#switch)
*/
const Switch = exports.Switch = /*#__PURE__*/React.forwardRef(function Switch(props, forwardedRef) {
const {
checked: checkedProp,
defaultChecked,
disabled: disabledProp,
onBlur,
onChange,
onFocus,
onFocusVisible,
readOnly: readOnlyProp,
required,
slotProps = {},
slots = {},
...other
} = props;
const {
getInputProps,
checked,
disabled,
focusVisible,
readOnly
} = (0, _useSwitch.useSwitch)(props);
const ownerState = {
...props,
checked,
disabled,
focusVisible,
readOnly
};
const classes = useUtilityClasses(ownerState);
const Root = slots.root ?? 'span';
const rootProps = (0, _utils.useSlotProps)({
elementType: Root,
externalSlotProps: slotProps.root,
externalForwardedProps: other,
additionalProps: {
ref: forwardedRef
},
ownerState,
className: classes.root
});
const Thumb = slots.thumb ?? 'span';
const thumbProps = (0, _utils.useSlotProps)({
elementType: Thumb,
externalSlotProps: slotProps.thumb,
ownerState,
className: classes.thumb
});
const Input = slots.input ?? 'input';
const inputProps = (0, _utils.useSlotProps)({
elementType: Input,
getSlotProps: getInputProps,
externalSlotProps: slotProps.input,
ownerState,
className: classes.input
});
const Track = slots.track === null ? () => null : slots.track ?? 'span';
const trackProps = (0, _utils.useSlotProps)({
elementType: Track,
externalSlotProps: slotProps.track,
ownerState,
className: classes.track
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Root, {
...rootProps,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Track, {
...trackProps
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Thumb, {
...thumbProps
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(Input, {
...inputProps
})]
});
});
process.env.NODE_ENV !== "production" ? Switch.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* If `true`, the component is checked.
*/
checked: _propTypes.default.bool,
/**
* Class name applied to the root element.
*/
className: _propTypes.default.string,
/**
* The default checked state. Use when the component is not controlled.
*/
defaultChecked: _propTypes.default.bool,
/**
* If `true`, the component is disabled.
*/
disabled: _propTypes.default.bool,
/**
* @ignore
*/
onBlur: _propTypes.default.func,
/**
* Callback fired when the state is changed.
*
* @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
* You can pull out the new value by accessing `event.target.value` (string).
* You can pull out the new checked state by accessing `event.target.checked` (boolean).
*/
onChange: _propTypes.default.func,
/**
* @ignore
*/
onFocus: _propTypes.default.func,
/**
* @ignore
*/
onFocusVisible: _propTypes.default.func,
/**
* If `true`, the component is read only.
*/
readOnly: _propTypes.default.bool,
/**
* If `true`, the `input` element is required.
*/
required: _propTypes.default.bool,
/**
* The props used for each slot inside the Switch.
* @default {}
*/
slotProps: _propTypes.default.shape({
input: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
thumb: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
track: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
}),
/**
* The components used for each slot inside the Switch.
* Either a string to use a HTML element or a component.
* @default {}
*/
slots: _propTypes.default /* @typescript-to-proptypes-ignore */.shape({
input: _propTypes.default.elementType,
root: _propTypes.default.elementType,
thumb: _propTypes.default.elementType,
track: _propTypes.default.oneOfType([_propTypes.default.elementType, _propTypes.default.oneOf([null])])
})
} : void 0;