sp-accordion-item
NPM 1.9.0
View Storybook
Try it on Stackblitz
Overview
The <sp-accordion-item> element represents a single item in an <sp-accordion>label attribute and default slot content make up the "headline" and "body" of the toggleable content item.
Usage
yarn add @spectrum-web-components/accordion
Import the side effectful registration of <sp-accordion-item> via:
import '@spectrum-web-components/accordion/sp-accordion-item.js';
When looking to leverage the AccordionItem base class as a type and/or for extension purposes, do so via:
import { AccordionItem } from '@spectrum-web-components/accordion';
Anatomy
The accordion item consists of two key parts:
- A header button with a label and chevron icon
- Content that is revealed when the item is expanded
<sp-accordion> <sp-accordion-item label="Accordion Item"> <div>Accordion item content can be toggled visible.</div> </sp-accordion-item> </sp-accordion>
States
Opened
An accordion item can be opened by default using the open attribute:
<sp-accordion allow-multiple> <sp-accordion-item open label="Opened Accordion Item"> <div>This accordion item content is visible by default.</div> </sp-accordion-item> </sp-accordion>
Disabled
Individual accordion items can be disabled using the disabled attribute. Disabled items cannot be expanded or collapsed:
<sp-accordion allow-multiple> <sp-accordion-item disabled label="Disabled Accordion Item"> <div>You can not toggle this content visible.</div> </sp-accordion-item> </sp-accordion>
Behaviors
Events
An <sp-accordion-item> element will dispatch sp-accordion-item-toggle events when it is opened or closed. By default, these events are dispatched to allow the parent <sp-accordion> to manage which of its item children are expanded at any given time. Consumers can also listen for this event and check event.target.open to determine whether the item is currently expanded.
Accessibility
The accordion component provides proper ARIA attributes and keyboard navigation:
- Each accordion item header has
aria-expandedto indicate its current state - The header button has
aria-controlspointing to the content region - The content region has
role="region"andaria-labelledbypointing to the header - Disabled items have
aria-disabled="true"applied - The accordion supports keyboard navigation with Enter/Space for activation
Include descriptive labels
Each accordion item should have a clear, descriptive label that indicates what content will be revealed when expanded.
Use appropriate content
Accordion content should be related to the header label and provide additional information or functionality that users can access when needed.
API
Attributes and Properties
disabled disabled boolean false label label string '' open open boolean false tabIndex tabIndex number Slots
default slot Events
sp-accordion-item-toggle CustomEvent Announce that an accordion item has been toggled while allowing the event to be cancelled.