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
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.
        
            
            import React, { useState } from 'react';
            import { MDBCollapse, MDBBtn } from 'mdb-react-ui-kit';
            
            export default function App() {
              const [isOpen, setIsOpen] = useState(false);

              const toggleOpen = () => setIsOpen(!isOpen);

              return (
                <>
                  <MDBBtn tag='a' onClick={toggleOpen}>
                    Link
                  </MDBBtn>
                  <MDBBtn onClick={toggleOpen}>Button</MDBBtn>
                  <MDBCollapse open={isOpen}>
                    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.

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.
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.
        
            
            import React, { useState } from 'react';
            import { MDBCollapse, MDBBtn, MDBRow, MDBCol } from 'mdb-react-ui-kit';
            
            export default function App() {
              const [showFirst, setShowFirst] = useState(false);
              const [showSecond, setShowSecond] = useState(false);
            
              const toggleFirst = () => setShowFirst(!showFirst);
              const toggleSecond = () => setShowSecond(!showSecond);

              const toggleBoth = () => {
                setShowFirst(!showFirst);
                setShowSecond(!showSecond);
              };
              return (
                <>
                  <MDBBtn onClick={toggleFirst}> Toggle first element</MDBBtn>
                  <MDBBtn onClick={toggleSecond}>Toggle second element</MDBBtn>
                  <MDBBtn onClick={toggleBoth}> Toggle both elements</MDBBtn>
      
                  <MDBRow>
                    <MDBCol>
                      <MDBCollapse open={showFirst} 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 open={showSecond} 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 />
collapseRef RejObject<HTMLElement> undefined Reference to MDBCollapse element. <MDBCollapse collapseRef={exampleRef} />
navbar Boolean false This prop is using in an MDBNavbar component to change default class 'collapse' to 'navbar-collapse'. <MDBCollapse navbar />
open Boolean | string false Change visible of content inside MDBCollapse <MDBCollapse open={true} />
tag string 'div' Defines tag of the MDBCollapse element <MDBCollapse tag='span' />

Events

Name Type Description
onClose () => any A callback fired when the Collapse requests to be closed.
onOpen () => any A callback fired when the Collapse requests to be opened.

SCSS variables

        
            
        $transition-collapse: height .35s ease;
        $transition-collapse-width: width .35s ease;