Yes. Each trigger is a native button inside a heading, and the open panel is a region named by it.

Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/accordion.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Accordion, AccordionItem } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type AccordionParts = Component.Headless.Parts<typeof Accordion>
    type AccordionItemParts = Component.Headless.Parts<typeof AccordionItem>
    
    const variants = {
      default: "",
      card: "overflow-hidden rounded-lg border bg-card shadow-sm",
      separated: "grid gap-2"
    } as const
    
    export type AccordionVariant = keyof typeof variants
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    /** The `<div>` around the items. `variant` also restyles the items, triggers and content inside it. */
    export const AccordionRoot = <Msg>(
      h: Html.H<Msg>,
      accordion: AccordionParts,
      options: ClassOption & { readonly variant?: AccordionVariant | undefined } = {}
    ) =>
      accordion.root(
        h.div
          .class(cn("group/accordion w-full", variants[options.variant ?? "default"], options.class))
          .attr("data-variant", options.variant ?? "default")
      )
    
    /** The `<div>` around one item's header and panel. */
    export const AccordionItemRoot = <Msg>(h: Html.H<Msg>, item: AccordionItemParts, options: ClassOption = {}) =>
      item.root(
        h.div.class(cn(
          "border-b group-data-[variant=card]/accordion:last:border-b-0 " +
            "group-data-[variant=separated]/accordion:rounded-md group-data-[variant=separated]/accordion:border " +
            "group-data-[variant=separated]/accordion:bg-card group-data-[variant=separated]/accordion:transition-colors " +
            "group-data-[variant=separated]/accordion:duration-150 " +
            "group-data-[variant=separated]/accordion:data-[open]:border-brand " +
            "group-data-[variant=separated]/accordion:data-[open]:bg-brand-wash",
          options.class
        ))
      )
    
    /** The heading around the trigger. An `<h3>`: pass the trigger as its child. */
    export const AccordionHeader = <Msg>(h: Html.H<Msg>, item: AccordionItemParts, options: ClassOption = {}) =>
      item.header(h.h3.class(cn("m-0 flex", options.class)))
    
    /** The button that opens and closes the item, with a chevron drawn by `::after`. */
    export const AccordionTrigger = <Msg>(h: Html.H<Msg>, item: AccordionItemParts, options: ClassOption = {}) =>
      item.trigger(
        h.button.class(cn(
          "flex w-full flex-1 items-center gap-2.5 rounded-xs py-3.5 text-left text-sm font-medium text-foreground " +
            "underline-offset-4 hover:underline data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 " +
            "data-[disabled]:no-underline [&_svg]:size-4 [&_svg]:shrink-0 [&_svg]:text-muted-foreground " +
            "after:mr-1 after:mb-1 after:ml-auto after:size-[7px] after:shrink-0 after:rotate-45 after:border-r-[1.5px] " +
            "after:border-b-[1.5px] after:border-muted-foreground after:transition-transform after:duration-200 " +
            "data-[panel-open]:after:mb-0 data-[panel-open]:after:mt-1 data-[panel-open]:after:rotate-[225deg] " +
            "group-data-[variant=card]/accordion:rounded-none group-data-[variant=card]/accordion:px-4 " +
            "group-data-[variant=card]/accordion:-outline-offset-2 group-data-[variant=card]/accordion:hover:bg-muted " +
            "group-data-[variant=card]/accordion:hover:no-underline " +
            "group-data-[variant=separated]/accordion:rounded-md group-data-[variant=separated]/accordion:px-3.5 " +
            "group-data-[variant=separated]/accordion:py-3 group-data-[variant=separated]/accordion:hover:no-underline",
          options.class
        ))
      )
    
    /**
     * The panel, whose height animates from the kit's `--accordion-panel-height`.
     * `undefined` once it has closed, so chain with `?.children(...)`.
     */
    export const AccordionPanel = <Msg>(h: Html.H<Msg>, item: AccordionItemParts, options: ClassOption = {}) =>
      item.panel(
        h.div.class(cn(
          "h-[var(--accordion-panel-height)] overflow-hidden text-[13px] leading-relaxed text-muted-foreground " +
            "transition-[height] duration-200 ease-out data-[starting-style]:h-0 data-[ending-style]:h-0",
          options.class
        ))
      )
    
    /** The padding inside a panel. The panel itself has none, so it can shrink to nothing. */
    export const AccordionContent = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.div.class(cn(
        "pr-6 pb-3.5 [&_p]:m-0 [&_p+p]:mt-2 group-data-[variant=card]/accordion:px-4 " +
          "group-data-[variant=card]/accordion:pb-4 group-data-[variant=separated]/accordion:px-3.5",
        options.class
      ))
    
  3. Import it

    import { AccordionRoot, AccordionItemRoot, AccordionHeader, AccordionTrigger, AccordionPanel, AccordionContent } from "#ui/accordion"
    

Usage

Single.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Accordion, AccordionItem } from "@effect/ui-kit"
import { Schema } from "effect"
import {
  AccordionContent,
  AccordionHeader,
  AccordionItemRoot,
  AccordionPanel,
  AccordionRoot,
  AccordionTrigger
} from "#ui/accordion"

const AccordionFaqMessage = UiSchema.TaggedEnum({
  ToggledAccordion: { value: Schema.Array(Schema.String) }
})

const questions = [
  {
    id: "accessible",
    question: "Is it accessible?",
    answer: "Yes. Each trigger is a native button inside a heading, and the open panel is a region named by it."
  },
  {
    id: "styled",
    question: "Is it styled?",
    answer: "The kit is headless: it sets the ARIA and data attributes, and the helpers in ui/accordion.ts add the classes."
  },
  {
    id: "animated",
    question: "Is it animated?",
    answer: "Yes. The panel gets its content's height as a CSS variable, so a plain transition on height opens and closes it."
  }
]

export const AccordionFaq = Html.component({
  name: "AccordionFaq",
  model: Schema.Struct({ open: Schema.Array(Schema.String) }),
  message: AccordionFaqMessage
}).make({
  init: () => ({ open: ["accessible"] }),
  update: {
    ToggledAccordion: ($, { value }) => $.evolve({ open: value })
  },
  view: (h, model) =>
    Accordion(h, {
      props: { value: model.open },
      onEmit: { Toggled: ({ value }) => AccordionFaqMessage.ToggledAccordion({ value }) },
      view: (accordion) =>
        AccordionRoot(h, accordion, { class: "max-w-md" }).children(
          questions.map((entry) =>
            AccordionItem(h, {
              key: entry.id,
              props: { value: entry.id },
              view: (item) =>
                AccordionItemRoot(h, item).children(
                  AccordionHeader(h, item).children(AccordionTrigger(h, item).children(entry.question)),
                  AccordionPanel(h, item)?.children(AccordionContent(h).children(entry.answer))
                )
            })
          )
        )
    })
})

Examples

Multiple

With multiple, each item opens and closes on its own.

Orders leave the warehouse within one working day and arrive in 3 to 5.

Send anything back within 30 days. The label is in the box.

Open: shipping, returns

Disabled item

A disabled item's trigger is a disabled button and emits nothing.

One project, community support and 1 GB of storage.

Card

The same parts inside a bordered card, from the root's variant option.

Mentions and replies arrive by email.

Everything else waits in the weekly digest.

With icons

Separate items with a leading icon, tinted through data-open while open.

Add @effect/ui and @effect/ui-kit to the project.

Expand and collapse all

The open values live in the Model, so other controls can change them.

Scroll areas, meters and an accordion that numbers its own items.

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
accordion.rootThe <div> around the items.
item.rootThe <div> around an item's header and panel.
item.headerAn <h1> to <h6> around the trigger.
item.triggerThe <button> that opens and closes the item's panel.
item.panelThe <div> with the content. Returns undefined once it has closed, unless kept mounted.

API reference

Props

PropTypeDescription
valueReadonlyArray<string>The values of the open items. Required: the consumer owns it.
multiplebooleanLets several items be open at once. Off by default.
disabledbooleanDisables every item.
keepMountedbooleanClosed panels stay in the page, with hidden.
hiddenUntilFoundbooleanClosed panels stay in the page as hidden="until-found", so find-in-page can open them.
orientation"vertical" | "horizontal"Written as data-orientation on every part, for styling.
AccordionItem: valuestringThe item's value, unique within its accordion. Required.
AccordionItem: disabledbooleanDisables this item.

Events

EventDescription
ToggledThe value the user asked for and the reason. Store it and pass it back as value.
AccordionItem: SettledThe panel's open or close transition finished.

Data attributes

AttributeDescription
data-open / data-closedOn the item's root, header and panel.
data-panel-openOn the trigger while its panel is open.
data-disabledOn every part of a disabled accordion or item.
data-starting-style / data-ending-styleOn the item's root, trigger and panel: the first frame after opening, and while closing.
data-indexThe item's position among the accordion's items.
data-orientationThe accordion's orientation, vertical by default.
--accordion-panel-height / --accordion-panel-widthCSS variables on the panel: its content's size in px while it opens and closes, auto otherwise.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.