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 shared layout using framer-motion and React.js
Next article icon

Animated expanding card using framer-motion and ReactJS

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

In this article, we are going to learn how to create an animated expanding card using react and framer.

Prerequisites:

  1. Knowledge of JavaScript (ES6). JavaScript inbuilt methods we are going to make use are :
    1. Arrow function (ES6)
    2. Ternary operator
    3. Objects in JavaScript
  2. Knowledge of HTML/CSS.
  3. Basic knowledge of ReactJS

React hooks used in building this application are:

  1. React useState

Framer: components and hooks we are going to make use of in this tutorial are :

  1. https://www.framer.com/api/frame/
  2. https://www.framer.com/api/scroll/
  3. https://www.framer.com/api/utilities/#useanimation

Creating React Application And Installing Module :

Step 1: Now, you will start a new project using create-react-app so open your terminal and type.

$ npx create-react-app animated-card

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

$ cd animated-card

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

$ npm install framer react-icons
// For yarn
$ yarn add framer react-icons

Open the src folder and delete the following files :

  1. logo.svg
  2. serviceWorker.js
  3. setupTests.js
  4. App.css
  5. App.js
  6. App.test.js (if any)

Create a file named Card.js.

Project structure: Your project structure tree should look like this :

Project structure

Example: 

index.js
import React from "react"; import { Frame, Scroll } from "framer"; import Card from "./Card"; import ReactDOM from "react-dom";  import "./index.css";  // main App HOC export const App = () => {   return (     <Frame height={"100%"} width={"100%"} background={"#333"}>       <Frame width={375} height={"100%"} background={"#FFF"} center>         <Scroll width={375} height={"100%"}>           {/* Card component with props yPos,title,subtitle */}           <Card             yPos={48}             title={"GEEKSFORGEEKS"}             subtitle="Don't learn alone"           />           <Card             yPos={48 + 300 + 24}             title={"reactJS"}             subtitle="Most starred JavaScript library"           />         </Scroll>       </Frame>     </Frame>   ); };  const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement); 
index.css
body {   margin: 0;   cursor: pointer; } 
Card.js
import React, { useState } from "react"; import { ImCross } from "react-icons/im"; import { Frame, Scroll, useAnimation } from "framer";  // Card component with destructured props : // yPos, title, subtitle const Card = ({ yPos, title, subtitle }) => {    // useState hook to manage the state of   // expanding of card   const [state, setState] = useState(false);    // utility function to handle   // onTap on card component   const handleTap = () => {     state ? controls.start({ y: 0 }) : setState(!state);   };    const controls = useAnimation();    // Variants allow you to define animation   // states and organise them by name.   // They allow you to control animations    // throughout a component   // tree by switching a single animate prop.   const variants = {     active: {       width: 320,       height: 800,       borderRadius: 0,       overflow: "visible",       left: 28,       right:0,       y: 0,       transition: { duration: 0.125,                      type: "spring",                      damping: 10,                      mass: 0.6 }     },     inactive: {       width: 280,       height: 280,       borderRadius: 24,       overflow: "hidden",       left: 45,       y: yPos,       transition: { duration: 0.125,                      type: "spring",                      damping: 10,                     mass: 0.6 }     }   };    return (     // basic container for layout, styling,     // animation and events.     <Frame       y={yPos}       variants={variants}       animate={state ? "active" : "inactive"}       width={300}       height={300}       borderRadius={24}       style={state ? { zIndex: 10 } : { zIndex: 1 }}       left={37.5}       onTap={handleTap}       shadow={         state           ? "0 0 0 0 rgba(0, 0, 0, 0)"           : "0px 0px 20px 0px rgba(0, 0, 0, .25)"       }     >       <Scroll         width="100%"         height="100%"         backgroundColor={null}         scrollAnimate={controls}       >         <Frame           position="relative"           backgroundColor={"#09a960"}           width="100%"           height={300}         />         <Frame position="relative"                 height={1200}                 background="white" />          <Frame           top={20}           left={20}           height={""}           width={""}           background={null}           style={{             color: "white",             fontFamily: "sans-serif"           }}         >           <span style={{ fontSize: "1.6em",                           fontWeight: 600 }}>                         {title}           </span>           <br />           <span             style={{               fontSize: "1em",               fontWeight: 500,               opacity: 0.5             }}           >             {subtitle}           </span>         </Frame>       </Scroll>       {state && (         <Frame           borderRadius={20}           size={15}           top={15}           right={20}           backgroundColor={"#09a960"}           onTap={() => {             setState(false);           }}         >           <ImCross color="red" />         </Frame>       )}     </Frame>   ); };  export default Card; 

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:

https://media.geeksforgeeks.org/wp-content/uploads/20240723203310/ezgifcomgifmaker13.mp4


Next Article
Animated shared layout using framer-motion and React.js
author
jt9999709701
Improve
Article Tags :
  • CSS
  • JavaScript
  • ReactJS
  • Web Technologies
  • CSS-Properties
  • Framer-motion

Similar Reads

  • 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
  • Animated shared layout using framer-motion and React.js
    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 NPMReact JSReact JS HooksApproach:To design an Animated shared layout using framer-motion and React.js we wi
    3 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
  • 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
  • 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
  • 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
  • Design a Flip Card Effect using ReactJS
    Design a Flip Card Effect using ReactJS refers to adding flip animations to the react component. The flip card effect is where a card flips to reveal its backside when clicked or hovered over. PrerequisitesReact JSNode.js & NPMTable of Content Approach 1: Using CSS stylingApproach 2: Using react
    4 min read
  • Build a Flip the Card & Match Done Game using React
    This application will provide users with the ability to play a fun and interactive game where they need to flip cards and match pairs with the same value. Along the way, we'll implement features like keeping track of the player's score, displaying a high score, and allowing the user to reset the gam
    5 min read
  • Movie Search Engine Using React and API
    In this article, we will create Movie Search Engine using ReactJS. In this movie search engine the user can search for movies which they want to know about and the search engine will fetch a list of movies from that keyword and display the result in the forms of card. A default search has been loade
    6 min read
  • How to create a Location finder app using ReactJS ?
    In this article, we will be building a location finder app that lets you search for different places on a map. Our app contains two sections, one for displaying the marker on the map and the other is for searching different places. For rendering the map and getting the coordinates for the searched l
    4 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