Cómo funciona
Each item is a filled, rounded row with no outlines or separators; its round plus turns 45° into a close mark and fills with ink while the item is open. Each panel is a one-row grid whose grid-template-rows goes from 0fr to 1fr (the content's natural height), with an inner min-height:0 + overflow:hidden layer clipping it. Nothing is measured in JS, so late images or rewrapping text cannot misalign it. The content also slides down 8px and fades in, slightly after the height starts. Closing is about 80% as long, and visibility turns hidden only after the height has closed.
Úsalo para
- FAQs in a help center or at the bottom of a checkout page.
- Folding low-use advanced options on a settings page.
- Long explanations such as shipping or return rules, collapsed by default to save space.
Busca otra opción cuando
- Key information every visitor must see: folded means hidden, so lay it out flat.
- A single item: use one disclosure such as “Show more” instead of an accordion.
- Switching between sibling views, one at a time: tabs are clearer.
Uso
import { SmoothAccordion } from './SmoothAccordion.jsx'
export function Faq() {
return (
<SmoothAccordion
defaultValue={['ship']}
items={[
{ value: 'ship', title: 'How long does delivery take?', content: 'Usually 2 to 4 business days.' },
{ value: 'return', title: 'Can I return an item?', content: 'Yes, within 30 days if it is unused.' },
]}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
items | { value, title, content, disabled? }[] | — | The items. Each has a unique value, a title, content, and an optional disabled flag. |
allowMultiple | boolean | false | Allows several items open at once. When off, opening one closes the others; turning it off at runtime keeps only the last opened item. |
value | string[] | — | Controlled: the list of open values. |
defaultValue | string[] | [] | Uncontrolled: the values open at first. |
onChange | (value: string[]) => void | — | Called when the open items change. |
duration | number | 240 | Opening time in ms; closing takes 0.8 times as long. When omitted it follows --ds-duration-base. |
headingLevel | 2 | 3 | 4 | 5 | 6 | 3 | Heading level wrapped around each title button; choose it to fit the page outline. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
Accesibilidad
- Each title is a <button> with aria-expanded and aria-controls, wrapped in a heading (h3 by default) to keep the page outline.
- Panels are role="region" labelled by their title.
- Closed panels are visibility: hidden, so their links and buttons cannot be tabbed to or read.
- Enter and Space toggle; Up and Down move between headings; Home and End jump to the first and last.
- Titles are at least 48px high with a :focus-visible ring; reduced motion opens and closes without a transition.
Movimiento
Opening: height 240ms ease-out, content fades in over 240ms ease-out (36ms delay) and slides down 8px, the plus turns 45° on a 240ms spring while its disc fills with ink. Closing: height 192ms standard, content fades out in 120ms ease-in. A pressed plus scales to 0.9.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
SmoothAccordion.jsxsmooth-accordion.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.