Accordion

Answers that unfold as cloth.

Billing with Egoist is simple and transparent. Choose a plan that fits your needs, add your payment method, and you're ready to go.

Track your usage, view payment history, download invoices, and manage your subscription, all from one place.

Yes. Cancel from your account settings in two clicks. You keep access until the end of your current billing period, and nothing renews after that.

If something isn't right within the first 14 days, reach out and we'll refund you in full, no questions, no forms.

Every plan includes collaborators. Invite teammates by email and choose whether they can edit, comment, or only view.

Seats are billed per month, prorated to the day.

All data is encrypted in transit and at rest. We never sell your data, and you can export or delete everything whenever you like.

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/accordion.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/accordion.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Accordion as AccordionEngine } from "@/lib/fabricui/accordion-engine";
import "./accordion.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

export type AccordionProps = Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue"> & {
  /** The open item's index, -1 for none (controlled). Leave out to let it keep its own. */
  value?: number;
  /** The item open at first when uncontrolled (-1: none). */
  defaultValue?: number;
  /** Called with the open item's index (-1: none) when one is clicked open or shut. */
  onValueChange?: (value: number) => void;
components/fabricui/accordion.css
/*
 * accordion.css — Accordion (accordion.js). Moved out of accordion.html.
 */
/*
 * Accordion. Each item is the calendar's trigger + panel pair: a #191919
 * pill question with an edge glow, and an answer panel in #202020 tucked
 * up behind it. Answers drop open as cloth (cloth-drop.js, in-flow, so the
 * items below move with the falling fabric) and collapse plainly.
 */
.frame__slot:where(.fui-accordion, .fui-accordion *) { width: 320px; height: 420px; }

.acc:where(.fui-accordion, .fui-accordion *) {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
lib/fabricui/accordion-engine.js
import { ClothDrop } from "./cloth-drop";

/*
 * accordion-engine.js — Accordion (accordion.css). Moved out of accordion.html so it can be used anywhere.
 */

const CHEVRON = `<svg class="icon acc__chevron" viewBox="0 0 24 24" aria-hidden="true"><path d="M18 9.00005C18 9.00005 13.5811 15 12 15C10.4188 15 6 9 6 9" /></svg>`;

/**
 * Accordion: one item open at a time (opening one collapses the other),
 * clicking the open one closes it. Arrow keys move between questions.
 */
export class Accordion {
  /**
   * @param {HTMLElement} root
   * @param {({ q: string, a: string[] } | HTMLElement)[]} items  questions to build, or `.acc__item`s already in root (rendered by accordion.tsx)

Usage

Usage
import { Accordion, AccordionItem } from "@/components/fabricui/accordion";

export function Faq() {
  return (
    <Accordion defaultValue={0}>
      <AccordionItem title="How does billing work?">
        <p>You're billed monthly from the day you start.</p>
      </AccordionItem>
      <AccordionItem title="Can I cancel my plan anytime?">
        <p>Yes. Your plan stays active until the end of the period.</p>
      </AccordionItem>
    </Accordion>
  );
}

Props

PropTypeDefault
children

The items. One is open at a time.

AccordionItem[]—
value

The open item's index, -1 for none, if you control it.

number—
defaultValue

The item open at first when you don't (-1: none).

number0
onValueChange

Called with the open item's index (-1: none) when one is clicked open or shut.

(value: number) => void—
AccordionItem title

The question: the item's trigger.

ReactNode—
AccordionItem children

The answer: paragraphs (<p>) or anything else.

ReactNode—