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 element to enable the collapse functionality. Click the
buttons below to show and hide another element:
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
Accessibility
Be sure to add aria-expanded
to the control element. Providing [attr.aria-expanded]="!collapseElement.collapsed"
attribute
will assure dynamic change to aria-expanded="false"
when an element is collapsed, and aria-expanded="true"
when it's expanded.
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 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
Inputs
Name | Type | Default | Description |
---|---|---|---|
collapsed |
Boolean | true |
Changes default collapsed state |
Outputs
Name | Type | Description |
---|---|---|
collapseShow |
EventEmitter<MdbCollapseDirective> | Fires when show animation starts. |
collapseShown |
EventEmitter<MdbCollapseDirective> | Fires when show animation ends. |
collapseHide |
EventEmitter<MdbCollapseDirective> | Fires when hide animation starts. |
collapseHidden |
EventEmitter<MdbCollapseDirective> | 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()
|