Skip to content

Conversation

@BrandonGoren
Copy link

@BrandonGoren BrandonGoren commented Oct 24, 2025

Adds a data-hidden attribute to collapsible and accordion when they are not open to match the behavior in tabs (which sets this attribute).

Attached is a screenshot from an accordion component where data-hidden is now applied!

image

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 24, 2025

vite-css-base-ui-example

pnpm add https://pkg.pr.new/mui/base-ui/@base-ui-components/react@3063
pnpm add https://pkg.pr.new/mui/base-ui/@base-ui-components/utils@3063

commit: 8bc13d3

@netlify
Copy link

netlify bot commented Oct 24, 2025

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 8bc13d3
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/68fd51dcc754220008a25554
😎 Deploy Preview https://deploy-preview-3063--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@mj12albert
Copy link
Member

Maybe data-hidden is redundant? The hidden attr can be styled directly and we are already doing so:

@BrandonGoren
Copy link
Author

Maybe data-hidden is redundant? The hidden attr can be styled directly and we are already doing so:

When using Tailwind, passing a display class (like block or flex) to the collapsible body will override the built-in hidden attribute, since Tailwind’s utility classes take precedence in the final CSS.

By adding a data-hidden attribute, we can increase CSS specificity and ensure the “hidden” state is correctly applied, even when other display utilities are present.

@BrandonGoren
Copy link
Author

BrandonGoren commented Oct 24, 2025

You can technically still style based on the hidden attribute in tailwind (and that's what I'm doing now), but it's a lot less intuitive:

[&[hidden]]:hidden

vs.

data-[hidden]:hidden

@mj12albert mj12albert added component: collapsible Changes related to the collapsible component. component: accordion Changes related to the accordion component. labels Oct 24, 2025
@mj12albert
Copy link
Member

in tailwind (and that's what I'm doing now), but it's a lot less intuitive

👍 makes sense ~ we have [&[hidden]:not([hidden='until-found'])]:hidden in our demo as well

Comment on lines 127 to 130
hidden: !open,
transitionStatus,
}),
[state, transitionStatus],
[state, transitionStatus, open],
Copy link
Member

Choose a reason for hiding this comment

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

open is already in the ...state, we don't need to modify the state to add a new data attr based on it, instead modify the state<>data attr mapping here:

export const collapsibleOpenStateMapping = {
open(value) {
if (value) {
return PANEL_OPEN_HOOK;
}
return PANEL_CLOSED_HOOK;
},
} satisfies StateAttributesMapping<{
open: boolean;
}>;

@BrandonGoren
Copy link
Author

@mj12albert I updated the PR to update the data attribute states in the common functions.

There are some different patterns in the repo for assembling these state attributes. I tried to match each collapsible/accordion to other parts of their "component" (e.g. the triggers and the roots), but it still felt a little awkward to merge these with the "parent" data-attributes in this way - let me know what you think!

@BrandonGoren BrandonGoren force-pushed the add-data-hidden-attribute-to-collapsible-and-accordion branch from 676cd67 to 8bc13d3 Compare October 25, 2025 22:40

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

Copy link
Member

@mj12albert mj12albert left a comment

Choose a reason for hiding this comment

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

@BrandonGoren I just remembered that internally hidden does not 100% correspond to open:

const hidden = React.useMemo(() => {
if (animationTypeRef.current === 'css-animation') {
return !visible;
}
return !open && !mounted;
}, [open, mounted, visible, animationTypeRef]);

Instead I think props.hidden that useCollapsiblePanel returns needs to be added to the state in both panel components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: accordion Changes related to the accordion component. component: collapsible Changes related to the collapsible component.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants