Svelte Accordion - Flowbite

Use the accordion component to show hidden information based on the collapse and expand state of the child elements using data attribute options

The accordion component is a collection of vertically collapsing header and body elements that can be used to show and hide information based on the Tailwind CSS utility classes and JavaScript from Flowbite.

A popular use case would be the “Frequently Asked Questions” section of a website or page when you can show questions and answers for each child element.

Setup #

  • Svelte
<script>
  import { AccordionItem, Accordion } from 'flowbite-svelte'
</script>

Default accordion #

Accordion uses the single selection mode by default i.e. it collapses every other child element when expanding a one.

To prevent that behavior set multiple property to true.

  • Svelte
<script>
  import {AccordionItem, Accordion} from 'flowbite-svelte'
</script>
<Accordion>
  <AccordionItem>
    <span slot="header">My Header 1</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
    <p class="text-gray-500 dark:text-gray-400">Check out this guide to learn how to <a href="/" target="_blank" rel="noreferrer" class="text-blue-600 dark:text-blue-500 hover:underline">get started</a> and start developing websites even faster with components on top of Tailwind CSS.</p>
  </AccordionItem>
  <AccordionItem>
    <span slot="header">My Header 2</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Learn more about these technologies:</p>
    <ul class="list-disc pl-5 dark:text-gray-400 text-gray-500">
      <li><a href="/" target="_blank" rel="noreferrer" class="text-blue-600 dark:text-blue-500 hover:underline" >Lorem ipsum</a></li>
      <li><a href="https://tailwindui.com/" rel="noreferrer" target="_blank"  class="text-blue-600 dark:text-blue-500 hover:underline">Tailwind UI</a></li>
    </ul>
  </AccordionItem>
</Accordion>

Always open #

Use the open prop to make an item open on mount.

  • Svelte
<script>
  import {AccordionItem, Accordion} from 'flowbite-svelte'
</script>

<Accordion>
  <AccordionItem open>
    <span slot="header">Header 2-1</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
  <AccordionItem>
    <span slot="header">Header 2-2</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
</Accordion>

Color option #

You can control the look and feel of AccordionItems by overwriting the activeClasses and inactiveClasses properties. You can define them in Accordion so that they will apply to all children or set them individually on each AccordionItem.

  • Svelte
<script>
  import {AccordionItem, Accordion} from 'flowbite-svelte'
</script>

<Accordion
  activeClasses="bg-blue-100 dark:bg-gray-800 text-blue-600 dark:text-white focus:ring-4 focus:ring-blue-200 dark:focus:ring-blue-800"
  inactiveClasses="text-gray-500 dark:text-gray-400 hover:bg-blue-100 dark:hover:bg-gray-800">
  <AccordionItem class="">
    <span slot="header">Header 2-1</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
  <AccordionItem>
    <span slot="header">Header 2-2</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
</Accordion>

Flush accordion #

Use flush prop to remove the rounded borders.

  • Svelte
<script>
  import {AccordionItem, Accordion} from 'flowbite-svelte'
</script>

<Accordion flush>
  <AccordionItem>
    <span slot="header">Header 2-1</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
  <AccordionItem>
    <span slot="header">Header 2-2</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
</Accordion>

Arrow style #

Use the arrowup and arrowdown slots to set up and down icons.

  • Svelte
<script>
  import {AccordionItem, Accordion} from 'flowbite-svelte'
  import { AngleDownSolid, AngleUpSolid } from 'flowbite-svelte-icons'
</script>

<Accordion>
  <AccordionItem>
    <span slot="header">Header 2-1</span>
    <div slot="arrowup">
      <AngleUpSolid size="12" class="shrink-0 -mr-0.5" />
    </div>
    <span slot="arrowdown">
      <AngleDownSolid size="12" class="shrink-0 -mr-0.5" />
    </span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
  <AccordionItem>
    <span slot="header">Header 2-2</span>
        <div slot="arrowup">
      <AngleUpSolid size="12" class="shrink-0 -mr-0.5" />
    </div>
    <span slot="arrowdown">
      <AngleDownSolid size="12" class="shrink-0 -mr-0.5" />
    </span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
</Accordion>

Icon Accordion #

  • Svelte
<script>
  import { AccordionItem, Accordion } from 'flowbite-svelte'
  import { CartOutline, CogOutline } from 'flowbite-svelte-icons'
</script>

<Accordion>
  <AccordionItem>
    <span slot="header" class="text-base flex gap-2">
      <CartOutline />
      <span> My Header 1</span>
    </span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo...</p>
    <p class="text-gray-500 dark:text-gray-400">Check out this guide to learn how to <a href="/" target="_blank" rel="noreferrer" class="text-blue-600 dark:text-blue-500 hover:underline">get started</a> and start  websites even faster with components on top of Tailwind CSS.</p>
  </AccordionItem>
  <AccordionItem>
    <span slot="header" class="text-base flex gap-2">
      <CogOutline />
      <span> My Header 2</span>
    </span>
    <p class="mb-2 text-gray-500 dark:text-gray-400"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sintexplicabo...</p>
  </AccordionItem>
</Accordion>

Multiple mode #

Example how to use the multiple option together with expand all behavior.

  • Svelte
<script>
  import {AccordionItem, Accordion, Button} from 'flowbite-svelte'
  const items = Array(3);

  const open_all = () => items.forEach((_,i)=> items[i] = true)
  const close_all= () => items.forEach((_,i)=> items[i] = false)
</script>
<Button on:click={open_all}>Open all</Button>
<Button on:click={close_all}>Close all</Button>
<Accordion multiple>
  <AccordionItem bind:open={items[0]}>
    <span slot="header">My Header 1</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
    <p class="text-gray-500 dark:text-gray-400">Check out this guide to learn how to <a href="/" target="_blank" rel="noreferrer" class="text-blue-600 dark:text-blue-500 hover:underline">get started</a> and start developing websites even faster with components on top of Tailwind CSS.</p>
  </AccordionItem>
  <AccordionItem bind:open={items[1]}>
    <div slot="header">My Header 2</div>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Learn more about these technologies:</p>
  </AccordionItem>
  <AccordionItem bind:open={items[2]}>
    <div slot="header">My Header 3</div>
    <p>Something more</p>
  </AccordionItem>
</Accordion>

Custom transitions #

The default transition of AccordionItems is slide . Use the transitionType and transitionParams prop to make custom transitions.

  • Svelte
<script>
  import {AccordionItem, Accordion} from 'flowbite-svelte'
</script>
<Accordion>
  <AccordionItem>
    <span slot="header">My Header 1</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
  <AccordionItem transitionParams="{{ duration: 2000 }}">
    <span slot="header">My Header 2</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
  <AccordionItem transitionType="fade" transitionParams="{{ duration: 1000 }}">
    <span slot="header">My Header 3</span>
    <p class="mb-2 text-gray-500 dark:text-gray-400">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo ab necessitatibus sint explicabo ...</p>
  </AccordionItem>
</Accordion>

Props #

The component has the following props, type, and default values. See types page for type information.

Accordion #

  • Use the class prop to overwrite defaultClass.
  • Use the classActive prop to overwrite activeClass.
  • Use the classInactive prop to overwrite inactiveClass.
Name Type Default
multiple boolean false
flush boolean false
activeClass string 'bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-800'
inactiveClass string 'text-gray-500 dark:text-gray-400 hover:bg-gray-100 hover:dark:bg-gray-800'
defaultClass string 'text-gray-500 dark:text-gray-400'

AccordionItem #

  • Use the class prop to overwrite defaultClass.
Name Type Default
open boolean false
activeClass string | undefined undefined
inactiveClass string | undefined undefined
defaultClass string 'flex items-center justify-between w-full font-medium text-left group-first:rounded-t-xl border-gray-200 dark:border-gray-700'
transitionType TransitionTypes 'slide'
transitionParams TransitionParamTypes {}
paddingFlush string 'py-5'
paddingDefault string 'p-5'
textFlushOpen string 'text-gray-900 dark:text-white'
textFlushDefault string 'text-gray-500 dark:text-gray-400'
borderClass string 'border-l border-r group-first:border-t'
borderOpenClass string 'border-l border-r'
borderBottomClass string 'border-b'
borderSharedClass string 'border-gray-200 dark:border-gray-700'
classActive string | undefined undefined
classInactive string | undefined undefined

Slots #

Accordion #

Name Description
default Place for AccordionItems

AccordionItem #

Name Description
default Item body slot
header Item header slot
arrowup Icon to close an accordion item
arrowdown Icon to open an accordion item

References #