From f3dc6249c0805df93c91ba5559ef7bffc4641a14 Mon Sep 17 00:00:00 2001 From: Jenish Dhanani <202112106@daiict.ac.in> Date: Mon, 16 Oct 2023 23:55:55 +0530 Subject: [PATCH] Added framer motion animation to accordion open and close --- src/components/Accordion.css | 89 +++++++++++++++--------------- src/components/Accordion.js | 102 ++++++++++++++++++++++++++--------- 2 files changed, 122 insertions(+), 69 deletions(-) diff --git a/src/components/Accordion.css b/src/components/Accordion.css index d207b98..e95f863 100644 --- a/src/components/Accordion.css +++ b/src/components/Accordion.css @@ -1,47 +1,46 @@ .accordion { - - overflow: hidden; - width: 100%; - } - - .accordion-item { - border-bottom: 1px solid #ddd; - margin: 8px 0px; - border: 1px solid #ddd; - border-radius: 4px; - } - .expand{ - width: 100%; - max-width: 15px; - } - .accordion-title { - display: flex; - justify-content: space-between; - align-items: center; - padding: 15px; - cursor: pointer; - background-color: #f5f5f5; - border-bottom: 1px solid #ddd; - - } - - .accordion-title:hover { - background-color: #e0e0e0; - } - - .accordion-title.active { - background-color: #ddd; - } - - .accordion-content { - padding: 15px; - background-color: lightcyan; - } + overflow: hidden; + width: 100%; +} - .expand { - transition: transform 0.3s ease-in-out; - } - - .rotate { - transform: rotate(180deg); - } \ No newline at end of file +.accordion-item { + border-bottom: 1px solid #ddd; + margin: 8px 0px; + border: 1px solid #ddd; + border-radius: 4px; + overflow: hidden; +} +.expand { + width: 100%; + max-width: 15px; +} +.accordion-title { + display: flex; + justify-content: space-between; + align-items: center; + padding: 15px; + cursor: pointer; + background-color: #f5f5f5; + border-bottom: 1px solid #ddd; +} + +.accordion-title:hover { + background-color: #e0e0e0; +} + +.accordion-title.active { + background-color: #ddd; +} + +.accordion-content { + padding: 15px; + background-color: lightcyan; +} + +.expand { + transition: transform 0.3s ease-in-out; +} + +.rotate { + transform: rotate(180deg); +} diff --git a/src/components/Accordion.js b/src/components/Accordion.js index 57c7978..13b5038 100644 --- a/src/components/Accordion.js +++ b/src/components/Accordion.js @@ -1,32 +1,86 @@ +// Import necessary React components and styling import React, { useState } from 'react'; -import "./Accordion.css"; -import Expand from '../assets/expand_more.png' -export default function Accordion({items}) { +import { motion, AnimatePresence } from 'framer-motion'; +import './Accordion.css'; +import Expand from '../assets/expand_more.png'; + +// Individual Accordion Item component +const AccordionItem = ({ item, index, isActive, onClick }) => { + // Destructure title and content from item + const { title, content } = item; + + return ( + // Use Framer Motion for layout animation + + {/* Title section with onClick handler */} + onClick(index)} + > + {title} + {/* Expand icon with rotation animation */} + + + {/* AnimatePresence for handling enter/exit animations */} + + {/* Container for content with background color animation */} + + + {/* Content animation when item is active */} + {isActive && ( + + {/* Actual content of the accordion item */} +
{content}
+
+ )} +
+
+
+
+ ); +}; + +// Main Accordion component that maps over items +const Accordion = ({ items }) => { + // State to track the active index const [activeIndex, setActiveIndex] = useState(null); + // Handle click to toggle active index const handleClick = (index) => { setActiveIndex(activeIndex === index ? null : index); }; + return ( -
- {items.map((item, index) => ( -
-
handleClick(index)} - > - {item.title} - expand -
- {activeIndex === index && ( -
{item.content}
- )} -
- ))} -
+ // Container for the entire accordion +
+ {/* Map over items and render AccordionItem for each */} + {items.map((item, index) => ( + + ))} +
); -} +}; + +// Export the Accordion component as the default export +export default Accordion;