React Bootstrap Accordion
A React Bootstrap accordion built from four small components: items open from React state with Bootstrap's height transition. Flush and always-open variants.
This component is only available in AdminKit PRO. Learn more.
Use an accordion when a page has more sections than a reader wants to see at once, such as a FAQ or a long help page. Only the headers show, and a click on a header opens its section.
In AdminKit the accordion is four small React components: Accordion, AccordionItem, AccordionHeader and AccordionBody. They render Bootstrap's accordion markup and open items from React state, without Bootstrap's JavaScript or react-bootstrap. They are part of the PRO version of AdminKit for React.
The body slides open with the same height transition as Collapse, which the accordion is built on.
Example
Give each AccordionItem an eventKey. To start with one item open, pass its key to defaultActiveKey. Opening another item then closes it.
import {
Accordion,
AccordionBody,
AccordionHeader,
AccordionItem,
} from "@/components/bootstrap/accordion";
export function ShippingFaq() {
return (
<Accordion defaultActiveKey="shipping">
<AccordionItem eventKey="shipping">
<AccordionHeader>When do orders ship?</AccordionHeader>
<AccordionBody>Orders ship within two business days.</AccordionBody>
</AccordionItem>
<AccordionItem eventKey="returns">
<AccordionHeader>Can I return an order?</AccordionHeader>
<AccordionBody>Return any order within 30 days for a full refund.</AccordionBody>
</AccordionItem>
<AccordionItem eventKey="payment">
<AccordionHeader>Which payment methods do you accept?</AccordionHeader>
<AccordionBody>Credit cards, PayPal and bank transfer.</AccordionBody>
</AccordionItem>
</Accordion>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultActiveKey | string | string[] | The item or items open on the first render; all closed without it | |
alwaysOpen | boolean | false | Items stay open when another item opens |
Other props of Accordion, such as className and id, go to the .accordion element. All four components are in src/components/bootstrap/accordion.tsx.
AccordionItemtakes a requiredeventKey(string), unique in its accordion. Its other props go to.accordion-item.AccordionHeadertakesasfor the heading around the button ("h2"to"h6","h2"by default),classNamefor that heading, andchildrenfor the button text.AccordionBodypasses its props,classNameincluded, to.accordion-body, not to the collapsing wrapper around it.
Accordion keeps the open items to itself. After the first render, no prop can open an item from outside. If you want to open a panel from your own state, use Collapse instead. The parts throw an error when they are not inside an Accordion.
Flush
Inside a card, you may want the accordion to sit edge to edge. className="accordion-flush" removes the outer borders and the rounded corners.
<Accordion className="accordion-flush">
<AccordionItem eventKey="account">
<AccordionHeader>Account</AccordionHeader>
<AccordionBody>Change your name, email and password.</AccordionBody>
</AccordionItem>
<AccordionItem eventKey="billing">
<AccordionHeader>Billing</AccordionHeader>
<AccordionBody>Update your payment method and download invoices.</AccordionBody>
</AccordionItem>
</Accordion>Always open
Sometimes readers want to compare two sections side by side. With alwaysOpen, opening an item leaves the others as they are. To open several items at the start, pass a list of keys to defaultActiveKey.
<Accordion alwaysOpen defaultActiveKey={["profile", "notifications"]}>
<AccordionItem eventKey="profile">
<AccordionHeader>Profile</AccordionHeader>
<AccordionBody>Your photo and display name.</AccordionBody>
</AccordionItem>
<AccordionItem eventKey="notifications">
<AccordionHeader>Notifications</AccordionHeader>
<AccordionBody>Choose which emails you receive.</AccordionBody>
</AccordionItem>
</Accordion>Accessibility
- Each header is a
<button>in a heading, witharia-expandedandaria-controlspointing at the body.Accordiongenerates the bodyids withuseId, so two accordions on a page never share one. - Pick the heading level that fits the page outline with
as, for exampleas="h3"under anh2. - The headers are plain buttons: Tab moves between them, Enter and Space open and close an item.
- A closed body has
.collapse(display: none), so its content is out of the tab order and hidden from screen readers.
Related
- Next.js Bootstrap accordion: the same component in AdminKit for Next.js
- Bootstrap 5 accordion: the accordion of the HTML version
- Collapse: one panel that shows and hides from your own state
- General UI demo: a card accordion built with
Collapse
Last updated on
Sidebar
Add pages to the React Bootstrap sidebar of AdminKit, see how it slides over the page on small screens, and find the navbar and footer around it.
Alerts
A React Bootstrap alert component with theme colors, icons, outline styles and a close button that fades the alert out. No Bootstrap JavaScript needed.