Morphos
Layout

Accordion

A vertically stacked set of collapsible sections that reveal content on demand.

Interactive example

Installation

npm install @morphos/layout
pnpm add @morphos/layout
yarn add @morphos/layout
bun add @morphos/layout

Import

import { Accordion, AccordionItem, AccordionTrigger, AccordionContent } from '@morphos/layout'

Usage

@Component()
class MyComponent extends StatefulComponent {
  @State() accordion = new Accordion({ type: 'single', collapsible: true })

  render() {
    return (
      <Accordion accordion={this.accordion}>
        <AccordionItem accordion={this.accordion} value="item-1">
          <AccordionTrigger accordion={this.accordion} item="item-1">
            What is Morphos?
          </AccordionTrigger>
          <AccordionContent accordion={this.accordion} item="item-1">
            Morphos is a headless PraxisJS component library.
          </AccordionContent>
        </AccordionItem>
        <AccordionItem accordion={this.accordion} value="item-2">
          <AccordionTrigger accordion={this.accordion} item="item-2">
            Is it accessible?
          </AccordionTrigger>
          <AccordionContent accordion={this.accordion} item="item-2">
            Yes — it follows WAI-ARIA authoring practices.
          </AccordionContent>
        </AccordionItem>
      </Accordion>
    )
  }
}

Set type="multiple" on the Accordion instance to allow any number of items open simultaneously instead of only one.

Compound components

ComponentDescription
AccordionRoot state manager — tracks which item(s) are expanded
AccordionItemA single collapsible section
AccordionTriggerThe button that toggles an item open or closed
AccordionContentThe content revealed when an item is expanded

Props — Accordion

PropTypeDefaultDescription
type"single" | "multiple""single"Whether one or many items can be open at once
valuestring | string[]Controlled expanded item(s)
defaultValuestring | string[]Initially expanded item(s) (uncontrolled)
onValueChange(value: string | string[]) => voidCalled when expanded items change
collapsiblebooleantrueWhether an open item can be collapsed by clicking it again (only meaningful for type="single")
classstringCSS class
idstringHTML id

Methods — Accordion

MethodDescription
isOpen(item: string)Returns whether the given item value is currently expanded
toggle(item: string)Opens or closes the given item, following the type/collapsible rules

Props — AccordionItem

PropTypeDefaultDescription
accordionAccordionThe Accordion state instance
valuestringUnique identifier for this item
disabledbooleanMarks the item as disabled (exposed via data-disabled; does not itself block interaction)
classstringCSS class
idstringHTML id

Props — AccordionTrigger

PropTypeDefaultDescription
accordionAccordionThe Accordion state instance
itemstringThe value of the parent AccordionItem
childrenChildrenTrigger label
classstringCSS class
idstringHTML id (defaults to an auto-generated id)

Props — AccordionContent

PropTypeDefaultDescription
accordionAccordionThe Accordion state instance
itemstringThe value of the parent AccordionItem
childrenChildrenContent to reveal
classstringCSS class
idstringHTML id

data-* attributes

AttributeElementWhen present
data-typeAccordionAlways — reflects the type prop ("single" or "multiple")
data-expandedAccordionItem, AccordionTrigger, AccordionContentThe item is expanded
data-disabledAccordionItemThe item's disabled prop is set

AccordionTrigger handles Enter and Space to toggle the item, in addition to onClick. Arrow-key navigation between triggers is not built in — each trigger is a regular tab-stop.

Styling example

.morphos-accordion-trigger[data-expanded]::after {
  transform: rotate(-135deg);
}

.morphos-accordion-item:last-child {
  border-bottom: none;
}

On this page