Accordion

Sections that open and close.

One section stays open at a time unless multiple is set. Each trigger sits in an AccordionHeader, so the sections keep a heading structure; the panels animate their height.

Preview
basic.tsx
import {
  Accordion,
  AccordionHeader,
  AccordionItem,
  AccordionPanel,
  AccordionTrigger,
} from "~/components/ui/accordion"

const sections = [
  {
    value: "setup",
    title: "Setup & paper shape",
    body: "Trim size, bleed and margins for every page of the product.",
  },
  {
    value: "sections",
    title: "Sections & pages",
    body: "Which sections the edition has, and how many pages each one gets.",
  },
  {
    value: "styles",
    title: "Styles & furniture",
    body: "Paragraph, character and object styles, plus folios and rules.",
  },
]

export default function Example() {
  return (
    <Accordion defaultValue={["setup"]} className="max-w-md">
      {sections.map((section) => (
        <AccordionItem key={section.value} value={section.value}>
          <AccordionHeader>
            <AccordionTrigger>{section.title}</AccordionTrigger>
          </AccordionHeader>
          <AccordionPanel>{section.body}</AccordionPanel>
        </AccordionItem>
      ))}
    </Accordion>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20lucide-react@1.73.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/accordion.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 lucide-react@1.73.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/accordion.tsx
import {
  Accordion as AccordionPrimitive,
  AccordionHeader,
  AccordionItem as AccordionItemPrimitive,
  AccordionPanel as AccordionPanelPrimitive,
  AccordionTrigger as AccordionTriggerPrimitive,
} from "@ui-sdk/react/accordion"
import { ChevronDown } from "lucide-react"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/** Sections that open and close; one stays open at a time unless `multiple` is set. */
function Accordion({ className, ...props }: React.ComponentProps<typeof AccordionPrimitive>) {
  return <AccordionPrimitive className={cnState("flex w-full flex-col", className)} {...props} />
}

/** One section: an `AccordionHeader` holding an `AccordionTrigger`, and an `AccordionPanel`. */
function AccordionItem({
  className,
  ...props
}: React.ComponentProps<typeof AccordionItemPrimitive>) {
  return (
    <AccordionItemPrimitive
      className={cnState("border-b border-border last:border-b-0", className)}
      {...props}
    />
  )
}

/** Opens and closes its section, with its chevron built in. */
function AccordionTrigger({
  className,
  children,
  ...props
}: React.ComponentProps<typeof AccordionTriggerPrimitive>) {
  return (
    <AccordionTriggerPrimitive
      className={cnState(
        [
          textVariants({ size: "sm", weight: "medium" }),
          "group/trigger flex w-full flex-1 cursor-pointer items-center justify-between gap-xl py-md text-left",
          "transition-colors outline-none hover:underline hover:underline-offset-4 focus-visible:ring-3 focus-visible:ring-ring/30",
          "data-disabled:pointer-events-none data-disabled:opacity-50",
        ].join(" "),
        className,
      )}
      {...props}
    >
      {(state) => (
        <>
          {typeof children === "function" ? children(state) : children}
          <ChevronDown className="size-4 shrink-0 text-muted-foreground transition-transform duration-200 group-data-open/trigger:rotate-180 motion-reduce:transition-none" />
        </>
      )}
    </AccordionTriggerPrimitive>
  )
}

/** The section's content. Its height animates as it opens and closes. */
function AccordionPanel({
  className,
  children,
  ...props
}: React.ComponentProps<typeof AccordionPanelPrimitive>) {
  return (
    <AccordionPanelPrimitive
      className={cnState(
        [
          textVariants({ size: "sm", tone: "muted" }),
          "h-(--accordion-panel-height) overflow-hidden transition-[height] duration-200 ease-out data-ending-style:h-0 data-starting-style:h-0 motion-reduce:transition-none",
        ],
        className,
      )}
      {...props}
    >
      {(state) => (
        <div className="pb-lg">{typeof children === "function" ? children(state) : children}</div>
      )}
    </AccordionPanelPrimitive>
  )
}

export { Accordion, AccordionHeader, AccordionItem, AccordionPanel, AccordionTrigger }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Accordion

Sections that open and close; one stays open at a time unless multiple is set.

className

Type string | ((state: AccordionState<string>) => string)

collapsible

Type boolean

controller

Type { getValue: () => string[]; setValue: (value: string[], options?: AccordionChangeOptions) => boolean; isOpen: (value: string) => boolean; open: (value: string, options?: AccordionChangeOptions) => boolean; ... 4 more ...; focus: (value?: string | undefined) => void; } | undefined

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultValue

Type string[]

hiddenUntilFound

Type boolean

keepMounted

Type boolean

onValueChange

Type ((value: string[], details: { reason: "imperative"; event?: undefined; source: ChangeSource; cancel: () => void; readonly canceled: boolean; } | { reason: "trigger-press"; event?: MouseEvent | KeyboardEvent | TouchEvent; source: ChangeSource; cancel: () => void; readonly canceled: boolean; } | { ...; }) => void) | u...

render

Type RenderProp<AccordionState<string>>

style

Type CSSProperties | ((state: AccordionState<string>) => CSSProperties)

value

Type string[]

AccordionItem

One section: an AccordionHeader holding an AccordionTrigger, and an AccordionPanel.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<AccordionItemState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<AccordionItemState>) => string)

disabled

Stops the item from opening or closing. Its trigger stays focusable.

Type boolean · Default false

onOpenChange

Runs before the item opens or closes, before the accordion's onValueChange. details.cancel() stops it.

Type (open: boolean, details: ChangeDetails<AccordionValueChangeReason>) => void

onOpenChangeComplete

Runs once the item's panel has finished opening or closing, after any animation.

Type (open: boolean) => void

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<AccordionItemState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<AccordionItemState>) => CSSProperties)

value

Identifies the item. The accordion's value lists the open items' values. Defaults to a generated id.

Type string

AccordionPanel

The section's content. Its height animates as it opens and closes.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<AccordionItemState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<AccordionItemState>) => string)

hiddenUntilFound

Whether the panel stays findable with hidden="until-found" while closed. Overrides the accordion's.

Type boolean

keepMounted

Whether the panel stays in the page, hidden, while closed. Overrides the accordion's.

Type boolean

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<AccordionItemState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<AccordionItemState>) => CSSProperties)

value

@internal The value of the item the panel belongs to. Parts read it from the item.

Type string

AccordionTrigger

Opens and closes its section, with its chevron built in.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<AccordionItemState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<AccordionItemState>) => string)

disabled

Stops this trigger from opening or closing the item. It stays focusable.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<AccordionItemState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<AccordionItemState>) => CSSProperties)

value

@internal The value of the item the trigger belongs to. Parts read it from the item.

Type string