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.

140 lines (139 loc) 5.72 kB
"use strict"; 'use client'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.CollapsiblePanel = void 0; var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _useComponentRenderer = require("../../utils/useComponentRenderer"); var _useEnhancedEffect = require("../../utils/useEnhancedEffect"); var _warn = require("../../utils/warn"); var _CollapsibleRootContext = require("../root/CollapsibleRootContext"); var _styleHooks = require("../root/styleHooks"); var _useCollapsiblePanel = require("./useCollapsiblePanel"); var _CollapsiblePanelCssVars = require("./CollapsiblePanelCssVars"); 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; } /** * A panel with the collapsible contents. * Renders a `<div>` element. * * Documentation: [Base UI Collapsible](https://base-ui.com/react/components/collapsible) */ const CollapsiblePanel = exports.CollapsiblePanel = /*#__PURE__*/React.forwardRef(function CollapsiblePanel(props, forwardedRef) { const { className, hiddenUntilFound: hiddenUntilFoundProp, id: idProp, keepMounted: keepMountedProp, render, ...otherProps } = props; 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 Collapsible will be ignored when using `hiddenUntilFound` since it requires the Panel to remain mounted even when closed.'); } }, [hiddenUntilFoundProp, keepMountedProp]); } const { mounted, open, panelId, setPanelId, setMounted, setOpen, state } = (0, _CollapsibleRootContext.useCollapsibleRootContext)(); (0, _useEnhancedEffect.useEnhancedEffect)(() => { if (idProp) { setPanelId(idProp); } }, [idProp, setPanelId]); const hiddenUntilFound = hiddenUntilFoundProp ?? false; const { getRootProps, height, width, isOpen } = (0, _useCollapsiblePanel.useCollapsiblePanel)({ hiddenUntilFound, panelId, keepMounted: keepMountedProp ?? false, mounted, open, ref: forwardedRef, setPanelId, setMounted, setOpen }); const { renderElement } = (0, _useComponentRenderer.useComponentRenderer)({ propGetter: getRootProps, render: render ?? 'div', state, className, extraProps: { ...otherProps, style: { ...otherProps.style, [_CollapsiblePanelCssVars.CollapsiblePanelCssVars.collapsiblePanelHeight]: height ? `${height}px` : undefined, [_CollapsiblePanelCssVars.CollapsiblePanelCssVars.collapsiblePanelWidth]: width ? `${width}px` : undefined } }, customStyleHookMapping: _styleHooks.collapsibleStyleHookMapping }); if (!keepMountedProp && !isOpen && !hiddenUntilFound) { return null; } return renderElement(); }); process.env.NODE_ENV !== "production" ? CollapsiblePanel.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]), /** * 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, /** * @ignore */ id: _propTypes.default.string, /** * Whether to keep the element in the DOM while the panel is hidden. * This prop is ignored when `hiddenUntilFound` is used. * @default false */ keepMounted: _propTypes.default.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.default.oneOfType([_propTypes.default.element, _propTypes.default.func]), /** * @ignore */ style: _propTypes.default.object } : void 0;