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.

161 lines (159 loc) 6.31 kB
"use strict"; 'use client'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.ToggleGroup = void 0; var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _noop = require("../utils/noop"); var _useComponentRenderer = require("../utils/useComponentRenderer"); var _CompositeRoot = require("../composite/root/CompositeRoot"); var _DirectionContext = require("../direction-provider/DirectionContext"); var _useToggleGroup = require("./useToggleGroup"); var _ToggleGroupContext = require("./ToggleGroupContext"); 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; } const customStyleHookMapping = { multiple(value) { if (value) { return { 'data-multiple': '' }; } return null; } }; /** * Provides a shared state to a series of toggle buttons. * * Documentation: [Base UI Toggle Group](https://base-ui.com/react/components/toggle-group) */ const ToggleGroup = exports.ToggleGroup = /*#__PURE__*/React.forwardRef(function ToggleGroup(props, forwardedRef) { const { defaultValue: defaultValueProp, disabled = false, loop = true, onValueChange: onValueChangeProp, orientation = 'horizontal', toggleMultiple = false, value: valueProp, className, render, ...otherProps } = props; const direction = (0, _DirectionContext.useDirection)(); const defaultValue = React.useMemo(() => { if (valueProp === undefined) { return defaultValueProp ?? []; } return undefined; }, [valueProp, defaultValueProp]); const { getRootProps, disabled: isDisabled, setGroupValue, value } = (0, _useToggleGroup.useToggleGroup)({ value: valueProp, defaultValue, disabled, toggleMultiple, onValueChange: onValueChangeProp ?? _noop.NOOP }); const state = React.useMemo(() => ({ disabled: isDisabled, multiple: toggleMultiple, orientation }), [isDisabled, orientation, toggleMultiple]); const contextValue = React.useMemo(() => ({ disabled: isDisabled, orientation, setGroupValue, value }), [isDisabled, orientation, setGroupValue, value]); const { renderElement } = (0, _useComponentRenderer.useComponentRenderer)({ propGetter: getRootProps, render: render ?? 'div', ref: forwardedRef, state, className, customStyleHookMapping, extraProps: otherProps }); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ToggleGroupContext.ToggleGroupContext.Provider, { value: contextValue, children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CompositeRoot.CompositeRoot, { direction: direction, loop: loop, render: renderElement() }) }); }); process.env.NODE_ENV !== "production" ? ToggleGroup.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * @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]), /** * The open state of the ToggleGroup represented by an array of * the values of all pressed `<ToggleGroup.Item/>`s. * This is the uncontrolled counterpart of `value`. */ defaultValue: _propTypes.default.array, /** * Whether the component should ignore user interaction. * @default false */ disabled: _propTypes.default.bool, /** * Whether to loop keyboard focus back to the first item * when the end of the list is reached while using the arrow keys. * @default true */ loop: _propTypes.default.bool, /** * Callback fired when the pressed states of the ToggleGroup changes. * * @param {any[]} groupValue An array of the `value`s of all the pressed items. * @param {Event} event The corresponding event that initiated the change. */ onValueChange: _propTypes.default.func, /** * @default 'horizontal' */ orientation: _propTypes.default.oneOf(['horizontal', 'vertical']), /** * 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]), /** * When `false` only one item in the group can be pressed. If any item in * the group becomes pressed, the others will become unpressed. * When `true` multiple items can be pressed. * @default false */ toggleMultiple: _propTypes.default.bool, /** * The open state of the ToggleGroup represented by an array of * the values of all pressed `<ToggleGroup.Item/>`s * This is the controlled counterpart of `defaultValue`. */ value: _propTypes.default.array } : void 0;