Layout

Accordion

Sections that expand and collapse, built on <details> and <summary>. Works without JavaScript; can keep a single section open.

JxAccordion import 'package:jx_cli/ui.dart' New in 0.0.3

Preview

Does it need JavaScript?
No. The browser opens and closes the sections, with the mouse or the keyboard.
Can several sections stay open?
Yes, unless the accordion is exclusive, like this one.
Is it searchable?
Closed sections are found by the find-in-page of the browser, which opens them.

When to use it

  • Frequently asked questions.
  • Optional settings hidden by default.
  • Long help text split in parts.

Minimal example

Dart
JxAccordion([
  JxAccordionItem(title: 'Shipping', children: [.text('2 to 4 days.')]),
  JxAccordionItem(title: 'Returns', children: [.text('Free for 30 days.')]),
])

Complete example

A full Jaspr component you can paste into a project.

Dart
import 'package:jaspr/jaspr.dart';
import 'package:jx_cli/ui.dart';

/// FAQ where opening a question closes the previous one.
class Faq extends StatelessComponent {
  const Faq(this.entries, {super.key});

  final List<(String question, String answer)> entries;

  @override
  Component build(BuildContext context) {
    return JxAccordion(
      name: 'faq',
      exclusive: true,
      [
        for (final (i, (question, answer)) in entries.indexed)
          JxAccordionItem(title: question, open: i == 0, children: [.text(answer)]),
      ],
    );
  }
}

Properties

Property Type Default Description
items List<JxAccordionItem> required The sections, in order.
exclusive bool false Opening a section closes the others.
name String? null Group name shared by the sections. Required when exclusive ; use one per accordion.
classes String? null Extra JX classes, appended after the component classes.

Related types

Name Role
JxAccordionItem(title, children, open: false) One section: header text, content, open on first render.

Customizing

  • Pass classes to add or override utilities: they come last, so classes: 'w:full' wins over the default width.
  • The chevron turns with group-open:rot:180: reuse group-open: for your own open styles.

Accessibility

  • Each header is a native <summary>: focusable, toggled with Enter and Space, announced as expanded or collapsed.
  • The chevron is decorative (aria-hidden).
  • The chevron rotation is disabled when the system asks for reduced motion.

Limitations

  • Exclusive mode relies on <details name>, supported by current browsers; older ones let several sections open.
  • The open and close are not animated: browsers do not animate <details> height yet.