Skip to content
2k
On This Page

Accordion

A vertically stacked set of interactive headings that each reveal a section of content.

We offer standard (5-7 days), express (2-3 days), and overnight shipping. Free shipping on international orders.

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "~/components/ui/accordion"

export default function AccordionDemo() {
  return (
    <Accordion class="max-w-lg" collapsible defaultValue={["shipping"]}>
      <AccordionItem value="shipping">
        <AccordionTrigger>What are your shipping options?</AccordionTrigger>
        <AccordionContent>
          We offer standard (5-7 days), express (2-3 days), and overnight
          shipping. Free shipping on international orders.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem value="returns">
        <AccordionTrigger>What is your return policy?</AccordionTrigger>
        <AccordionContent>
          Returns accepted within 30 days. Items must be unused and in original
          packaging. Refunds processed within 5-7 business days.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem value="support">
        <AccordionTrigger>How can I contact customer support?</AccordionTrigger>
        <AccordionContent>
          Reach us via email, live chat, or phone. We respond within 24 hours
          during business days.
        </AccordionContent>
      </AccordionItem>
    </Accordion>
  )
}

Installation

pnpm dlx solidui-cli@latest add accordion

Install the following dependencies:

pnpm add @ui-sdk/solid @ui-sdk/core

Copy and paste the following code into your project.

components/ui/accordion.tsx
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import type { JSX } from "@solidjs/web"
import { type ComponentProps } from "@solidjs/web"
import {
  Accordion as SdkAccordion,
  AccordionItem as SdkAccordionItem,
  AccordionHeader as SdkAccordionHeader,
  AccordionTrigger as SdkAccordionTrigger,
  AccordionPanel as SdkAccordionPanel
} from "@ui-sdk/solid/accordion"
import { merge, omit } from "solid-js"

import { cn } from "~/lib/utils"
type AccordionProps = Omit<
  ComponentProps<typeof SdkAccordion>,
  "class" | "children"
> & {
  children?: JSX.Element
  class?: string
}
const Accordion = (props: AccordionProps) => (
  <SdkAccordion
    {...props}
    class={cn("flex w-full flex-col", props.class)}
  />
)
type AccordionItemProps = Omit<
  ComponentProps<typeof SdkAccordionItem>,
  "class" | "children"
> & {
  children?: JSX.Element
  class?: string
}
const AccordionItem = (props: AccordionItemProps) => (
  <SdkAccordionItem {...props} class={cn("not-last:border-b", props.class)} />
)
type AccordionTriggerProps = Omit<
  ComponentProps<typeof SdkAccordionTrigger>,
  "class" | "children"
> & { children?: JSX.Element; class?: string }
const AccordionTrigger = (props: AccordionTriggerProps) => {
  const others = omit(props, "class", "children", "render")
  const renderTrigger: AccordionTriggerProps["render"] = (part, state) => {
    const attributes = merge(part, () => ({
      tabindex: state.disabled ? -1 : part.tabindex
    }))
    return props.render ? (
      props.render(attributes, state)
    ) : (
      <button {...attributes} />
    )
  }
  return (
    <SdkAccordionHeader class="flex">
      <SdkAccordionTrigger
        class={cn(
          "group/accordion-trigger rounded-lg py-2.5 text-left text-sm font-medium hover:underline focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:after:border-ring **:data-[slot=accordion-trigger-icon]:ml-auto **:data-[slot=accordion-trigger-icon]:size-4 **:data-[slot=accordion-trigger-icon]:text-muted-foreground relative flex flex-1 items-start justify-between border border-transparent transition-all outline-none disabled:pointer-events-none disabled:opacity-50 data-disabled:pointer-events-none data-disabled:opacity-50",
          props.class
        )}
        {...others}
        render={renderTrigger}
      >
        {props.children}
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          class="pointer-events-none shrink-0 duration-300 group-aria-expanded/accordion-trigger:rotate-180"
          data-slot="accordion-trigger-icon"
        >
          <path d="m6 9 6 6 6-6" />
        </svg>
      </SdkAccordionTrigger>
    </SdkAccordionHeader>
  )
}
type AccordionContentProps = Omit<
  ComponentProps<typeof SdkAccordionPanel>,
  "class" | "children"
> & { children?: JSX.Element; class?: string }
const AccordionContent = (props: AccordionContentProps) => {
  const others = omit(props, "class", "children")
  return (
    <SdkAccordionPanel class="text-sm data-ending-style:animate-accordion-up data-open:animate-accordion-down overflow-hidden" {...others}>
      <div
        class={cn(
          "pt-0 pb-2.5 h-(--accordion-panel-height) [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4",
          props.class
        )}
        data-slot="accordion-content-inner"
      >
        {props.children}
      </div>
    </SdkAccordionPanel>
  )
}
export { Accordion, AccordionContent, AccordionItem, AccordionTrigger }

Update the import paths to match your project setup.

Usage

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "~/components/ui/accordion"
<Accordion collapsible defaultValue={["item-1"]}>
  <AccordionItem value="item-1">
    <AccordionTrigger>Is it accessible?</AccordionTrigger>
    <AccordionContent>
      Yes. It adheres to the WAI-ARIA design pattern.
    </AccordionContent>
  </AccordionItem>
</Accordion>

Composition

Use the following composition to build an Accordion:

Accordion
โ”œโ”€โ”€ AccordionItem
โ”‚   โ”œโ”€โ”€ AccordionTrigger
โ”‚   โ””โ”€โ”€ AccordionContent
โ””โ”€โ”€ AccordionItem
    โ”œโ”€โ”€ AccordionTrigger
    โ””โ”€โ”€ AccordionContent

Basic

A basic accordion that shows one item at a time. The first item is open by default.

Click on 'Forgot Password' on the login page, enter your email address, and we'll send you a link to reset your password. The link will expire in 24 hours.

import { For } from "solid-js"

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "~/components/ui/accordion"

const items = [
  {
    value: "item-1",
    trigger: "How do I reset my password?",
    content:
      "Click on 'Forgot Password' on the login page, enter your email address, and we'll send you a link to reset your password. The link will expire in 24 hours."
  },
  {
    value: "item-2",
    trigger: "Can I change my subscription plan?",
    content:
      "Yes, you can upgrade or downgrade your plan at any time from your account settings. Changes will be reflected in your next billing cycle."
  },
  {
    value: "item-3",
    trigger: "What payment methods do you accept?",
    content:
      "We accept all major credit cards, PayPal, and bank transfers. All payments are processed securely through our payment partners."
  }
]

export default function AccordionBasic() {
  return (
    <Accordion class="max-w-lg" collapsible defaultValue={["item-1"]}>
      <For each={items}>
        {(item) => (
          <AccordionItem value={item.value}>
            <AccordionTrigger>{item.trigger}</AccordionTrigger>
            <AccordionContent>{item.content}</AccordionContent>
          </AccordionItem>
        )}
      </For>
    </Accordion>
  )
}

Multiple

Use the multiple prop to allow multiple items to be open at the same time.

Manage how you receive notifications. You can enable email alerts for updates or push notifications for mobile devices.

import { For } from "solid-js"

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "~/components/ui/accordion"

const items = [
  {
    value: "notifications",
    trigger: "Notification Settings",
    content:
      "Manage how you receive notifications. You can enable email alerts for updates or push notifications for mobile devices."
  },
  {
    value: "privacy",
    trigger: "Privacy & Security",
    content:
      "Control your privacy settings and security preferences. Enable two-factor authentication, manage connected devices, review active sessions, and configure data sharing preferences. You can also download your data or delete your account."
  },
  {
    value: "billing",
    trigger: "Billing & Subscription",
    content:
      "View your current plan, payment history, and upcoming invoices. Update your payment method, change your subscription tier, or cancel your subscription."
  }
]

export default function AccordionMultiple() {
  return (
    <Accordion class="max-w-lg" defaultValue={["notifications"]} multiple>
      <For each={items}>
        {(item) => (
          <AccordionItem value={item.value}>
            <AccordionTrigger>{item.trigger}</AccordionTrigger>
            <AccordionContent>{item.content}</AccordionContent>
          </AccordionItem>
        )}
      </For>
    </Accordion>
  )
}

Disabled

Use the disabled prop on AccordionItem to disable individual items.

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "~/components/ui/accordion"

export default function AccordionDisabled() {
  return (
    <Accordion class="w-full" collapsible>
      <AccordionItem value="item-1">
        <AccordionTrigger>Can I access my account history?</AccordionTrigger>
        <AccordionContent>
          Yes, you can view your complete account history including all
          transactions, plan changes, and support tickets in the Account History
          section of your dashboard.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem disabled value="item-2">
        <AccordionTrigger>Premium feature information</AccordionTrigger>
        <AccordionContent>
          This section contains information about premium features. Upgrade your
          plan to access this content.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem value="item-3">
        <AccordionTrigger>How do I update my email address?</AccordionTrigger>
        <AccordionContent>
          You can update your email address in your account settings.
          You&apos;ll receive a verification email at your new address to
          confirm the change.
        </AccordionContent>
      </AccordionItem>
    </Accordion>
  )
}

Borders

Add border to the Accordion and border-b last:border-b-0 to the AccordionItem to add borders to the items.

We offer monthly and annual subscription plans. Billing is charged at the beginning of each cycle, and you can cancel anytime. All plans include automatic backups, 24/7 support, and unlimited team members.

import { For } from "solid-js"

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "~/components/ui/accordion"

const items = [
  {
    value: "billing",
    trigger: "How does billing work?",
    content:
      "We offer monthly and annual subscription plans. Billing is charged at the beginning of each cycle, and you can cancel anytime. All plans include automatic backups, 24/7 support, and unlimited team members."
  },
  {
    value: "security",
    trigger: "Is my data secure?",
    content:
      "Yes. We use end-to-end encryption, SOC 2 Type II compliance, and regular third-party security audits. All data is encrypted at rest and in transit using industry-standard protocols."
  },
  {
    value: "integration",
    trigger: "What integrations do you support?",
    content:
      "We integrate with 500+ popular tools including Slack, Zapier, Salesforce, HubSpot, and more. You can also build custom integrations using our REST API and webhooks."
  }
]

export default function AccordionBorders() {
  return (
    <Accordion
      class="max-w-lg rounded-lg border"
      collapsible
      defaultValue={["billing"]}
    >
      <For each={items}>
        {(item) => (
          <AccordionItem
            class="border-b px-4 last:border-b-0"
            value={item.value}
          >
            <AccordionTrigger>{item.trigger}</AccordionTrigger>
            <AccordionContent>{item.content}</AccordionContent>
          </AccordionItem>
        )}
      </For>
    </Accordion>
  )
}

Card

Wrap the Accordion in a Card component.

Subscription & Billing
Common questions about your account, plans, payments and cancellations.

We offer three subscription tiers: Starter ($9/month), Professional ($29/month), and Enterprise ($99/month). Each plan includes increasing storage limits, API access, priority support, and team collaboration features.

import { For } from "solid-js"

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger
} from "~/components/ui/accordion"
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle
} from "~/components/ui/card"

const items = [
  {
    value: "plans",
    trigger: "What subscription plans do you offer?",
    content:
      "We offer three subscription tiers: Starter ($9/month), Professional ($29/month), and Enterprise ($99/month). Each plan includes increasing storage limits, API access, priority support, and team collaboration features."
  },
  {
    value: "billing",
    trigger: "How does billing work?",
    content:
      "Billing occurs automatically at the start of each billing cycle. We accept all major credit cards, PayPal, and ACH transfers for enterprise customers. You'll receive an invoice via email after each payment."
  },
  {
    value: "cancel",
    trigger: "How do I cancel my subscription?",
    content:
      "You can cancel your subscription anytime from your account settings. There are no cancellation fees or penalties. Your access will continue until the end of your current billing period."
  }
]

export default function AccordionCard() {
  return (
    <Card class="w-full max-w-sm">
      <CardHeader>
        <CardTitle>Subscription & Billing</CardTitle>
        <CardDescription>
          Common questions about your account, plans, payments and
          cancellations.
        </CardDescription>
      </CardHeader>
      <CardContent>
        <Accordion collapsible defaultValue={["plans"]}>
          <For each={items}>
            {(item) => (
              <AccordionItem value={item.value}>
                <AccordionTrigger>{item.trigger}</AccordionTrigger>
                <AccordionContent>{item.content}</AccordionContent>
              </AccordionItem>
            )}
          </For>
        </Accordion>
      </CardContent>
    </Card>
  )
}

API Reference

PartDescription
AccordionRoot container that manages expanded state.
AccordionItemIndividual collapsible section in the accordion.
AccordionTriggerInteractive header button for an item.
AccordionContentExpandable panel content for the active item.