Accordion
lightning-accordion
A collection of vertically stacked sections with multiple content areas.
For Use In
Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A lightning-accordion component shows vertically stacked sections of content that you can expand and collapse. Click a section's header to expand its content. Users can control how much content is visible without scrolling as much to see the content of a page.
To create an accordion section, nest a lightning-accordion-section component within lightning-accordion. Each lightning-accordion-section can contain HTML markup or Lightning components.
By default, you can open only one section at a time. Close a section by opening another section. You can configure the accordion to allow multiple sections to be open, and then click the section headers to open and close the section.
You can specify one or more active sections. Active sections are expanded when the component loads. This documentation uses the terms "active", "expanded", and "open" interchangeably.
lightning-accordion doesn't support lazy loading. The content you load in inactive sections also impact your page load time.
lightning-accordion implements the accordion blueprint in the Salesforce Lightning Design System (SLDS). The accordion adapts to SLDS 1 or SLDS 2 styling based on the org's theme or the container app that you use.
| SLDS 1 | SLDS 2 | |
|---|---|---|
| Design | Accordion | Accordion |
| For Use In | Lightning Experience, Experience Builder sites, Salesforce mobile app, Lightning Out (Beta), Standalone Lightning app, Mobile Offline | Lightning Experience |
The first accordion section is expanded by default. To change the default, provide a section name by using the active-section-name attribute. The section name is case-sensitive.
This example creates a basic accordion with three sections, where section B is expanded by default. Only one accordion section is expanded at a time.
If two or more sections use the same name and that name is also specified as the active-section-name, the first section with that name is expanded by default.
To view interactive examples, see the Examples tab.
To specify multiple expanded sections, include allow-multiple-sections-open in your markup and pass in an array of accordion section names to active-section-name.
If you don't pass in a section name, all sections are closed by default.
In your JavaScript code, define the sections you want to expand.
When the open sections change, use the onsectiontoggle handler to find out which sections are active.
This example shows the name of the active section.
Use the detail.openSections property to return the active section names.
The openSections event parameter returns a string or an array of strings depending on the allow-multiple-sections-open attribute.
- If you include the
allow-multiple-sections-openattribute,openSectionsreturns an array. For example:['A']for an open accordion section and['A', 'B']for multiple open accordion sections. An accordion section without anamevalue or anamevalue that contains an empty string is excluded from theopenSectionsarray. If only this accordion section is opened,openSectionsreturns an empty array andopenSections.lengthreturns 0. If none of the accordion sections include anamevalue,openSectionsreturns an empty array andopenSections.lengthreturns 0 when any number of accordion sections are opened. - If you don't include the
allow-multiple-sections-openattribute,openSectionsreturns a string. For example:Afor an open accordion section. In this case, only one section can be open at a time. If an accordion section without anameattribute or anamevalue that contains an empty string is opened,openSectionsreturns undefined.
To expand a section, pass in the section name by using active-section-name.
This example expands section B when the Expand Section B button is pressed.
The onclick handler on the button sets the active section name, which opens the section programmatically.
Set the value for the open section by using the openSections property in the onsectiontoggle handler.
To expand multiple sections programmatically, include allow-multiple-sections-open in your markup and pass in an array of section names instead.
To toggle the visibility of an accordion section, use the if:true directive.
lightning-accordion adapts to SLDS 1 and SLDS 2 styling.
To apply additional styling, use the SLDS utility classes with the class attribute.
This example adds a border and gray background to the accordion content area by using SLDS classes.
Component styling hooks provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.
For more information, see Style Components Using Lightning Design System Styling Hooks in the Lightning Web Components Developer Guide.
You can't initialize an accordion with multiple sections by using only markup. Pass the array of section names using JavaScript, as described in Initialize the Accordion with Multiple Active Sections.
Accordion sections aren’t lazy loaded. Content in sections that are collapsed initially are available on load in the DOM.
This component has usage differences from its Aura counterpart. See Base Components: Aura Vs Lightning Web Components in the Lightning Web Components Developer Guide.
lightning-accordion implements the SLDS accordion blueprint, which follows the WAI-ARIA Authoring Practices for the accordion pattern. In addition to the keyboard interaction guidelines on the WAI-ARIA site, you can use the Left Arrow and Right Arrow key to move focus from an accordion header. The Home and End keys aren’t used for keyboard interaction on lightning-accordion.
lightning-accordion observes these roles, states, and properties.
lightning-accordionhasrole="list".- When you set
active-section-nameto expand specific sections, the button on those section headers usesaria-expandedset totrue. - An accordion section sets
aria-hiddentotrueif the section is collapsed, which hides the content from assistive technologies.
sectiontoggle
The event that's fired when an accordion loads with at least one active section or when a section is toggled.
The sectiontoggle event returns this parameter.
| Parameter | Type | Description |
|---|---|---|
| openSections | object | The name of the active section. Returns an array of strings for the active section names. |
The event properties are as follows.
| Property | Value | Description |
|---|---|---|
| bubbles | false | This event doesn’t bubble. |
| cancelable | false | This event has no default behavior that can be canceled. You can't call preventDefault() on this event. |
| composed | false | This event doesn’t propagate outside the template in which it was dispatched. |