Collapse
React Bootstrap 5 Collapse component
Toggle the visibility of content across your project with a few classes and our JavaScript plugins.
Note: Read the API tab to find all available options and advanced customization
How it works
The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used
as triggers that are mapped to specific elements you toggle. Collapsing an element will
animate the height
from its current value to 0
. Given how CSS
handles animations, you cannot use padding
on a .collapse
element.
Instead, use the class as an independent wrapping element.
Basic example
Click the buttons below to show and hide another element via class changes:
.collapse
hides content.collapsing
is applied during transitions.collapse.show
shows content
import React, { useState } from 'react';
import { MDBCollapse, MDBBtn } from 'mdb-react-ui-kit';
export default function App() {
const [showShow, setShowShow] = useState(false);
const toggleShow = () => setShowShow(!showShow);
return (
<>
<MDBBtn tag='a' onClick={toggleShow}>
Link
</MDBBtn>
<MDBBtn onClick={toggleShow}>Button</MDBBtn>
<MDBCollapse show={showShow}>
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim
keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
</MDBCollapse>
</>
);
}
Multiple targets
You can show and hide multiple elements with one button or multiple buttons. You have to change state to render component with another value.
import React, { useState } from 'react';
import { MDBCollapse, MDBBtn, MDBRow, MDBCol } from 'mdb-react-ui-kit';
export default function App() {
const [showFirstElement, setShowFirstElement] = useState(false);
const [showSecondElement, setShowSecondElement] = useState(false);
const toggleFirstElement = () => setShowFirstElement(!showFirstElement);
const toggleSecondElement = () => setShowSecondElement(!showSecondElement);
const toggleBothElements = () => {
setShowFirstElement(!showFirstElement);
setShowSecondElement(!showSecondElement);
};
return (
<>
<MDBBtn onClick={toggleFirstElement}> Toggle first element</MDBBtn>
<MDBBtn onClick={toggleSecondElement}>Toggle second element</MDBBtn>
<MDBBtn onClick={toggleBothElements}> Toggle both elements</MDBBtn>
<MDBRow>
<MDBCol>
<MDBCollapse show={showFirstElement} className='mt-3'>
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil
anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
</MDBCollapse>
</MDBCol>
<MDBCol>
<MDBCollapse show={showSecondElement} className='mt-3'>
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil
anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
</MDBCollapse>
</MDBCol>
</MDBRow>
</>
);
}
Accessibility
Be sure to add aria-expanded
to the control element. This attribute explicitly
conveys the current state of the collapsible element tied to the control to screen readers and
similar assistive technologies. If the collapsible element is closed by default, the attribute
on the control element should have a value of aria-expanded="false"
. If you’ve
set the collapsible element to be open by default using the show
class, set
aria-expanded="true"
on the control instead. The plugin will automatically toggle
this attribute on the control based on whether or not the collapsible element has been opened
or closed (via JavaScript, or because the user triggered another control element also tied to
the same collapsible element). If the control element’s HTML element is not a button (e.g., an
<a>
or <div>
), the attribute
role="button"
should be added to the element.
Note that Bootstrap’s current implementation does not cover the various keyboard interactions described in the WAI-ARIA Authoring Practices 1.1 accordion pattern - you will need to include these yourself with custom JavaScript.
Collapse - API
Import
import { MDBCollapse } from 'mdb-react-ui-kit';
Properties
MDBCollapse
Name | Type | Default | Description | Example |
---|---|---|---|---|
center
|
Boolean | false |
Center elements in MDBCollapse. |
<MDBCollapse center />
|
className
|
String | '' |
Add custom class to MDBCollapse |
<MDBCollapse className="class" />
|
show
|
Boolean | false |
Change visible of content inside MDBCollapse |
<MDBCollapse show={yourState} />
|
navbar
|
Boolean | false |
This prop is using in an MDBNavbar component to change default class 'collapse' to 'navbar-collapse'. |
<MDBCollapse navbar />
|