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.
Note
lightning-accordion doesn’t support lazy loading. The content you load in inactive sections also impact your page load time.
Design
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.
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.
1<template>2 <lightning-accordion active-section-name="B">3 <lightning-accordion-section name="A" label="Accordion Title A"4 >This is the content area for section A</lightning-accordion-section5 >6 <lightning-accordion-section name="B" label="Accordion Title B"7 >This is the content area for section B</lightning-accordion-section8 >9 <lightning-accordion-section name="C" label="Accordion Title C"10 >This is the content area for section C</lightning-accordion-section11 >12 </lightning-accordion>13</template>
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.
Tip
Initialize the Accordion with Multiple Active Sections
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.
1<template>2 <lightning-accordion3 allow-multiple-sections-open4 active-section-name={activeSections}5 >6 <lightning-accordion-section name="A" label="Accordion Title A">7 <p>This is the content area for section A.</p>8 </lightning-accordion-section>910 <lightning-accordion-section name="B" label="Accordion Title B">11 <p>This is the content area for section B.</p>12 </lightning-accordion-section>1314 <lightning-accordion-section name="C" label="Accordion Title C">15 <p>This is the content area for section C.</p>16 </lightning-accordion-section>17 </lightning-accordion>18</template>
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.
1<template>2 <p>{activeSectionsMessage}</p>34 <lightning-accordion5 allow-multiple-sections-open6 onsectiontoggle={handleSectionToggle}7 active-section-name="A"8 >9 <lightning-accordion-section name="A" label="Accordion Title A">10 <p>This is the content area for section A.</p>11 </lightning-accordion-section>1213 <lightning-accordion-section name="B" label="Accordion Title B">14 <p>This is the content area for section B.</p>15 </lightning-accordion-section>1617 <lightning-accordion-section name="C" label="Accordion Title C">18 <p>This is the content area for section C.</p>19 </lightning-accordion-section>20 </lightning-accordion>21</template>
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-open attribute, openSections returns an array. For example: ['A'] for an open accordion section and ['A', 'B'] for multiple open accordion sections. An accordion section without a name value or a name value that contains an empty string is excluded from the openSections array. If only this accordion section is opened, openSections returns an empty array and openSections.length returns 0. If none of the accordion sections include a name value, openSections returns an empty array and openSections.length returns 0 when any number of accordion sections are opened.
If you don’t include the allow-multiple-sections-open attribute, openSections returns a string. For example: A for an open accordion section. In this case, only one section can be open at a time. If an accordion section without a name attribute or a name value that contains an empty string is opened, openSections returns undefined.
Expand Sections Programmatically
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.
To toggle the visibility of an accordion section, use the if:true directive.
1<template>2 <lightning-button onclick={handleToggleSection} label="Toggle Section B"></lightning-button>34 <lightning-accordion class="example-accordion" active-section-name="A">5 <lightning-accordion-section name="A" label="Accordion Title A">6 <p>This is the content area for section A.</p>7 </lightning-accordion-section>89 <template if:true={isDVisible}>10 <lightning-accordion-section name="B" label="Accordion Title B">11 <p>This is the content area for section B.</p>12 </lightning-accordion-section>13 </template>14 </lightning-accordion>15</template>
Component Styling
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.
1<lightning-accordion active-section-name="A">2 <lightning-accordion-section name="A" label="Accordion Title A">3 <div class="slds-box slds-theme_shade">4 <p>This is the content area for section A.</p>5 </div>6 </lightning-accordion-section>7<!-- More accordion sections here -->8</lightning-accordion>
Styling Hooks
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.
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-accordion has role="list".
When you set active-section-name to expand specific sections, the button on those section headers uses aria-expanded set to true.
An accordion section sets aria-hidden to true if the section is collapsed, which hides the content from assistive technologies.
Custom Events
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.
Attributes
Name
Description
Type
Default
Required
active-section-name
Expands the specified accordion sections. Pass in a string for a single section or a list of section names. Section names are case-sensitive. To support multiple expanded sections, include allow-multiple-sections-open in your markup. By default, only the first section in the accordion is expanded.
array|string
allow-multiple-sections-open
If present, the accordion allows multiple open sections. Otherwise, opening a section closes another that's currently open.
boolean
false
title
Displays tooltip text when the mouse moves over the element.