Framer Motion (for React) #3 - Animating Elements

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

Animates elements using Framer Motion in a React application

Full Transcript

a rather than gang so the first thing that we're going to do is look at how to create some basic simple animations now to do that we'll be working here inside the home component and we're just gonna try animating the text the button and also this div which surrounds everything as well so the first thing we need to do is to import something from the frame up motion library remember we installed the frame emotion library at the start of the last tutorial and it was this library right here frame emotion that got installed when we said npm install so now we can just import something from it and that's something is motion so I'm importing motion from the frame up motion library now the way this works is that if we want to animate an element we make it into a motion elements and that basically means that we just say motion dots whatever elements it is and then if the element has a closing tag then we say motion dots at the end as well now this returns to us a new motion component which will still be an h2 when it's rendered to the Dom but now it comes with some extra abilities that we can use to animate this element so how do we do that well all we need to do is add on an animate prop so I could say animate is equal to something and it's going to be something dynamic hence the curly braces now inside this animates prop we need to pass an object and this object is going to represent the different properties of this h2 that we want to animate so for example I could say that I want to animate the font size to be 50 pixels we don't need to say pixels we can just say 50 frame emotion figures that out and that means that now when this component first loads it's going to see this animate prop and it's going to animate this to be this size so if I save this and come over here and we'll go to the home page we can see this straight away we get that animation to this font size okay so this is basically just animating a CSS property except that this is camelcase we don't use hyphens like we would in CSS we use camel case for words instead so we can pass in a lot of different CSS properties inside this object and it's going to animate each one of those so I could also animate the color which will be a hex code and I'll just say hex and this is FF 2 9 9 4 so if I save this now come over here and refresh you see animate the size and the text color alright so what if I want to move this around on the page well what I could do is use an x and a y property now these are not CSS properties we don't say x and y in a CSS rule so these are more convenience properties added to the frame emotion library for us and this basically just translates the elements to the left or the right the Y property would translate it up or down so if this is a positive number it's going to go to the right if it's a negative number to the left and if the Y is a positive number it means it's gonna go down and if it's a negative number it's going to go up so let me just test this out I'm gonna say X is 100 save that and come over here and we can see it moved to the right a little bit and if I say that Y is going to be minus 100 and save that then we should see it move up a little bit let me refresh and we can see now it animates to this position as well as the color and the font size awesome okay so let's try animating something else I'm gonna try animating the button so again remember we need to add motion in front of the button there and also in the closing tag so I'll alt click there as well and say motion dots now we get a motion button and we can add on the animate property now what I'm gonna do is put these props on the next line because they can get quite long and it makes it more readable the more props that we have so I'm gonna do that down here as well come to the next line and say animate set that equal to an object and inside here I'm going to say the scale will be 1.5 again this is not a CSS property it's a frame emotion convenience property so we can add CSS properties but also the convenience properties as well now this is going to make it 1.5 times bigger when we first load the page and we can see that right there cool okay so now let's do this div at the top again we need to do motion at the start and the end so motion dots div and the opening tag and the closing one then I'm gonna come to the next line and say animate and set it equal to an object now inside this one I'm gonna say margin top is going to be 200 so it's going to bring it down a little bit and animate that let me save it and preview now we can see go down and I'm also going to say the opacity is going to go to zero point two so it should only be very slightly visible on the screen save that and we can see now the opacity is animated as well and then finally I'm gonna add another property that is going to be rotate and then it's gonna be around the z-axis now the z axis is the axis that goes into the page and I'm going to rotate it around that by a hundred and eighty degrees so that it should be upside down so let me save that and come over here and now we can see it's rotated downwards like that so hopefully now you can see how easy it is to animate properties of our elements and I get that this isn't great and we wouldn't animate anything this way I just wanted to show you how easy it is to actually animate these different things so the important things to remember are first of all that we import motion from frame up motion then we add motion dot to whatever element that we want to add motion to if it's a self-closing tag just add it at the stances there's no closing tag then we use the animation property inside and inside that we can pass an object and that object is basically CSS property and value pairings as well as the convenience properties that we've seen right here that frame emotion ads now before we finish I'm just going to delete the h2 motion property completely because I don't actually want to animate this in the future I was just using that to demo a point so let's get all of this on one line again and I will keep the animate prop there but I'm going to delete the object inside of it so we can work from a fresh in the future and the same for the button so that now we shouldn't have any animation Oh in fact we get a problem let me just put an empty object into these it's complaining that we don't have one so let me save that okay there's no animation but in the future now we can come to these different elements and animate them

Original Description

🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/framer-motion 🐱‍💻 🐱‍💻 Other Related Free Courses: + HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + React Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG + React Hooks Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9hNokByJilPg5g9m2APUePI 🐱‍💻 🐱‍💻 Framer Motion Docs: https://www.framer.com/api/motion/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
The two-Reacts bug: when packages aren't singletons
Learn to fix the two-Reacts bug by understanding how to handle non-singleton packages in React applications
Dev.to · r9v
📰
🚀 Introducing Prism Guard — An Open Source Frontend Architecture Intelligence Platform
Learn about Prism Guard, an open-source frontend architecture intelligence platform, and how it can help improve codebase quality
Dev.to · Ritumoni Sarma
📰
The Death of the Heavy Hydration Layer
Learn why plain HTML is the new developer flex and how to simplify web development by ditching heavy hydration layers
Dev.to · Amodit Jha
📰
Stop Asking "React or Angular?" Until You Can Answer This First
Learn how to approach the React vs Angular debate by understanding the key factors that influence the choice of a frontend framework
Dev.to · Charlie
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →