import { createElement, type ComponentProps, type ReactNode } from 'react' import { Accordion as BaseAccordion } from '@base-ui/react/accordion' import { ChevronDown } from 'lucide-react' import { cn } from '../lib/cn' import { ACCORDION_ITEM_STYLES, ACCORDION_PANEL_INNER, ACCORDION_ROOT_STYLES, ACCORDION_TRIGGER_STYLES, accordionChevron, accordionPanel, accordionTrigger, type AccordionHeadingLevel, type AccordionVariant, } from './accordion.tokens' export type AccordionItem = { /** Unique id for the item; used in `value`. */ value: string title: ReactNode content: ReactNode disabled?: boolean } export type AccordionProps = Omit, 'defaultValue' | 'onChange'> & { items: AccordionItem[] /** Allows several items to be open at once. */ multiple?: boolean /** Open items (controlled). */ value?: string[] /** Initially open items (uncontrolled). */ defaultValue?: string[] /** Called with the open items whenever one opens or closes. */ onValueChange?: (value: string[]) => void /** `bordered` groups items in one box, `separated` gives each its own card, `ghost` only divides them. */ variant?: AccordionVariant /** Heading level of each trigger, to fit the page outline. */ headingLevel?: AccordionHeadingLevel /** Disables every item. */ disabled?: boolean } /** * A stack of collapsible sections. Closed panels stay in the DOM as * `hidden="until-found"`, so the browser's find-in-page can open them. */ export function Accordion({ items, multiple = false, value, defaultValue, onValueChange, variant = 'bordered', headingLevel = 3, disabled, className, ...props }: AccordionProps) { const heading = `h${headingLevel}` return ( onValueChange(next as string[]) : undefined} disabled={disabled} hiddenUntilFound className={cn(ACCORDION_ROOT_STYLES[variant], className)} {...props} > {items.map((item) => ( createElement(heading, headerProps)} className="m-0"> {item.title}
{item.content}
))}
) }