-
Notifications
You must be signed in to change notification settings - Fork 4.8k
@wordpress/ui: add Collapsible component #76280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
436fd97
Add Collapsible component to @wordpress/ui
ciampo fcce537
Add Storybook stories for Collapsible
ciampo 87ddb77
Add unit tests for Collapsible
ciampo bddef13
Update CHANGELOG for Collapsible component
ciampo a863533
Address review feedback
ciampo d21d88c
Update CollapsibleCard to use internal Collapsible wrapper
ciampo 3fb13ed
Apply suggestion from @Copilot
ciampo 85cd618
Add comment explaining aria-expanded tradeoff in header render prop
ciampo fa6df0c
avoid unnecessary destructuring
ciampo b9d7fcc
add hidden until found example
ciampo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { Panel } from './panel'; | ||
| import { Root } from './root'; | ||
| import { Trigger } from './trigger'; | ||
|
|
||
| export { Root, Trigger, Panel }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Collapsible as _Collapsible } from '@base-ui/react/collapsible'; | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import type { PanelProps } from './types'; | ||
|
|
||
| /** | ||
| * A panel with the collapsible contents. Hidden when collapsed, visible | ||
| * when expanded. | ||
| * | ||
| * `Collapsible` is a collection of React components that combine to render | ||
| * a collapsible panel controlled by a button. | ||
| */ | ||
| export const Panel = forwardRef< HTMLDivElement, PanelProps >( | ||
| function CollapsiblePanel( props, forwardedRef ) { | ||
| return <_Collapsible.Panel ref={ forwardedRef } { ...props } />; | ||
| } | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Collapsible as _Collapsible } from '@base-ui/react/collapsible'; | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import type { RootProps } from './types'; | ||
|
|
||
| /** | ||
| * Groups all parts of the collapsible. | ||
| * | ||
| * `Collapsible` is a collection of React components that combine to render | ||
| * a collapsible panel controlled by a button. | ||
| */ | ||
| export const Root = forwardRef< HTMLDivElement, RootProps >( | ||
| function CollapsibleRoot( props, forwardedRef ) { | ||
| return <_Collapsible.Root ref={ forwardedRef } { ...props } />; | ||
| } | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { useState } from '@wordpress/element'; | ||
| import * as Collapsible from '../index'; | ||
|
|
||
| const meta: Meta< typeof Collapsible.Root > = { | ||
| title: 'Design System/Components/Collapsible', | ||
| component: Collapsible.Root, | ||
| subcomponents: { | ||
| 'Collapsible.Trigger': Collapsible.Trigger, | ||
| 'Collapsible.Panel': Collapsible.Panel, | ||
| }, | ||
| }; | ||
| export default meta; | ||
|
|
||
| type Story = StoryObj< typeof Collapsible.Root >; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| children: ( | ||
| <> | ||
| <Collapsible.Trigger>Toggle</Collapsible.Trigger> | ||
| <Collapsible.Panel> | ||
| <p>Collapsible content here.</p> | ||
| </Collapsible.Panel> | ||
| </> | ||
| ), | ||
| }, | ||
| }; | ||
|
|
||
| export const DefaultOpen: Story = { | ||
| argTypes: { open: { control: false } }, | ||
| args: { | ||
| defaultOpen: true, | ||
| children: ( | ||
| <> | ||
| <Collapsible.Trigger>Toggle</Collapsible.Trigger> | ||
| <Collapsible.Panel> | ||
| <p>This panel is open by default.</p> | ||
| </Collapsible.Panel> | ||
| </> | ||
| ), | ||
| }, | ||
| }; | ||
|
|
||
| export const Disabled: Story = { | ||
| args: { | ||
| disabled: true, | ||
| children: ( | ||
| <> | ||
| <Collapsible.Trigger>Toggle (disabled)</Collapsible.Trigger> | ||
| <Collapsible.Panel> | ||
| <p>This content cannot be toggled.</p> | ||
| </Collapsible.Panel> | ||
| </> | ||
| ), | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * When `hiddenUntilFound` is set on `Collapsible.Panel`, the collapsed content | ||
| * remains in the DOM using the `hidden="until-found"` HTML attribute instead of | ||
| * being removed entirely. This lets the browser's native page search (Ctrl/Cmd+F) | ||
| * find text inside collapsed panels and automatically expand them to reveal the | ||
| * match — improving discoverability without sacrificing the collapsed layout. | ||
| */ | ||
| export const HiddenUntilFound: Story = { | ||
| render: function HiddenUntilFound() { | ||
| return ( | ||
| <div> | ||
| <p> | ||
| Use the browser's find-in-page (Ctrl/Cmd+F) to search | ||
| for "hidden treasure". The collapsed panel will | ||
| automatically expand to reveal the match. | ||
| </p> | ||
| <Collapsible.Root> | ||
| <Collapsible.Trigger>Expand to reveal</Collapsible.Trigger> | ||
| <Collapsible.Panel hiddenUntilFound> | ||
| <p> | ||
| This is the hidden treasure that can be found via | ||
| the browser's built-in page search even while | ||
| the panel is collapsed. | ||
| </p> | ||
| </Collapsible.Panel> | ||
| </Collapsible.Root> | ||
| </div> | ||
| ); | ||
| }, | ||
| }; | ||
|
|
||
| export const Controlled: Story = { | ||
| argTypes: { | ||
| open: { control: false }, | ||
| defaultOpen: { control: false }, | ||
| }, | ||
| render: function Controlled() { | ||
| const [ open, setOpen ] = useState( false ); | ||
| return ( | ||
| <Collapsible.Root open={ open } onOpenChange={ setOpen }> | ||
| <Collapsible.Trigger> | ||
| { open ? 'Close' : 'Open' } | ||
| </Collapsible.Trigger> | ||
| <Collapsible.Panel> | ||
| <p>Controlled collapsible panel.</p> | ||
| </Collapsible.Panel> | ||
| </Collapsible.Root> | ||
| ); | ||
| }, | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point I think it's definitely cleaner to use a single svg and rotate it in CSS based on the
[aria-expanded]. What do you think?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, it's the approach that I've taken in #76265 anyway. I may keep the code in this PR as-is and apply that change in #76265