Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/reference/generated/accordion-panel.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"data-disabled": {
"description": "Present when the accordion item is disabled."
},
"data-hidden": {
"description": "Present when the panel is hidden."
},
"data-index": {
"description": "Indicates the index of the accordion item.",
"type": "number"
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/generated/collapsible-panel.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"data-closed": {
"description": "Present when the collapsible panel is closed."
},
"data-hidden": {
"description": "Present when the panel is hidden."
},
"data-starting-style": {
"description": "Present when the panel is animating in."
},
Expand Down
19 changes: 18 additions & 1 deletion packages/react/src/accordion/panel/AccordionPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ import { AccordionPanelCssVars } from './AccordionPanelCssVars';
import { useOpenChangeComplete } from '../../utils/useOpenChangeComplete';
import { useRenderElement } from '../../utils/useRenderElement';
import type { TransitionStatus } from '../../utils/useTransitionStatus';
import type { StateAttributesMapping } from '../../utils/getStateAttributesProps';
import { AccordionPanelDataAttributes } from './AccordionPanelDataAttributes';

const stateAttributesMapping: StateAttributesMapping<AccordionPanel.State> = {
...accordionStateAttributesMapping,
open: (open) => {
const hidden = !open;
const itemMapping = accordionStateAttributesMapping.open?.(open) ?? {};
if (hidden) {
return {
...itemMapping,
[AccordionPanelDataAttributes.hidden]: '',
};
}
return itemMapping;
},
};

/**
* A collapsible panel with the accordion item contents.
Expand Down Expand Up @@ -153,7 +170,7 @@ export const AccordionPanel = React.forwardRef(function AccordionPanel(
},
elementProps,
],
stateAttributesMapping: accordionStateAttributesMapping,
stateAttributesMapping,
});

const shouldRender = keepMounted || hiddenUntilFound || (!keepMounted && mounted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export enum AccordionPanelDataAttributes {
* Present when the panel is animating out.
*/
endingStyle = TransitionStatusDataAttributes.endingStyle,
/**
* Present when the panel is hidden.
*/
hidden = 'data-hidden',
}
10 changes: 8 additions & 2 deletions packages/react/src/collapsible/panel/CollapsiblePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import { BaseUIComponentProps } from '../../utils/types';
import { useRenderElement } from '../../utils/useRenderElement';
import { useCollapsibleRootContext } from '../root/CollapsibleRootContext';
import type { CollapsibleRoot } from '../root/CollapsibleRoot';
import { collapsibleStateAttributesMapping } from '../root/stateAttributesMapping';
import { transitionStatusMapping } from '../../utils/stateAttributesMapping';
import { panelOpenStateMapping } from '../../utils/collapsibleOpenStateMapping';
import { useCollapsiblePanel } from './useCollapsiblePanel';
import { CollapsiblePanelCssVars } from './CollapsiblePanelCssVars';
import { useOpenChangeComplete } from '../../utils/useOpenChangeComplete';
import type { TransitionStatus } from '../../utils/useTransitionStatus';
import type { StateAttributesMapping } from '../../utils/getStateAttributesProps';

const stateAttributesMapping: StateAttributesMapping<CollapsiblePanel.State> = {
...panelOpenStateMapping,
...transitionStatusMapping,
Copy link
Member

@mj12albert mj12albert Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transitionStatusMapping can be added directly to collapsibleOpenStateMapping since it's the same for both panel components

};
/**
* A panel with the collapsible contents.
* Renders a `<div>` element.
Expand Down Expand Up @@ -144,7 +150,7 @@ export const CollapsiblePanel = React.forwardRef(function CollapsiblePanel(
},
elementProps,
],
stateAttributesMapping: collapsibleStateAttributesMapping,
stateAttributesMapping,
});

const shouldRender = keepMounted || hiddenUntilFound || (!keepMounted && mounted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export enum CollapsiblePanelDataAttributes {
* Present when the panel is animating out.
*/
endingStyle = TransitionStatusDataAttributes.endingStyle,
/**
* Present when the panel is hidden.
*/
hidden = 'data-hidden'
}
18 changes: 18 additions & 0 deletions packages/react/src/utils/collapsibleOpenStateMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ export const collapsibleOpenStateMapping = {
} satisfies StateAttributesMapping<{
open: boolean;
}>;

export const panelOpenStateMapping = {
open: (open) => {
const panelHidden = !open;
const collapsibleStateMapping = collapsibleOpenStateMapping.open(open);

if (panelHidden) {
return {
...collapsibleStateMapping,
[CollapsiblePanelDataAttributes.hidden]: '',
};
}

return collapsibleStateMapping;
},
} satisfies StateAttributesMapping<{
open: boolean;
}>;
Loading