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.

109 lines (107 loc) 3.57 kB
'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 { useCollapsibleRoot } from './useCollapsibleRoot.js'; import { CollapsibleRootContext } from './CollapsibleRootContext.js'; import { collapsibleStyleHookMapping } from './styleHooks.js'; /** * Groups all parts of the collapsible. * Renders a `<div>` element. * * Documentation: [Base UI Collapsible](https://base-ui.com/react/components/collapsible) */ import { jsx as _jsx } from "react/jsx-runtime"; const CollapsibleRoot = /*#__PURE__*/React.forwardRef(function CollapsibleRoot(props, forwardedRef) { const { children, className, defaultOpen = false, disabled = false, onOpenChange: onOpenChangeProp, open, render: renderProp, ...otherProps } = props; const collapsible = useCollapsibleRoot({ open, defaultOpen, onOpenChange: onOpenChangeProp ?? NOOP, disabled }); const state = React.useMemo(() => ({ open: collapsible.open, disabled: collapsible.disabled, transitionStatus: collapsible.transitionStatus }), [collapsible.open, collapsible.disabled, collapsible.transitionStatus]); const contextValue = React.useMemo(() => ({ ...collapsible, state }), [collapsible, state]); const { renderElement } = useComponentRenderer({ render: renderProp ?? 'div', className, state, ref: forwardedRef, extraProps: { children, ...otherProps }, customStyleHookMapping: collapsibleStyleHookMapping }); if (renderProp !== null) { return /*#__PURE__*/_jsx(CollapsibleRootContext.Provider, { value: contextValue, children: renderElement() }); } return /*#__PURE__*/_jsx(CollapsibleRootContext.Provider, { value: contextValue, children: children }); }); export { CollapsibleRoot }; process.env.NODE_ENV !== "production" ? CollapsibleRoot.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]), /** * Whether the collapsible panel is initially open. * * To render a controlled collapsible, use the `open` prop instead. * @default false */ defaultOpen: PropTypes.bool, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * Event handler called when the panel is opened or closed. */ onOpenChange: PropTypes.func, /** * Whether the collapsible panel is currently open. * * To render an uncontrolled collapsible, use the `defaultOpen` prop instead. */ open: PropTypes.bool, /** * @ignore */ render: PropTypes.oneOfType([PropTypes.element, PropTypes.func]) } : void 0;