sp-accordion-item

open true false
allowMultiple true false
density Compact Spacious Default
size Small Medium Large Extra large
Overview API

Overview

Section titled Overview

The <sp-accordion-item> element represents a single item in an <sp-accordion> parent element. Its label attribute and default slot content make up the "headline" and "body" of the toggleable content item.

Usage

Section titled Usage

See it on NPM! How big is this package in your project?

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

Section titled 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

Section titled States

Opened

Section titled 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

Section titled 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

Section titled Behaviors

Events

Section titled 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

Section titled Accessibility

The accordion component provides proper ARIA attributes and keyboard navigation:

  • Each accordion item header has aria-expanded to indicate its current state
  • The header button has aria-controls pointing to the content region
  • The content region has role="region" and aria-labelledby pointing to the header
  • Disabled items have aria-disabled="true" applied
  • The accordion supports keyboard navigation with Enter/Space for activation

Include descriptive labels

Section titled Include descriptive labels

Each accordion item should have a clear, descriptive label that indicates what content will be revealed when expanded.

Use appropriate content

Section titled 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

Section titled Attributes and Properties
Property Attribute Type Default Description disabled disabled boolean false Disable this control. It will not receive focus or events label label string '' open open boolean false tabIndex tabIndex number The tab index to apply to this control. See general documentation about the tabindex HTML property

Slots

Section titled Slots
Name Description default slot The content of the item that is hidden when the item is not open

Events

Section titled Events
Name Type Description sp-accordion-item-toggle CustomEvent Announce that an accordion item has been toggled while allowing the event to be cancelled.