Slab

Disclosure

Accordion

A group of Disclosures where opening one can close the others. Great for FAQs.

Element
<slab-accordion>
Built on
group of <details>
As Markdown
accordion.md

Example

Here's a styled version, <slab-styled-accordion>. It's not in the package — it's an example of what you can build with the headless element. Copy it and make it yours at the bottom of this page. Switch to "As it ships" to see the same markup with the headless element and no CSS at all. That's your starting point.

Styled example As it ships
Concrete is strong when it's squeezed and weak when it's stretched. The steel takes the stretching. Concrete cures by reacting with water, not by drying. A slab that dries too soon is weaker and cracks. Most crews wait three to seven days, until it's strong enough to carry the walls.
HTML
<slab-styled-accordion exclusive>
  <slab-styled-disclosure label="Why rebar?" open>
    Concrete is strong when it's squeezed and weak when it's stretched. The
    steel takes the stretching.
  </slab-styled-disclosure>
  <slab-styled-disclosure label="Why keep it wet?">
    Concrete cures by reacting with water, not by drying. A slab that dries
    too soon is weaker and cracks.
  </slab-styled-disclosure>
  <slab-styled-disclosure label="When can framing start?">
    Most crews wait three to seven days, until it's strong enough to carry
    the walls.
  </slab-styled-disclosure>
</slab-styled-accordion>
Concrete is strong when it's squeezed and weak when it's stretched. The steel takes the stretching. Concrete cures by reacting with water, not by drying. A slab that dries too soon is weaker and cracks. Most crews wait three to seven days, until it's strong enough to carry the walls.
HTML
<slab-accordion exclusive>
  <slab-disclosure label="Why rebar?" open>
    Concrete is strong when it's squeezed and weak when it's stretched. The
    steel takes the stretching.
  </slab-disclosure>
  <slab-disclosure label="Why keep it wet?">
    Concrete cures by reacting with water, not by drying. A slab that dries
    too soon is weaker and cracks.
  </slab-disclosure>
  <slab-disclosure label="When can framing start?">
    Most crews wait three to seven days, until it's strong enough to carry
    the walls.
  </slab-disclosure>
</slab-accordion>

Usage

Install
pnpm add slab-elements lit
HTML
<script type="module">
  import 'slab-elements/accordion.js';
</script>

<slab-accordion exclusive>
  <slab-disclosure label="Shipping">…</slab-disclosure>
  <slab-disclosure label="Returns">…</slab-disclosure>
</slab-accordion>

Each item is a Disclosure, documented over there.

Keyboard

Key What it does
Tab Moves to the next summary, through any open content before it.
Enter, Space Opens or closes the focused Disclosure.

Behavior

A group of Disclosures with no visual styles, where opening one can close the others.

A name would do that for <details> in the same tree, but each Disclosure's is in a shadow root of its own. So Accordion does it instead, from the "toggle" each Disclosure dispatches. Disclosures are found by class rather than by tag name, so your subclass of Disclosure can be named anything.

Disclosures are independent unless exclusive is set. When it is and more than one is open, the first one open is kept, the way a <details> inserted open beside one of the same name is closed. So a Disclosure added later, open, is closed if one is already open.

There are no arrow keys. The APG makes them optional for an accordion, and each summary is already in the Tab order, with its content between it and the next. Arrows would be a second, less familiar way to do the same thing.

API

<slab-accordion>

Attributes
Name Type Default Description
exclusive boolean false Whether opening a Disclosure closes the others.
version string
Slots
Name Type Description
(default) Disclosure

Copy and own it

<slab-styled-accordion> isn't in the package. It's an example — copy it into your project and change whatever you want. Under the hood, it's just the headless element, subclassed, with styles set. That's it. Keyboard, focus, ARIA, forms, and events all come from the headless element, and the styles only reach in through ::part() and :state(), same as yours would.

To use it, copy these files, swap the tag in @customElement() for one of your own, and import it. tokens.styles.ts holds the colors and fonts every styled example shares, so you only need it once.

accordion.ts
import { customElement } from 'lit/decorators.js';
import Accordion from 'slab-elements/accordion.js';
import styles from './accordion.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-accordion': StyledAccordion;
  }
}

/**
 * Accordion, styled: a heavy ink rule over its Disclosures, which rule
 * themselves off from each other.
 */
@customElement('slab-styled-accordion')
export default class StyledAccordion extends Accordion {
  static override styles = [tokens, styles];
}
accordion.styles.ts
import { css } from 'lit';

export default css`
  :host {
    border-block-start: 4px solid var(--ink);
  }
`;
tokens.styles.ts
import { css } from 'lit';

/*
  The styled components' palette and type, taken from the logo: ink for the
  courses, ember for the base, paper behind them.

  Each is a short alias for a "--slab-styled-*" property, with the default as
  its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
  which every shadow root inherits. Were the defaults declared under those
  names here instead, each host's own declaration would shadow the page's.

  Each default is "light-dark()", so the components follow the page's
  "color-scheme": "dark" for dark, "light" for light, and "light dark" to
  follow the operating system. Ink and paper trade places in the dark, so
  ink is always the text and paper always what's behind it. Contrast is given
  as light, then dark.
*/
export default css`
  :host {
    /* Secondary text. 6.8:1 and 6.9:1 on paper. */
    --ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
    --border: 2px solid var(--ink);

    /* Behind disabled and read-only controls, and hovered rows. */
    --chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));

    /*
      The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
      enough for borders, fills and large text, but not always for small text,
      which takes "--ember-deep".
    */
    --ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));

    /*
      Orange for small text on paper, and for paper text on it. 5.7:1 and
      7.4:1.
    */
    --ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
    --font-body: var(
      --slab-styled-font-body,
      'IBM Plex Sans',
      system-ui,
      sans-serif
    );
    --font-display: var(
      --slab-styled-font-display,
      'Zilla Slab',
      rockwell,
      'Roboto Slab',
      serif
    );
    --font-mono: var(
      --slab-styled-font-mono,
      'IBM Plex Mono',
      ui-monospace,
      monospace
    );
    --ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
    --paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));

    /* The hard, offset shadow a raised surface casts. */
    --shadow: 4px 4px 0 var(--ink);
  }
`;