A panel to collapse and expand your content.
The component can be used in a controlled or uncontrolled manner. Please refer to the React documentation if you are unsure what either or entails.
Please have a look at the Component Best Practices to ensure that you are using the component as intended from a development and UX perspective.
yarn add @commercetools-uikit/collapsible-panel
npm --save install @commercetools-uikit/collapsible-panel
Additionally install the peer dependencies (if not present)
yarn add react
npm --save install react
import CollapsiblePanel from '@commercetools-uikit/collapsible-panel';
import Spacings from '@commercetools-uikit/spacings';
import Text from '@commercetools-uikit/text';
const Example = () => (
<Spacings.Stack>
{/* 1. Uncontrolled. The `CollapsiblePanel` controls its own state. You do not pass handlers or state related props. */}
<CollapsiblePanel header="Lorem">
<Text.Detail>Hello World</Text.Detail>
</CollapsiblePanel>;{/* 2. Controlled. You control the `CollapsiblePanel`. You do pass handlers or state related props.
Assume the parent rendering the `CollapsiblePanel` has `isPanelOpen` as state and a `togglePanel` as a handler. */}
<CollapsiblePanel
isClosed={this.state.isPanelOpen}
onToggle={this.togglePanel}
header="Lorem"
>
<Text.Detail>Hello World</Text.Detail>
</CollapsiblePanel>
;
</Spacings.Stack>
);
export default Example;
Props | Type | Required | Default | Description |
---|---|---|---|---|
id |
string |
An unique id for the panel header, which will also be used to generate a prefixed id for the panel content section.
Read about getPanelContentId below for more about this. |
||
header |
ReactNode |
✅ | The title being rendered at top left of the panel | |
secondaryHeader |
ReactNode |
A secondary header for the panel (only pass if needed) | ||
description |
string |
If passed will be shown below the title as more information regarding the panel | ||
className |
string |
Allow to override the styles by passing a className prop.
Custom styles can also be passed using the css prop from emotion. |
||
isSticky |
boolean |
Makes the panel's header sticky in regards to the page's scroll | ||
headerControls |
ReactNode |
Controls at the top right part of the panel | ||
isDisabled |
boolean |
false |
Disables the panel and all interactions with it | |
children |
ReactNode |
The actual content rendered inside the panel | ||
tone |
union Possible values: 'urgent' , 'primary'
|
Indicates the color scheme of the panel. | ||
theme |
union Possible values: 'dark' , 'light'
|
'dark' |
Determines the background color of the panel. | |
condensed |
boolean |
false |
Whenever true the headers and content itself
will consume less space in that to the borders are smaller and everything has less padding |
|
hideExpansionControls |
boolean |
Controls the visibility of the expansion controls on the left | ||
headerControlsAlignment |
union Possible values: 'left' , 'right'
|
'right' |
Indicates the position of the control elements in the header component. | |
isDefaultClosed |
boolean |
Indicates if the panel's content should be collapsed or shown by default.
Updates to this value are not respected. Only used for uncontrolled mode (when no onToggle is passed.) |
||
isClosed |
boolean |
Indicates if the panel's content should be collapsed or shown.
Component becomes *controlled when this is passed. |
||
onToggle |
Function See signature. |
function to be triggered whenever the user clicks the top area to collapse the panel's content
Becomes required when isClosed is passed. |
||
horizontalConstraint |
union Possible values: , 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto'
|
'scale' |
Horizontal size limit of the panel. |
() => void
Whenever some content on a given page should be concealed and/or expandible. This often holds true for instance for detailed but hidden information of some entity to save space.
Header
The header of this component should follow our design and UX conventions so it looks the same across the application. It accepts a node
to accompany the two scenarios:
- Given the
CollapsiblePanel
is condensed
Whenever the component is configured to be condensed
all content pased via the header
will be wrapped in a <Text.Detail />
. For this use case please ensure that no combinations of components are passed via the header
.
import CollapsiblePanel from '@commercetools-uikit/collapsible-panel';
<CollapsiblePanel condensed={true} header={'My title'}>
<YourComponentAsContentOfThePanel />
</CollapsiblePanel>;
- Given the
CollapsiblePanel
is not condensed
For this use case you will need to explicitly pass the header
wrapped by the <CollapsiblePanel.Header />
component to automatically ensure it follows our designs (enforced by the <CollapsiblePanel.Header />
).
import CollapsiblePanel from '@commercetools-uikit/collapsible-panel';
<CollapsiblePanel
header={
<CollapsiblePanel.Header>
<FormattedMessage {...messages.title} />
</CollapsiblePanel.Header>
}
>
<YourComponentAsContentOfThePanel />
</CollapsiblePanel>;
- Passing a combination of components (e.g. label and input)
Rendering any input(s) inside the header
is allowed, but there are certain contraints that need to be taken into account:
- You can only render a maximum amount of 3 components inside the
header
. - Given a combination of label and input
- Then the input's label should be bold and the input only a
<TextInput />
- Then the input's label should be bold and the input only a
- All has to be vertically aligned in the
header
container.
Theme
The component supports themes (dark and light) to enable nesting of collapsible panels inside one another.
- Given the background of the parent is
dark
- Then the child needs to be
light
for better accessibility and visibility
- Then the child needs to be
Condensed
There are a some restrictions given using condensed={true}
- Nothing complex is passed as the
header
- Nor button or inputs or anything that is not within title itself
- The title should be only (if possible) be a string or a combination or text components
- Everything is automatically wrapped within a
<Text.Detail />
SecondaryHeader
The secondaryHeader
allow rendering some secondary information in addition to the title.
It accepts a node
but with few restrictions:
-
Bold text is allowed while the text component is
<Text.Detail />
- Text size should not exceed the font size of
<Text.Detail />
headerControls
The headerControls
allows rendering a component which should only contain buttons or links. Apart from that there is one more limitation:
- You can only render a maximum amount of 3 button inside the
headerControls
Returns the generated id
used for the wrapper of the panel content section. It is used for setting the aria-controls
attribute on the header and can also be useful for finding the child element when testing.
CollapsiblePanel.getPanelContentId('example'); // -> panel-content-example