@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
141 lines (140 loc) • 5.66 kB
JavaScript
"use strict";
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SwitchRoot = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _useSwitchRoot = require("./useSwitchRoot");
var _SwitchRootContext = require("./SwitchRootContext");
var _styleHooks = require("../styleHooks");
var _useComponentRenderer = require("../../utils/useComponentRenderer");
var _FieldRootContext = require("../../field/root/FieldRootContext");
var _proptypes = require("../../utils/proptypes");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* Represents the switch itself.
* Renders a `<button>` element and a hidden `<input>` beside.
*
* Documentation: [Base UI Switch](https://base-ui.com/react/components/switch)
*/const SwitchRoot = exports.SwitchRoot = /*#__PURE__*/React.forwardRef(function SwitchRoot(props, forwardedRef) {
const {
checked: checkedProp,
className,
defaultChecked,
inputRef,
onCheckedChange,
readOnly = false,
required = false,
disabled: disabledProp = false,
render,
...other
} = props;
const {
getInputProps,
getButtonProps,
checked
} = (0, _useSwitchRoot.useSwitchRoot)(props);
const {
state: fieldState,
disabled: fieldDisabled
} = (0, _FieldRootContext.useFieldRootContext)();
const disabled = fieldDisabled || disabledProp;
const state = React.useMemo(() => ({
...fieldState,
checked,
disabled,
readOnly,
required
}), [fieldState, checked, disabled, readOnly, required]);
const {
renderElement
} = (0, _useComponentRenderer.useComponentRenderer)({
render: render || 'button',
className,
propGetter: getButtonProps,
state,
extraProps: other,
customStyleHookMapping: _styleHooks.styleHookMapping,
ref: forwardedRef
});
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_SwitchRootContext.SwitchRootContext.Provider, {
value: state,
children: [renderElement(), !checked && props.name && /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
type: "hidden",
name: props.name,
value: "off"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
...getInputProps()
})]
});
});
process.env.NODE_ENV !== "production" ? SwitchRoot.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Whether the switch is currently active.
*
* To render an uncontrolled switch, use the `defaultChecked` prop instead.
*/
checked: _propTypes.default.bool,
/**
* @ignore
*/
children: _propTypes.default.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]),
/**
* Whether the switch is initially active.
*
* To render a controlled switch, use the `checked` prop instead.
* @default false
*/
defaultChecked: _propTypes.default.bool,
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: _propTypes.default.bool,
/**
* A React ref to access the hidden `<input>` element.
*/
inputRef: _proptypes.refType,
/**
* Identifies the field when a form is submitted.
*/
name: _propTypes.default.string,
/**
* Event handler called when the switch is activated or deactivated.
*
* @param {boolean} checked The new checked state.
* @param {Event} event The corresponding event that initiated the change.
*/
onCheckedChange: _propTypes.default.func,
/**
* Whether the user should be unable to activate or deactivate the switch.
* @default false
*/
readOnly: _propTypes.default.bool,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: _propTypes.default.oneOfType([_propTypes.default.element, _propTypes.default.func]),
/**
* Whether the user must activate the switch before submitting a form.
* @default false
*/
required: _propTypes.default.bool
} : void 0;