Accordion
Why dump all your content on the screen when you can hide it in style? Our accordion lets you tuck away details neatly until they’re needed—like a treasure chest waiting to be opened! Perfect for FAQs, docs, or any content that deserves a dramatic reveal.
Preview
Why use stackbits?
Follow below steps 👇🏻
Install dependencies
1npm i framer-motion lucide-react
Component
Create a file accordion.tsx in your components folder and paste this code
1import React from 'react';2import { motion } from 'framer-motion';3import { Plus } from 'lucide-react';45type AccordionProps = {6isOpen: boolean;7selectAccordion: (index: number) => void;8item: {9title: string;10content: string;11};12index: number;13toggleIcon?: React.ReactNode;14};1516const Accordion = ({17isOpen = false,18selectAccordion,19item,20index,21toggleIcon22}: AccordionProps) => {23return (24<motion.div25initial={{26height: '60px',27background: 'linear-gradient(90deg, rgba(38, 38, 66, 1) 59%, rgba(69, 67, 89, 1) 100%)'28}}29animate={{30height: isOpen ? 'auto' : '60px', // ? height of the accordion increases when it is open31background: isOpen32? 'linear-gradient(90deg, rgba(255, 99, 71, 1) 50%, rgba(255, 165, 0, 0.8) 100%)'33: 'linear-gradient(90deg, rgba(38, 38, 66, 1) 59%, rgba(69, 67, 89, 1) 100%)'34}}35className="shadow-lg rounded-xl overflow-hidden border border-gray-300 w-full relative"36>37<button38className="text-sm sm:text-[16px] w-full flex justify-between items-center p-5 h-[60px] text-left font-semibold text-white"39onClick={() => selectAccordion(index)}40>41{item.title}42{toggleIcon || (43<motion.div animate={{ rotate: isOpen ? 135 : 0 }}>44<Plus className="text-white h-4 w-4" />45</motion.div>46)}47</button>4849<div className="px-5 pb-5 text-white overflow-hidden text-xs sm:text-[16px]">50{item.content}51</div>52</motion.div>53);54};5556export default Accordion;
Usage
1import { useState } from 'react';23import Accordion from './ui/accordion';45// ? Content of the accordions6const accordionItems = [7{8title: '📜 No Boring Documentation',9content:10'Our docs are actually *fun* to read. No 10,000-word essays—just quick, clear explanations so you can get back to building cool stuff.'11},12{13title: '⚡ Copy. Paste. Ship.',14content:15'Other devs *write* code. You? You *assemble* greatness. With StackBits, you get copy-paste-ready snippets that actually work (yes, really).'16},17{18title: '🛠️ Backend Included',19content:20'Think we just give you UI components? Nah. We also include backend logic, so you don’t have to keep Googling “how to hash a password securely.”'21}22];2324const AccordionDemo = () => {25const [expanded, setExpanded] = useState<number | null>(null);2627// ? Toggles the accordion28const toggleAccordion = (index: number) => {29setExpanded(expanded === index ? null : index);30};3132return (33<div className="max-w-md w-full mx-auto p-6 space-y-4">34<p>Why use stackbits?</p>35{accordionItems.map((item, index) => (36<Accordion37key={index}38isOpen={expanded === index}39selectAccordion={toggleAccordion}40item={item}41index={index}42// toggleIcon={<ChevronDown className="text-white h-4 w-4" />} // ? Optional icon to toggle the accordion43/>44))}45</div>46);47};4849export default AccordionDemo;
⭐️ Got a question or feedback?
Feel free to reach out!