@base-ui/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.
111 lines (110 loc) • 3.44 kB
JavaScript
'use client';
import * as React from 'react';
import { useIsoLayoutEffect } from '@base-ui/utils/useIsoLayoutEffect';
import { warn } from '@base-ui/utils/warn';
import { resolveStyle } from "../../utils/resolveStyle.js";
import { useRenderElement } from "../../internals/useRenderElement.js";
import { useCollapsibleRootContext } from "../root/CollapsibleRootContext.js";
import { collapsibleStateAttributesMapping } from "../root/stateAttributesMapping.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)
*/
export const CollapsiblePanel = /*#__PURE__*/React.forwardRef(function CollapsiblePanel(componentProps, forwardedRef) {
const {
className,
hiddenUntilFound: hiddenUntilFoundProp,
keepMounted: keepMountedProp,
render,
id: idProp,
style,
...elementProps
} = componentProps;
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useIsoLayoutEffect(() => {
if (hiddenUntilFoundProp && keepMountedProp === false) {
warn('The `keepMounted={false}` prop on `Collapsible.Panel` is ignored when `hiddenUntilFound` is enabled, since the panel must remain mounted while closed.');
}
}, [hiddenUntilFoundProp, keepMountedProp]);
}
const {
mounted,
onOpenChange,
open,
panelId,
setMounted,
setPanelIdState,
setOpen,
state,
transitionStatus
} = useCollapsibleRootContext();
const hiddenUntilFound = hiddenUntilFoundProp ?? false;
const keepMounted = keepMountedProp ?? false;
useIsoLayoutEffect(() => {
if (idProp) {
setPanelIdState(idProp);
return () => {
setPanelIdState(undefined);
};
}
return undefined;
}, [idProp, setPanelIdState]);
const {
height,
props,
ref,
shouldPreventOpenAnimation,
shouldRender,
transitionStatus: panelTransitionStatus,
width
} = useCollapsiblePanel({
externalRef: forwardedRef,
hiddenUntilFound,
id: panelId,
keepMounted,
mounted,
onOpenChange,
open,
setMounted,
setOpen,
transitionStatus
});
const panelState = {
...state,
transitionStatus: panelTransitionStatus
};
const resolvedStyle = resolveStyle(style, panelState);
const element = useRenderElement('div', {
...componentProps,
style: undefined
}, {
state: panelState,
ref,
props: [props, {
style: {
[CollapsiblePanelCssVars.collapsiblePanelHeight]: height === undefined ? 'auto' : `${height}px`,
[CollapsiblePanelCssVars.collapsiblePanelWidth]: width === undefined ? 'auto' : `${width}px`
}
}, elementProps, resolvedStyle ? {
style: resolvedStyle
} : undefined,
// Resolve the public `style` prop so temporary `animationName: 'none'`
// can still win after user's inline styles have been merged.
shouldPreventOpenAnimation ? {
style: {
animationName: 'none'
}
} : undefined],
stateAttributesMapping: collapsibleStateAttributesMapping
});
if (!shouldRender) {
return null;
}
return element;
});
if (process.env.NODE_ENV !== "production") CollapsiblePanel.displayName = "CollapsiblePanel";