Nuovi strumenti aggiunti! Scopri le novità

Home/Componenti/Accordion

Accordion examples

Un elenco comprimibile basato su elementi nativi details e summary, senza bisogno di JavaScript.

Un elenco comprimibile basato sugli elementi nativi details e summary. L'espansione e la chiusura, il supporto da tastiera e la corretta semantica sono gestiti direttamente dal browser, quindi il componente viene fornito senza alcun JavaScript.

La freccia ruota utilizzando la variante group-open, il che significa che lo stato aperto è stilizzato in CSS anziché tracciato nello state.

Accordion with dividers

The default FAQ layout: rows separated by hairlines, with a chevron that rotates when a row opens. No JavaScript.

Preview
export default function Accordion() {
  return (
    <div className="mx-auto max-w-2xl divide-y divide-gray-200">
      <details className="group py-4" open>
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do I need to install anything?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
      </details>

      <details className="group py-4">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Can I use these commercially?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
      </details>

      <details className="group py-4">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do the components work without JavaScript?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
      </details>

      <details className="group py-4">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Which frameworks are supported?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
      </details>
    </div>
  );
}

Single-open accordion

Every details element shares one name attribute, so opening a row closes the others. The browser handles it, with no script.

Preview
export default function ExclusiveAccordion() {
  return (
    <div className="mx-auto max-w-2xl divide-y divide-gray-200">
      <details className="group py-4" name="faq" open>
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do I need to install anything?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
      </details>

      <details className="group py-4" name="faq">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Can I use these commercially?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
      </details>

      <details className="group py-4" name="faq">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do the components work without JavaScript?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
      </details>

      <details className="group py-4" name="faq">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Which frameworks are supported?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
      </details>
    </div>
  );
}

Accordion with bordered cards

Each row is its own rounded card that tints when open. Reads better when the questions are unrelated rather than one continuous list.

Preview
export default function CardAccordion() {
  return (
    <div className="mx-auto max-w-2xl space-y-3">
      <details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50" open>
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do I need to install anything?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
      </details>

      <details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Can I use these commercially?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
      </details>

      <details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do the components work without JavaScript?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
      </details>

      <details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Which frameworks are supported?
          <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
      </details>
    </div>
  );
}

Accordion with plus and minus icons

A plus that becomes a minus using group-open. Clearer than a chevron when the open state needs to be obvious from a distance.

Preview
export default function PlusMinusAccordion() {
  return (
    <div className="mx-auto max-w-2xl divide-y divide-gray-200">
      <details className="group py-4" open>
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do I need to install anything?
          <svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
          <svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
      </details>

      <details className="group py-4">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Can I use these commercially?
          <svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
          <svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
      </details>

      <details className="group py-4">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Do the components work without JavaScript?
          <svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
          <svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
      </details>

      <details className="group py-4">
        <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
          Which frameworks are supported?
          <svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
          <svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
        </summary>
        <p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
      </details>
    </div>
  );
}

FAQ section with heading beside the accordion

A heading and support line on the left and the accordion on the right at lg, stacked on smaller screens. Suits a full FAQ section on a landing page.

Preview
export default function FaqSection() {
  return (
    <section className="mx-auto grid max-w-5xl gap-10 px-4 py-12 lg:grid-cols-3">
      <div className="bg-gray-900 p-4 rounded-lg">
        <h2 className="text-2xl font-bold text-gray-100">Frequently asked questions</h2>
        <p className="mt-3 text-sm text-white">Cannot find an answer? Email us and we will reply within a day.</p>
      </div>

      <div className="divide-y divide-gray-200 lg:col-span-2">
        <details className="group py-4" open>
          <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
            Do I need to install anything?
            <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
          </summary>
          <p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
        </details>

        <details className="group py-4">
          <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
            Can I use these commercially?
            <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
          </summary>
          <p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
        </details>

        <details className="group py-4">
          <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
            Do the components work without JavaScript?
            <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
          </summary>
          <p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
        </details>

        <details className="group py-4">
          <summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
            Which frameworks are supported?
            <svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
          </summary>
          <p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
        </details>
      </div>
    </section>
  );
}

What is a Accordion?

Un accordion è un elenco verticale in cui ogni riga ha un'intestazione su cui è possibile cliccare per rivelare il suo contenuto. È uno strumento di densità: consente a una pagina di mostrare venti intestazioni nello spazio di tre paragrafi e permette ai lettori di aprire solo quelle che li riguardano. Le sezioni FAQ sono l'uso più comune, ma lo stesso pattern serve per pannelli di impostazioni, gruppi di filtri e qualsiasi elenco in cui la maggior parte dei lettori preferisce che la maggior parte del contenuto sia compressa.

Il pattern viene costantemente ricostruito in JavaScript, il che è quasi sempre non necessario. L'HTML ha gli elementi details e summary che fanno esattamente questo: summary è l'intestazione cliccabile, tutto il resto all'interno di details è il pannello, e il browser gestisce il toggle, l'attivazione da tastiera, lo stato expanded nell'albero di accessibilità e la capacità della funzione trova nella pagina del browser di aprire una sezione compressa per mostrare una corrispondenza. Un accordion in JavaScript deve reimplementare tutto ciò, e in genere ne reimplementa la maggior parte in modo errato.

L'unica cosa che details non fa per impostazione predefinita è chiudere i suoi elementi fratelli quando uno viene aperto. Questo comportamento, spesso chiamato accordion esclusivo, è ora disponibile assegnando a ogni elemento details lo stesso attributo name, che i browser trattano come un gruppo. Valutare se utilizzarlo è una vera questione di design piuttosto che tecnica: gli accordion esclusivi mantengono la pagina corta ma rendono impossibile confrontare due righe senza riaprirne una.

Stilizzare details era complicato a causa del triangolo di disclosure predefinito, che è un indicatore di elenco su summary. Per rimuoverlo serve marker:content-none in Tailwind, più la variante specifica per webkit sui vecchi Safari. Una volta rimosso, summary è un normale contenitore flex e la freccia personalizzata può essere posizionata dove desiderato.

Anatomy

Container
Il wrapper che racchiude le righe, contenente i divider e la max width. Vincola la larghezza per la leggibilità: le risposte FAQ a tutta larghezza sono difficili da scorrere.
Details
Uno per riga. La classe group qui è ciò che permette alla freccia di rispondere allo stato aperto tramite la variante group-open.
Summary
L'intestazione cliccabile. Già focalizzabile e attivabile da tastiera, quindi non richiede alcun role, nessun tabindex e nessun gestore di eventi da tastiera.
Indicator
La freccia, ruotata con group-open. Contrassegnata come aria-hidden perché lo stato expanded viene già annunciato dall'elemento stesso.
Panel
Tutto ciò che si trova all'interno di details dopo summary. Qualsiasi markup funziona qui: paragrafi, elenchi, form, altri componenti.

How to Use

  1. 1

    Copia il markup

    Scegli una qualsiasi versione. Quella in React è un semplice function component senza state e senza la direttiva use client, quindi funziona come server component nel Next.js App Router.

  2. 2

    Sostituisci le righe

    Modifica l'array items o duplica un blocco details nella versione HTML. I pannelli accettano qualsiasi markup, non solo un paragrafo.

  3. 3

    Decidi cosa inizia aperto

    Aggiungi l'attributo open alla riga che deve essere espansa al caricamento. Aprire la prima rende ovvio che le righe sono interattive.

  4. 4

    Scegli se renderlo esclusivo o meno

    Assegna a ogni details lo stesso attributo name se l'apertura di uno deve chiudere gli altri. Lascia l'attributo se i lettori potrebbero voler confrontare le righe.

  5. 5

    Stilizza il summary

    Rimuovi il marker predefinito con marker:content-none, quindi tratta summary come un normale contenitore flex per l'etichetta e l'indicatore.

Features

Nessun JavaScript

L'attivazione tramite tastiera, il toggle e lo stato sono gestiti dal browser, quindi il componente funziona prima dell'idratazione e continua a funzionare in caso di errore degli script.

Semantica corretta per impostazione predefinita

Lo stato espanso viene comunicato automaticamente alle tecnologie assistive, senza alcun attributo aria-expanded da impostare o mantenere sincronizzato.

Individuabile quando è chiuso

La funzione Trova nella pagina del browser può aprire un pannello chiuso per rivelare una corrispondenza, cosa che un accordion in JavaScript basato su div non può fare.

Indicatore gestito via CSS

La rotazione della chevron deriva dalla variante group-open, quindi lo stato aperto guida direttamente lo styling anziché passare attraverso una variabile di stato.

Modalità esclusiva senza codice

Un attributo name condiviso fa sì che il gruppo si comporti come un accordion in cui è possibile aprirne uno alla volta, gestito interamente dal browser.

Compatibile con i Server Component

Senza alcuno stato né effetti, la versione React viene eseguita sul server nel Next.js App Router senza la direttiva use client.

Common Uses

Sezioni FAQ

L'uso più comune. Venti domande trovano spazio nell'altezza di uno schermo e i lettori aprono solo le due che interessano loro.

Gruppi di filtri

Gruppi comprimibili di categorie, prezzi e attributi in una sidebar di prodotti, in modo che un set di filtri lungo rimanga navigabile su uno schermo corto.

Pannelli delle impostazioni

Raggruppare opzioni avanzate dietro una riga chiusa rende una pagina di impostazioni accessibile senza nascondere del tutto le opzioni.

Dettagli della documentazione

Inserire tabelle API complete, messaggi di errore lunghi o passaggi di configurazione opzionali dietro una disclosure in modo che il flusso principale rimanga leggibile.

Accessibility

Customization

Props

PropTypeDefaultDescription
items *{ question: string; answer: string }[]-Le righe dell'accordion. Mantienile a circa una dozzina o meno; oltre questo limite, un elenco ricercabile o un set di pagine separate serve meglio i lettori.
defaultOpenIndexnumber0Quale riga inizia espansa. Aprire la prima segnala che le righe sono interattive; lasciare tutto chiuso può sembrare un elenco statico di intestazioni.

Why Use These Accordions?

È la versione con minori possibilità di errore. Nessuno stato, nessuna dipendenza, nessuna idratazione, nessun ARIA da mantenere sincronizzato e un comportamento che funziona prima che JavaScript venga caricato e anche se fallisce.

Rimane individuabile. Il contenuto all'interno di un details chiuso può comunque essere trovato tramite la funzione Trova nella pagina del browser, che è il modo in cui molti lettori navigano realmente in una FAQ lunga - un accordion basato su div nasconde silenziosamente quel contenuto alla ricerca.

La versione React non richiede alcuna direttiva use client, quindi viene renderizzata sul server. La maggior parte dei componenti accordion in circolazione sono client component puramente perché tengono traccia dello stato di apertura che il browser stava già tracciando.

Tips

Frequently Asked Questions

Related Components