Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
Animated modal using react, framer-motion & styled-components
Next article icon

Animated shared layout using framer-motion and React.js

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Animated shared layout using framer-motion and React.js involves items that toggle open and close show some animation/transitions and switch the states when clicked.

Prerequisites:

  • Node.js and NPM
  • React JS
  • React JS Hooks

Approach:

To design an Animated shared layout using framer-motion and React.js we will be using the AnimatedSharedLayout along with motion and AnimatePresence components for showing transitions and useState hook to store and render the components after state changes.

Steps to Create React Application And Installing Module :

Step 1: Create a React application using the following command:

npx create-react-application demo

Step 2: After creating your project folder i.e. demo, move to it using the following command

cd animated-layout

Step 3: Add the npm packages you will need during the project :

npm i framer-motion

Project Structure:

Folder structure

The updated dependencies in packages.json file.

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"framer-motion": "^10.16.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example: This example uses AnimatedSharedLayout and AnimatePresence components to implement the Animated shared layout.

CSS
/* Filename - styles.css */  body {     min-height: 100vh;     margin: 0;     display: flex;     justify-content: center;     align-items: center; }  * {     box-sizing: border-box; }  ul, li {     list-style: none;     margin: 0;     padding: 0; }  ul {     width: 300px;     display: flex;     flex-direction: column;     background: #fcfcfc;     padding: 20px;     border-radius: 25px; }  li {     background-color: rgba(214, 214, 214, 0.5);     border-radius: 10px;     padding: 20px;     margin-bottom: 20px;     overflow: hidden;     cursor: pointer;     width: 300px; }  li:last-child {     margin-bottom: 0px; }  .avatar {     width: 40px;     height: 40px;     border-radius: 20px; }  .avatar img {     width: 40px;     border-radius: 100%; }  .row {     margin-top: 12px; }  img {     width: 250px;     height: 40px; } 
JavaScript
// Filename - App.js  import React from "react"; import { AnimateSharedLayout } from "framer-motion"; import Item from "./Item"; import "./styles.css";  // This is an example of animating shared layouts // using react and framer-motion library. const itemsList = [     {         index: 0,         content: `Motion components are DOM primitives    optimised for 60fps animation and gestures.`,     },     {         index: 1,         content: `Motion can animate:     Numbers: 0, 10 etc.     Strings containing numbers: "0vh", "10px" etc.`,     },     {         index: 2,         content: `Transform properties are accelerated by the GPU,      and therefore animate smoothly. `,     }, ];  const App = () => {     return (         // The framer-motion component to wrap Item component to animate it         <AnimateSharedLayout>             {/* Mapping through itemList array to render layouts*/}             {itemsList.map((item) => (                 <Item                     key={item.index}                     content={item.content}                 />             ))}         </AnimateSharedLayout>     ); };  export default App; 
JavaScript
// Filename - Item.js  import React, { useState } from "react"; import { motion, AnimatePresence } from "framer-motion";  const Content = ({ content }) => {    const url = "https://media.geeksforgeeks.org/wp-content/cdn-uploads/" +     "20200817185016/gfg_complete_logo_2x-min.png"    return (     <motion.div       layout       initial={{ opacity: 0 }}       animate={{ opacity: 1 }}       exit={{ opacity: 0 }}     >       <img         src={url}         alt="geeksforgeeks"       />       <div className="row">{content}</div>     </motion.div>   ); };  const Item = ({ content }) => {   // React useState hook is used to manage the state of 'isOpen'   // that in turn toggles shared layout, user clicks on   const [isOpen, setIsOpen] = useState(false);    // Utility function to set 'isOpen' '!'(not) of its last value   const toggleOpen = () => setIsOpen(!isOpen);    const url = "https://yt3.ggpht.com/ytc/AAUvwnjJqZG9PvGfC3GoV" +     "27UlohMeBLxyUdhs9hUbc-Agw=s900-c-k-c0x00ffffff-no-rj"    return (     <motion.li       layout       title="Click to reveal"       onClick={toggleOpen}       initial={{ borderRadius: [25] }}     >       <motion.div className="avatar" layout>         {" "}         <img           src={url}           alt="gfg"         />{" "}       </motion.div>       <br />       <AnimatePresence>{isOpen && <Content content={content} />}       </AnimatePresence>     </motion.li>   ); };  export default Item; 

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:


Next Article
Animated modal using react, framer-motion & styled-components
author
jt9999709701
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • CSS
  • ReactJS
  • React-Questions
  • Framer-motion

Similar Reads

  • Animated sliding page gallery using framer-motion and React.js
    The following approach covers how to create an animated sliding page gallery using framer-motion and ReactJS. Prerequisites: Knowledge of JavaScript (ES6)Knowledge of HTML and CSS.Basic knowledge of ReactJS.Creating React Application And Installing Module: Step 1: Create a React application using th
    5 min read
  • Animated sliding image gallery using framer and ReactJS
    Animated sliding image gallery using Framer and React JS will have some image and show one by one by one with a sliding animation from right to left. Prerequisites:Node.js and NPM React JSReact JS HooksApproach:To design an animated sliding image gallery using Framer in React we will be using the Pa
    2 min read
  • Animated expanding card using framer-motion and ReactJS
    In this article, we are going to learn how to create an animated expanding card using react and framer. Prerequisites: Knowledge of JavaScript (ES6). JavaScript inbuilt methods we are going to make use are :Arrow function (ES6)Ternary operatorObjects in JavaScriptKnowledge of HTML/CSS.Basic knowledg
    4 min read
  • Animated modal using react, framer-motion & styled-components
    In this article, we are going to learn how to create an animated model using react, framer-motion & styled-components. PrerequisitesJavaScript (ES6).HTMLCSSReactJS. React useStateSteps to Create React Application And Installing Module:Step 1: Now, you will start a new project using create-react-
    4 min read
  • Design an Animated Toggle Switch Button using framer-motion & React
    Animated Toggle Switch Button using framer-motion & React is a button that shows some animation/transitions and switches the states when clicked. Prerequisites:Node.js and NPM React JSReact JS HooksApproach:To design an animated toggle switch button using framer motion in react we will be using
    3 min read
  • How to create Tinder card swipe gesture using React and framer-motion ?
    In the world of modern dating, Tinder has become one of the most popular platforms for meeting new people. One of its defining features is the swipe gesture, where users can swipe left or right to indicate their interest or disinterest in a potential match. In this article, we'll explore how to crea
    5 min read
  • How to create a rolling die using React and framer-motion ?
    We can create a die using react with plain CSS and framer-motion library for animating, Framer Motion library helps to animate the UI elements. Prerequisites:JavaScriptHTMLCSSReactJSSteps to Create the React Application And Installing Module:Step 1: Create a React application using the following com
    3 min read
  • Create Form Layouts using React and Tailwind CSS
    We will create a responsive form layout using React and Tailwind CSS. We will design the form with input fields, buttons, and icons to create a modern UI. This form layout can be used in various applications such as login pages sign-up forms and contact forms. Forms are essential components of web a
    4 min read
  • Create Flyout Menus using React and Tailwind CSS
    Flyout menus are a type of navigational menu that can be displayed when the user hovers over or clicks on an item allowing for a clean and organized display of additional options without crowding the main interface. In this article, we will create a responsive flyout menu using React and Tailwind CS
    4 min read
  • How to reorder list of items using Framer Motion in ReactJS?
    To reorder the list of items using Framer Motion in ReactJS we will use a dummy list and the reorder component that enables reordering of items on user interaction. Prerequisites:NPM & Node.jsReactJSIntroduction of Framer-motionApproachWe can reorder a list of items using the Framer motion libra
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences