@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.
154 lines (152 loc) • 5.03 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { NOOP } from '../utils/noop.js';
import { useComponentRenderer } from '../utils/useComponentRenderer.js';
import { CompositeRoot } from '../composite/root/CompositeRoot.js';
import { useDirection } from '../direction-provider/DirectionContext.js';
import { useToggleGroup } from './useToggleGroup.js';
import { ToggleGroupContext } from './ToggleGroupContext.js';
import { jsx as _jsx } from "react/jsx-runtime";
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 = /*#__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 = useDirection();
const defaultValue = React.useMemo(() => {
if (valueProp === undefined) {
return defaultValueProp ?? [];
}
return undefined;
}, [valueProp, defaultValueProp]);
const {
getRootProps,
disabled: isDisabled,
setGroupValue,
value
} = useToggleGroup({
value: valueProp,
defaultValue,
disabled,
toggleMultiple,
onValueChange: onValueChangeProp ?? 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
} = useComponentRenderer({
propGetter: getRootProps,
render: render ?? 'div',
ref: forwardedRef,
state,
className,
customStyleHookMapping,
extraProps: otherProps
});
return /*#__PURE__*/_jsx(ToggleGroupContext.Provider, {
value: contextValue,
children: /*#__PURE__*/_jsx(CompositeRoot, {
direction: direction,
loop: loop,
render: renderElement()
})
});
});
export { ToggleGroup };
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.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]),
/**
* 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.array,
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: PropTypes.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.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.func,
/**
* @default 'horizontal'
*/
orientation: PropTypes.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.oneOfType([PropTypes.element, PropTypes.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.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.array
} : void 0;