Use Accordion...
- To present a large amount of content in a compact form.
- To break down information into manageable sections, especially if the content is categorized.
- To prevent information overload by allowing users to expand only the sections they are interested in.
- For FAQs sections.
Don't use Accordion...
- To display critical information. Use a Modal Dialog instead.
- To present a small amount of content where the benefit of collapsing/expanding is minimal. Use a Card instead.
- To navigate through the page. Use Tabs instead.
Multiple
In the React API, by default multiple Collapsibles can be expanded simultaneously.
In the Vanilla API, add the swan-accordion-multiple
class to the element that has the swan-accordion
class to enable this functionality.
The preview has been updated.
<Accordion><BasicCollapsible collapsibleId="coll-one" heading="Summary 1">Content 1.</BasicCollapsible><BasicCollapsible collapsibleId="coll-two" heading="Summary 2">Content 2.</BasicCollapsible></Accordion>
<div class="swan-accordion swan-accordion-multiple"><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="content-1">Summary 1</button></div><div class="swan-collapsible-content" id="content-1">Content 1.</div></div><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="content-2">Summary 2</button></div><div class="swan-collapsible-content" id="content-2">Content 2.</div></div></div>
Single
In the React API, use single
to ensure that only one Collapsible is expanded at any given time.
In the Vanilla API, by default only one Collapsible can be expanded at any given time.
The preview has been updated.
<Accordion single><BasicCollapsible collapsibleId="coll-one" heading="Summary 1">Content 1.</BasicCollapsible><BasicCollapsible collapsibleId="coll-two" heading="Summary 2">Content 2.</BasicCollapsible></Accordion>
<div class="swan-accordion"><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="content-1">Summary 1</button></div><div class="swan-collapsible-content" id="content-1">Content 1.</div></div><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="content-2">Summary 2</button></div><div class="swan-collapsible-content" id="content-2">Content 2.</div></div></div>
Accordions as List
Use the following HTML tags for when the Accordion
is semantically a list:
- In the React API, use
purpose="list"
on theAccordion
. - In the Vanilla API, use a
ul
tag for the Accordion, andli
tags for each Collapsible within it.
The preview has been updated.
<Accordion single><BasicCollapsible collapsibleId="coll-one" heading="Summary 1">Content 1.</BasicCollapsible><BasicCollapsible collapsibleId="coll-two" heading="Summary 2">Content 2.</BasicCollapsible></Accordion>
<ol class="swan-accordion"><li aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="collapsible-1">Summary 1</button></div><div class="swan-collapsible-content" id="collapsible-1">Content 1.</div></li><li aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="collapsible-2">Summary 2</button></div><div class="swan-collapsible-content" id="collapsible-2">Content 2.</div></li></ol>
Full bleed
Use fullBleed
to remove the right padding of the content within every Collapsible component of the Accordion.
The preview has been updated.
<Accordion fullBleed><BasicCollapsible collapsibleId="coll-one" heading="Summary 1">Content 1 using full bleed. This collapsible's content will go all the wayout to the right edge. This collapsible's content will go all the way out tothe right edge. This collapsible's content will go all the way out to theright edge. This collapsible's content will go all the way out to the rightedge.</BasicCollapsible><BasicCollapsible collapsibleId="coll-two" heading="Summary 2">Content 2 using full bleed. This collapsible's content will go all the wayout to the right edge. This collapsible's content will go all the way out tothe right edge. This collapsible's content will go all the way out to theright edge. This collapsible's content will go all the way out to the rightedge.</BasicCollapsible></Accordion>
<div class="swan-accordion swan-accordion-full-bleed"><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="content-1">Summary 1</button></div><div class="swan-collapsible-content" id="content-1">Content 1 using full bleed. This collapsible's content will go all the wayout to the right edge. This collapsible's content will go all the way outto the right edge. This collapsible's content will go all the way out tothe right edge. This collapsible's content will go all the way out to theright edge.</div></div><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="content-2">Summary 2</button></div><div class="swan-collapsible-content" id="content-2">Content 2 using full bleed. This collapsible's content will go all the wayout to the right edge. This collapsible's content will go all the way outto the right edge. This collapsible's content will go all the way out tothe right edge. This collapsible's content will go all the way out to theright edge.</div></div></div>
Color Swatches
Use skin="color-swatches"
to wrap a large set of Color Swatches to avoid making the content excessively tall.
The CollapsibleSummaryButton
shows the total number of available swatches.
The preview has been updated.
<GridContainer><Row><Column span={3}><Accordion skin="color-swatches"><Collapsible collapsibleId="color-swatches-one"><CollapsibleSummary><CollapsibleSummaryButton>10<VisuallyHidden>color choices</VisuallyHidden></CollapsibleSummaryButton></CollapsibleSummary><CollapsibleContent><StandardTileSwatches><SelectionSet data-testid="set1" defaultSelectedValue="indigo"><ColorSwatches><SelectionSetInput value="red/black"><SelectionSetLabel><ColorSwatchprimaryColor="red"secondaryColor="black"accessibleText="red/black"/></SelectionSetLabel></SelectionSetInput><SelectionSetInput value="indigo"><SelectionSetLabel><ColorSwatchprimaryColor="indigo"accessibleText="indigo"/></SelectionSetLabel></SelectionSetInput><SelectionSetInput value="green"><SelectionSetLabel><ColorSwatchprimaryColor="green"accessibleText="green"/></SelectionSetLabel></SelectionSetInput><SelectionSetInput value="gold"><SelectionSetLabel><ColorSwatch primaryColor="gold" accessibleText="gold" /></SelectionSetLabel></SelectionSetInput><SelectionSetInput value="blue"><SelectionSetLabel><ColorSwatch primaryColor="blue" accessibleText="blue" /></SelectionSetLabel></SelectionSetInput><SelectionSetInput value="orange"><SelectionSetLabel><ColorSwatchprimaryColor="orange"accessibleText="orange"/></SelectionSetLabel></SelectionSetInput><SelectionSetInput value="pink"><SelectionSetLabel><ColorSwatch primaryColor="pink" accessibleText="pink" /></SelectionSetLabel></SelectionSetInput><SelectionSetInput value="brown"><SelectionSetLabel><ColorSwatchprimaryColor="brown"accessibleText="brown"/></SelectionSetLabel></SelectionSetInput><SelectionSetInput disabled value="limegreen"><SelectionSetLabel><ColorSwatchprimaryColor="limegreen"accessibleText="lime green"/></SelectionSetLabel></SelectionSetInput><SelectionSetInput disabled value="darkmagenta"><SelectionSetLabel><ColorSwatchprimaryColor="darkmagenta"accessibleText="dark magenta"/></SelectionSetLabel></SelectionSetInput></ColorSwatches></SelectionSet></StandardTileSwatches></CollapsibleContent></Collapsible></Accordion></Column></Row></GridContainer>
<div class="swan-grid-container"><div class="swan-row"><div class="swan-col-3"><div class="swan-accordion swan-accordion-skin-color-swatches"><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="auto-id-ha6e6oguq-124">10 <span class="swan-visually-hidden">color choices</span></button></div><div class="swan-collapsible-content" id="auto-id-ha6e6oguq-124"><div class="swan-standard-tile-swatches"><divdata-testid="set1"role="radiogroup"class="swan-selection-set"><div class="swan-color-swatches"><inputtype="radio"class="swan-selection-set-input swan-radio"value="red/black"id="auto-id-ha6e6oguq-113"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-113"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: red; color: black;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color red/black</span></span></label><inputtype="radio"class="swan-selection-set-input swan-radio"value="indigo"checked=""id="auto-id-ha6e6oguq-114"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-114"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: indigo;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color indigo</span></span></label><inputtype="radio"class="swan-selection-set-input swan-radio"value="green"id="auto-id-ha6e6oguq-115"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-115"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: green;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color green</span></span></label><inputtype="radio"class="swan-selection-set-input swan-radio"value="gold"id="auto-id-ha6e6oguq-116"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-116"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: gold;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color gold</span></span></label><inputtype="radio"class="swan-selection-set-input swan-radio"value="blue"id="auto-id-ha6e6oguq-117"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-117"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: blue;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color blue</span></span></label><inputtype="radio"class="swan-selection-set-input swan-radio"value="orange"id="auto-id-ha6e6oguq-118"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-118"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: orange;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color orange</span></span></label><inputtype="radio"class="swan-selection-set-input swan-radio"value="pink"id="auto-id-ha6e6oguq-119"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-119"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: pink;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color pink</span></span></label><inputtype="radio"class="swan-selection-set-input swan-radio"value="brown"id="auto-id-ha6e6oguq-120"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-120"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: brown;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color brown</span></span></label><inputtype="radio"disabled=""class="swan-selection-set-input swan-radio"value="limegreen"id="auto-id-ha6e6oguq-121"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-121"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: limegreen;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color lime green</span></span></label><inputtype="radio"disabled=""class="swan-selection-set-input swan-radio"value="darkmagenta"id="auto-id-ha6e6oguq-122"name="auto-id-ha6e6oguq-123"/><labelclass="swan-selection-set-label"for="auto-id-ha6e6oguq-122"><span class="swan-color-swatch-wrapper"><divaria-disabled="false"role="presentation"class="swan-color-swatch"style="background-color: darkmagenta;"></div><spanclass="swan-color-swatch-accessible-label swan-visually-hidden">color dark magenta</span></span></label></div></div></div></div></div></div></div></div></div>
Default expanded collapsibles
Use defaultExpandedCollapsibles
to present certain Collapsibles as expanded by default.
The preview has been updated.
<Accordion defaultExpandedCollapsibles={{ 'coll-two': true }}><BasicCollapsible collapsibleId="coll-one" heading="Summary 1">Content 1.</BasicCollapsible><BasicCollapsible collapsibleId="coll-two" heading="Summary 2">Content 2.</BasicCollapsible></Accordion>
<div class="swan-accordion swan-accordion-multiple"><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="true"class="swan-collapsible-summary-button"aria-controls="content-1">Summary 1</button></div><divclass="swan-collapsible-content swan-collapsible-content-open"id="content-1">Content 1.</div></div><div aria-disabled="false" class="swan-collapsible"><div class="swan-collapsible-summary"><buttonaria-expanded="false"class="swan-collapsible-summary-button"aria-controls="content-2">Summary 2</button></div><div class="swan-collapsible-content" id="content-2">Content 2.</div></div></div>
Managing state
Controlling the state of an Accordion
is only necessary if there's a need to read or write its state from another component. This requirement is rather uncommon.
In the React API, use expandedCollapsibles
on the Accordion
to map the collapsibleId
s to a boolean, indicating whether each Collapsible should be expanded or not.
If provided, expandedCollapsibles
is treated as the source of truth, and overrides the single prop.
Use the onRequestExpandedChange
prop on the Accordion
to be notified when a Collapsible
wants to expand or collapse. This handler is invoked with two arguments:
collapsibleId
: to identify theCollapsible
changing its state.expanded
: to indicate whether theCollapsible
seeks to be expanded or not.
The preview has been updated.
() => {const [expandedCollapsibles, setExpandedCollapsibles] = React.useState({'coll-one': false,'coll-two': true,})return (<AccordionexpandedCollapsibles={expandedCollapsibles}onRequestExpandedChange={(collapsibleId, expanded) => {setExpandedCollapsibles(prevValue => ({...prevValue,[collapsibleId]: expanded,}))// or if you want to emulate "single" behavior// setExpandedCollapsibles({ [collapsibleId]: expanded })}}><BasicCollapsible collapsibleId="coll-one" heading="Summary 1">Content 1.</BasicCollapsible><BasicCollapsible collapsibleId="coll-two" heading="Summary 2">Content 2.</BasicCollapsible></Accordion>)}