Collapse

Angular 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

Add mdbCollapse to the elment to enable the collapse funcionality. Click the buttons below to show and hide another element:

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.

          <!-- Buttons trigger collapse -->
          <a
            class="btn btn-primary"
            (click)="basicCollapse.toggle()"
            role="button"
            aria-expanded="false"
            aria-controls="collapseExample"
          >
            Link with href
          </a>
          <button
            class="btn btn-primary"
            type="button"
            (click)="basicCollapse.toggle()"
            aria-expanded="false"
            aria-controls="collapseExample"
          >
            Button
          </button>

          <!--Collapsed content-->
          <div class="mt-3" id="collapseExample" mdbCollapse #basicCollapse="mdbCollapse">
            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.
          </div>
        

Multiple targets

A <button> or <a> can show and hide multiple elements by referencing them with a selector in its href or data-mdb-target attribute. Multiple <button> or <a> can show and hide an element if they each reference it with their href or data-mdb-target attribute

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.

          <!-- Buttons trigger collapse -->
          <a
            class="btn btn-primary"
            role="button"
            (click)="linkCollapse.toggle()"
            aria-expanded="false"
            aria-controls="multiCollapseExample1"
          >
            Toggle first element
          </a>
          <button
            class="btn btn-primary"
            type="button"
            (click)="buttonCollapse.toggle()"
            aria-expanded="false"
            aria-controls="multiCollapseExample2"
          >
            Toggle second element
          </button>
          <button
            class="btn btn-primary"
            type="button"
            (click)="linkCollapse.toggle(); buttonCollapse.toggle()"
            aria-expanded="false"
            aria-controls="multiCollapseExample1 multiCollapseExample2"
          >
            Toggle both elements
          </button>

          <!-- Collapsed content -->
          <div class="row">
            <div class="col">
              <div
                class="collapse multi-collapse mt-3"
                id="multiCollapseExample1"
                mdbCollapse
                #linkCollapse="mdbCollapse"
              >
                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.
              </div>
            </div>
            <div class="col">
              <div
                class="collapse multi-collapse mt-3"
                id="multiCollapseExample2"
                mdbCollapse
                #buttonCollapse="mdbCollapse"
              >
                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.
              </div>
            </div>
          </div>
        

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.

If your control element is targeting a single collapsible element – i.e. the data-mdb-target attribute is pointing to an id selector – you should add the aria-controls attribute to the control element, containing the id of the collapsible element. Modern screen readers and similar assistive technologies make use of this attribute to provide users with additional shortcuts to navigate directly to the collapsible element itself.

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 { MdbCollapseModule } from 'mdb-angular-ui-kit/collapse';
          …
          @NgModule ({
            ...
            imports: [MdbCollapseModule],
            ...
          })
        

Inputs

Name Type Default Description
collapsed Boolean true Changes default collapsed state

Outputs

Name Description
collapseShow Fires when show animation starts.
collapseShown Fires when show animation ends.
collapseHide Fires when hide animation starts.
collapseHidden Fires when hide animation ends.

Methods

Name Description Example
hide Manually closes a collapse collapse.hide()
show Manually opens a collapse collapse.show()
toggle Manually toggle a collapse collapse.toggle()

            <button
              class="btn btn-primary"
              type="button"
              (click)="collapse.toggle()"
              aria-expanded="false"
              aria-controls="collapseExample"
            >
              Button
            </button>

            <!--Collapsed content-->
            <div class="mt-3" id="collapseExample" mdbCollapse #collapse="mdbCollapse">
              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.
            </div>
          

            @ViewChild('collapse') collapse: MdbCollapseDirective;

            ngAfterViewInit(): void {
              this.collapse.toggle();
            }