@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.
141 lines (139 loc) • 4.84 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 '../../collapsible/root/CollapsibleRootContext.js';
import { useCollapsiblePanel } from '../../collapsible/panel/useCollapsiblePanel.js';
import { useAccordionRootContext } from '../root/AccordionRootContext.js';
import { useAccordionItemContext } from '../item/AccordionItemContext.js';
import { accordionStyleHookMapping } from '../item/styleHooks.js';
import { AccordionPanelCssVars } from './AccordionPanelCssVars.js';
/**
* A collapsible panel with the accordion item contents.
* Renders a `<div>` element.
*
* Documentation: [Base UI Accordion](https://base-ui.com/react/components/accordion)
*/
const AccordionPanel = /*#__PURE__*/React.forwardRef(function AccordionPanel(props, forwardedRef) {
const {
className,
hiddenUntilFound: hiddenUntilFoundProp,
keepMounted: keepMountedProp,
id: idProp,
render,
style: styleProp,
...otherProps
} = props;
const {
hiddenUntilFound: contextHiddenUntilFound,
keepMounted: contextKeepMounted
} = useAccordionRootContext();
const hiddenUntilFound = hiddenUntilFoundProp ?? contextHiddenUntilFound;
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useEnhancedEffect(() => {
if (keepMountedProp === false && hiddenUntilFound) {
warn('The `keepMounted={false}` prop on a Accordion.Panel will be ignored when using `contextHiddenUntilFound` on the Panel or the Root since it requires the panel to remain mounted when closed.');
}
}, [hiddenUntilFound, keepMountedProp]);
}
const {
mounted,
open,
panelId,
setPanelId,
setMounted,
setOpen
} = useCollapsibleRootContext();
const keepMounted = keepMountedProp ?? contextKeepMounted;
const {
getRootProps,
height,
width,
isOpen
} = useCollapsiblePanel({
hiddenUntilFound,
panelId: idProp ?? panelId,
keepMounted,
mounted,
open,
ref: forwardedRef,
setPanelId,
setMounted,
setOpen
});
const {
state,
triggerId
} = useAccordionItemContext();
const {
renderElement
} = useComponentRenderer({
propGetter: getRootProps,
render: render ?? 'div',
state,
className,
extraProps: {
...otherProps,
'aria-labelledby': triggerId,
role: 'region',
style: {
[AccordionPanelCssVars.accordionPanelHeight]: height ? `${height}px` : undefined,
[AccordionPanelCssVars.accordionPanelWidth]: width ? `${width}px` : undefined,
...styleProp
}
},
customStyleHookMapping: accordionStyleHookMapping
});
if (!isOpen && !keepMounted && !hiddenUntilFound) {
return null;
}
return renderElement();
});
export { AccordionPanel };
process.env.NODE_ENV !== "production" ? AccordionPanel.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 closed.
* 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;