UNPKG

@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.

136 lines (134 loc) 4.38 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useSwitchRoot } from './useSwitchRoot.js'; import { SwitchRootContext } from './SwitchRootContext.js'; import { styleHookMapping } from '../styleHooks.js'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useFieldRootContext } from '../../field/root/FieldRootContext.js'; import { refType } from '../../utils/proptypes.js'; /** * Represents the switch itself. * Renders a `<button>` element and a hidden `<input>` beside. * * Documentation: [Base UI Switch](https://base-ui.com/react/components/switch) */ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const 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 } = useSwitchRoot(props); const { state: fieldState, disabled: fieldDisabled } = useFieldRootContext(); const disabled = fieldDisabled || disabledProp; const state = React.useMemo(() => ({ ...fieldState, checked, disabled, readOnly, required }), [fieldState, checked, disabled, readOnly, required]); const { renderElement } = useComponentRenderer({ render: render || 'button', className, propGetter: getButtonProps, state, extraProps: other, customStyleHookMapping: styleHookMapping, ref: forwardedRef }); return /*#__PURE__*/_jsxs(SwitchRootContext.Provider, { value: state, children: [renderElement(), !checked && props.name && /*#__PURE__*/_jsx("input", { type: "hidden", name: props.name, value: "off" }), /*#__PURE__*/_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.bool, /** * @ignore */ children: PropTypes.node, /** * CSS class applied to the element, or a function that * returns a class based on the component’s state. */ className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]), /** * Whether the switch is initially active. * * To render a controlled switch, use the `checked` prop instead. * @default false */ defaultChecked: PropTypes.bool, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * A React ref to access the hidden `<input>` element. */ inputRef: refType, /** * Identifies the field when a form is submitted. */ name: PropTypes.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.func, /** * Whether the user should be unable to activate or deactivate the switch. * @default false */ readOnly: PropTypes.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.oneOfType([PropTypes.element, PropTypes.func]), /** * Whether the user must activate the switch before submitting a form. * @default false */ required: PropTypes.bool } : void 0; export { SwitchRoot };