@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.
134 lines (132 loc) • 4.43 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect.js';
import { warn } from '../../utils/warn.js';
import { useCollapsibleRootContext } from '../root/CollapsibleRootContext.js';
import { collapsibleStyleHookMapping } from '../root/styleHooks.js';
import { useCollapsiblePanel } from './useCollapsiblePanel.js';
import { CollapsiblePanelCssVars } from './CollapsiblePanelCssVars.js';
/**
* A panel with the collapsible contents.
* Renders a `<div>` element.
*
* Documentation: [Base UI Collapsible](https://base-ui.com/react/components/collapsible)
*/
const 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
useEnhancedEffect(() => {
if (hiddenUntilFoundProp && keepMountedProp === false) {
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
} = useCollapsibleRootContext();
useEnhancedEffect(() => {
if (idProp) {
setPanelId(idProp);
}
}, [idProp, setPanelId]);
const hiddenUntilFound = hiddenUntilFoundProp ?? false;
const {
getRootProps,
height,
width,
isOpen
} = useCollapsiblePanel({
hiddenUntilFound,
panelId,
keepMounted: keepMountedProp ?? false,
mounted,
open,
ref: forwardedRef,
setPanelId,
setMounted,
setOpen
});
const {
renderElement
} = useComponentRenderer({
propGetter: getRootProps,
render: render ?? 'div',
state,
className,
extraProps: {
...otherProps,
style: {
...otherProps.style,
[CollapsiblePanelCssVars.collapsiblePanelHeight]: height ? `${height}px` : undefined,
[CollapsiblePanelCssVars.collapsiblePanelWidth]: width ? `${width}px` : undefined
}
},
customStyleHookMapping: collapsibleStyleHookMapping
});
if (!keepMountedProp && !isOpen && !hiddenUntilFound) {
return null;
}
return renderElement();
});
export { CollapsiblePanel };
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.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]),
/**
* 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.bool,
/**
* @ignore
*/
id: PropTypes.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.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.oneOfType([PropTypes.element, PropTypes.func]),
/**
* @ignore
*/
style: PropTypes.object
} : void 0;