@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.
182 lines (179 loc) • 7.45 kB
JavaScript
"use strict";
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AccordionRoot = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _noop = require("../../utils/noop");
var _useComponentRenderer = require("../../utils/useComponentRenderer");
var _useEnhancedEffect = require("../../utils/useEnhancedEffect");
var _warn = require("../../utils/warn");
var _CompositeList = require("../../composite/list/CompositeList");
var _DirectionContext = require("../../direction-provider/DirectionContext");
var _useAccordionRoot = require("./useAccordionRoot");
var _AccordionRootContext = require("./AccordionRootContext");
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 rootStyleHookMapping = {
value: () => null
};
/**
* Groups all parts of the accordion.
* Renders a `<div>` element.
*
* Documentation: [Base UI Accordion](https://base-ui.com/react/components/accordion)
*/
const AccordionRoot = exports.AccordionRoot = /*#__PURE__*/React.forwardRef(function AccordionRoot(props, forwardedRef) {
const {
className,
disabled = false,
hiddenUntilFound: hiddenUntilFoundProp,
keepMounted: keepMountedProp,
loop = true,
onValueChange: onValueChangeProp,
openMultiple = true,
orientation = 'vertical',
value,
defaultValue: defaultValueProp,
render,
...otherProps
} = props;
const direction = (0, _DirectionContext.useDirection)();
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
(0, _useEnhancedEffect.useEnhancedEffect)(() => {
if (hiddenUntilFoundProp && keepMountedProp === false) {
(0, _warn.warn)('The `keepMounted={false}` prop on a Accordion.Root will be ignored when using `hiddenUntilFound` since it requires Panels to remain mounted when closed.');
}
}, [hiddenUntilFoundProp, keepMountedProp]);
}
// memoized to allow omitting both defaultValue and value
// which would otherwise trigger a warning in useControlled
const defaultValue = React.useMemo(() => {
if (value === undefined) {
return defaultValueProp ?? [];
}
return undefined;
}, [value, defaultValueProp]);
const {
getRootProps,
...accordion
} = (0, _useAccordionRoot.useAccordionRoot)({
direction,
disabled,
defaultValue,
loop,
orientation,
onValueChange: onValueChangeProp ?? _noop.NOOP,
openMultiple,
value
});
const state = React.useMemo(() => ({
value: accordion.value,
disabled: accordion.disabled,
orientation: accordion.orientation
}), [accordion.value, accordion.disabled, accordion.orientation]);
const contextValue = React.useMemo(() => ({
...accordion,
hiddenUntilFound: hiddenUntilFoundProp ?? false,
keepMounted: keepMountedProp ?? false,
state
}), [accordion, hiddenUntilFoundProp, keepMountedProp, state]);
const {
renderElement
} = (0, _useComponentRenderer.useComponentRenderer)({
propGetter: getRootProps,
render: render ?? 'div',
className,
state,
ref: forwardedRef,
extraProps: otherProps,
customStyleHookMapping: rootStyleHookMapping
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_AccordionRootContext.AccordionRootContext.Provider, {
value: contextValue,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_CompositeList.CompositeList, {
elementsRef: accordion.accordionItemRefs,
children: renderElement()
})
});
});
process.env.NODE_ENV !== "production" ? AccordionRoot.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 uncontrolled value of the item(s) that should be initially expanded.
*
* To render a controlled accordion, use the `value` prop instead.
*/
defaultValue: _propTypes.default.array,
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled: _propTypes.default.bool,
/**
* Allows the browser’s built-in page search to find and expand the panel contents.
*
* Overrides the `keepMounted` prop and uses `hidden="until-found"`
* to hide the element without removing it from the DOM.
* @default false
*/
hiddenUntilFound: _propTypes.default.bool,
/**
* Whether to keep the element in the DOM while the panel is closed.
* This prop is ignored when `hiddenUntilFound` is used.
* @default false
*/
keepMounted: _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,
/**
* Event handler called when an accordion item is expanded or collapsed.
* Provides the new value as an argument.
*/
onValueChange: _propTypes.default.func,
/**
* Whether multiple items can be open at the same time.
* @default true
*/
openMultiple: _propTypes.default.bool,
/**
* The visual orientation of the accordion.
* Controls whether roving focus uses left/right or up/down arrow keys.
* @default 'vertical'
*/
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]),
/**
* The controlled value of the item(s) that should be expanded.
*
* To render an uncontrolled accordion, use the `defaultValue` prop instead.
*/
value: _propTypes.default.array
} : void 0;