import { useId, type ReactNode } from 'react' import { Accordion as Primitive } from '@base-ui/react/accordion' import type { AccordionItem } from '@ggary/core/accordion' export interface AccordionProps { items: AccordionItem[] /** The section under an item. */ children: (item: AccordionItem) => ReactNode /** Controlled: the open sections. Omit and use `defaultValue` for uncontrolled. */ value?: string[] defaultValue?: string[] onValueChange?: (value: string[]) => void /** Several sections may stand open. Default false. */ multiple?: boolean /** The open section may be closed, leaving none. Default true. */ collapsible?: boolean disabled?: boolean /** The heading level of each section's button. Default 3. */ headingLevel?: 1 | 2 | 3 | 4 | 5 | 6 /** Keep a closed section's content in the page, hidden. Default true: find-in-page searches it. */ keepMounted?: boolean } const part = (name: string) => ({ 'data-scope': 'accordion', 'data-part': name, 'data-backend': 'base-ui' }) /** Base UI owns disclosure, focus semantics and find-in-page expansion. */ export function Accordion(props: AccordionProps) { const { items, children, value, defaultValue, onValueChange, multiple = false, collapsible = true, disabled, headingLevel = 3, keepMounted = true } = props const id = useId() const itemId = (value: string) => `${id}-${encodeURIComponent(value)}` return {...part('root')} value={value} defaultValue={defaultValue} disabled={disabled} multiple={multiple} hiddenUntilFound={keepMounted} onValueChange={(next, details) => { if (!multiple && !collapsible && next.length === 0) { details.cancel(); return } onValueChange?.(next) }}> {items.map((item) =>
}> }> } />
}>
{children(item)}
)} }