Use our Tailwind CSS Accordion
component to allow the user to show and hide sections of related content on a page. There are many ways to use this component, like displaying a list of FAQs, showing various menus and submenus, displaying the locations of a particular company, and so on.
See below our simple and versatile Accordion
example that you can use in your React and Tailwind CSS projects. We also included some Accordion
props that are available.
import { Fragment, useState } from "react";
import {
Accordion,
AccordionHeader,
AccordionBody,
} from "@material-tailwind/react";
export default function Example() {
const [open, setOpen] = useState(0);
const handleOpen = (value) => {
setOpen(open === value ? 0 : value);
};
return (
<Fragment>
<Accordion open={open === 1} onClick={() => handleOpen(1)}>
<AccordionHeader>What is Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
<Accordion open={open === 2} onClick={() => handleOpen(2)}>
<AccordionHeader>How to use Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
<Accordion open={open === 3} onClick={() => handleOpen(3)}>
<AccordionHeader>What can I do with Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
</Fragment>
);
}
You can modify the open/close state icon for Accordion
component using the icon
prop.
import { Fragment, useState } from "react";
import {
Accordion,
AccordionHeader,
AccordionBody,
} from "@material-tailwind/react";
function Icon({ id, open }) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
className={`${
id === open ? "rotate-180" : ""
} h-5 w-5 transition-transform`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" />
</svg>
);
}
export default function Example() {
const [open, setOpen] = useState(0);
const handleOpen = (value) => {
setOpen(open === value ? 0 : value);
};
return (
<Fragment>
<Accordion
open={open === 1}
icon={<Icon id={1} open={open} />}
onClick={() => handleOpen(1)}
>
<AccordionHeader>What is Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
<Accordion
open={open === 2}
icon={<Icon id={2} open={open} />}
onClick={() => handleOpen(2)}
>
<AccordionHeader>How to use Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
<Accordion
open={open === 3}
icon={<Icon id={3} open={open} />}
onClick={() => handleOpen(3)}
>
<AccordionHeader>What can I do with Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
</Fragment>
);
}
You can modify the open/close state animation for Accordion
component using the animate
prop.
import { useState, Fragment } from "react";
import {
Accordion,
AccordionHeader,
AccordionBody,
} from "@material-tailwind/react";
export default function Example() {
const [open, setOpen] = useState(0);
const handleOpen = (value) => {
setOpen(open === value ? 0 : value);
};
const customAnimation = {
mount: { scale: 1 },
unmount: { scale: 0.9 },
};
return (
<Fragment>
<Accordion
open={open === 1}
animate={customAnimation}
onClick={() => handleOpen(1)}
>
<AccordionHeader>What is Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at.
We're constantly growing. We're constantly making mistakes.
We're constantly trying to express ourselves and actualize our
dreams.
</AccordionBody>
</Accordion>
<Accordion
open={open === 2}
animate={customAnimation}
onClick={() => handleOpen(2)}
>
<AccordionHeader>How to use Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at.
We're constantly growing. We're constantly making mistakes.
We're constantly trying to express ourselves and actualize our
dreams.
</AccordionBody>
</Accordion>
<Accordion
open={open === 3}
animate={customAnimation}
onClick={() => handleOpen(3)}
>
<AccordionHeader>What can I do with Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at.
We're constantly growing. We're constantly making mistakes.
We're constantly trying to express ourselves and actualize our
dreams.
</AccordionBody>
</Accordion>
</Fragment>
);
}
An Accordion
could be disabled as well, it will help you to prevent user interactions like click over the Accordion
component.
import { Fragment, useState } from "react";
import {
Accordion,
AccordionHeader,
AccordionBody,
} from "@material-tailwind/react";
export default function Example() {
const [open, setOpen] = useState(0);
const handleOpen = (value) => {
setOpen(open === value ? 0 : value);
};
return (
<Fragment>
<Accordion open={open === 1} onClick={() => handleOpen(1)} disabled>
<AccordionHeader>What is Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
<Accordion open={open === 2} onClick={() => handleOpen(2)}>
<AccordionHeader>How to use Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
<Accordion open={open === 3} onClick={() => handleOpen(3)}>
<AccordionHeader>What can I do with Material Tailwind?</AccordionHeader>
<AccordionBody>
We're not always in the position that we want to be at. We're
constantly growing. We're constantly making mistakes. We're constantly
trying to express ourselves and actualize our dreams.
</AccordionBody>
</Accordion>
</Fragment>
);
}