Framer Motion (for React) #14 - Modal Animation (part 2)

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago
Skills: React90%

Key Takeaways

Builds a modal animation using Framer Motion and React

Full Transcript

okay then so now we have this backdrop showing after a few seconds and that's animating in fine it's fading in what I'd like to do now is animate in some kind of modal and I want this to drop from the top so inside this div right here this backdrop let's create another div which will also be a motion component and we're going to apply some variants to that as well so let's say motion div like so and let's also give this a class name class name and set that equal to modal now inside I'm going to do a paragraph tag first of all and we'll say want to make another pizza question mark and underneath that we'll have a link to the home page again so let's use the link component which we imported remember at the top up here from react or outer Dom and this link is going to go to forward slash that's the home page and inside this I'm just going to create a button that they can click and that will say start again or something like that doesn't really matter anyway now we have our content right here if we take a look at this after a few seconds it should show but it's not going to be styled just yet it's just looking a bit pants at the top over there so let's now add some CSS first of all to make this look at least a little bit respectable so let's go to index dot CSS and down here again I'm just going to paste these in and walk through them so remember we called this mold or this class and we're targeting that right here and we're saying the max width of this model the pop-up itself is going to be 400 pixels the margin is 0 top and bottom auto left and right and what that does is automatically assigned margin to the left and right all the available margin left on the page after it takes up this width so it means it's going to be a central modal now and it's going to sit in the middle with automatic margin left and right of it the padding of the modal is going to be 40 pixels top and bottom and 20 pixels left and right so that the content inside has some breathing room the background of the model is white border radius 10 pixels so we get soft corners and text align in the model is going to be to the center so all the text will sit in the center of it now the button inside is going to have a text color or like a deep gray and also a border call of a deep gray the font weight is bold and the margin of the button at the top is 20 pixels and the paragraph tag which we also have inside the modal that is going to be dark green color and also bold font weight so let me save this now and come over to this again and now we can see this looks a bit better so all we need to do now is animate this and what I'd like to do is animate it from off the screen up here down here so to do that let us first of all make some variants for this so I'm going to come to the top and create a new constant called button and set this equal to an object or not button sorry we're not making variants for the button it's the modal so let's set that equal to an object and inside we're going to have a hidden property first of all and this is going to have a y-coordinate to begin with of minus 100 VH so viewport height not 110 100 okay so it's going to be up right at the top up here off the screen now we also want to set the opacity to begin with to be zero so it's not going to show at all to begin with okay now we also need another property and that is going to be the visible property so visible and this object is going to say okay bring the Y position down to 200 pixels so let's say 200 pixels like so so it's gonna go from way off the screen down to 200 pixels down into the screen and also we want the opacity to be 1 like so and I'm also going to add a transition delay so I'll explain why in a second let me just first create this delay not 0.5 seconds and that's because then this will start animate first of all and start to fade out the background to this kind of deep gray color or black semi-transparent black and then not upon 5 seconds later after it starts doing this this is going to pop down okay so it's kind of a nice effect okay so we need to apply this now to this thing over here so let me say that the variants are going to be equal to modal and we need to define the initial that's going to be equal to hidden that's what we called this thing right here and in fact we don't need to do this because remember propagation and we've already defined these up here on the parents so we don't need that right here we can just leave it as so variance equal modal okay so let me try this out I'm gonna save it and come over here and we should see this all work fingers crossed okay cool so that works and that looks pretty nice now if I click this and go to start again well we get the home page but all of this modal and the backdrop still shows so we need to exit this animation now remember we did this before animate presence and we surrounded this motion div right here so that we could animate this out but remember to do that we need some kind of exit animation so let's go to this backdrop variance object and add an exit animation in fact we don't need to we can just say that the exit is going to be equal to this one right here because it's not this one sorry this one hidden because we're gonna go back to an opacity of 0 again so it doesn't show so let me first of all do that and this isn't going to be enough on its own and I'll explain why in a minute so exit is going to be equal to hidden again so that it fades back out well let me try this again let me go back to the end screen we should see the modal first of all and then start again and hmm it doesn't work so why is it not fading out again well we've not actually taken it out of the Dom we've said we've got an exit animation but this is still true so it's not actually exiting out of the Dom so what we could do is use this thing right here set show modal when we click on this button to set it to false again right and let me show you this first of all I'm going to say on click and set that equal to a function and inside this function we're going to set show modal to be false again right so let me show you this first of all there is a problem with this and we'll see that in a second but let's just try it out so let's wait for the modal and then when we click on that ok it seems to work right that's worked and we can go and create the picture again however check this out if I do this and the modal shows again and I don't click start again instead and go back to the previous page well it's still here because now we're not clicking this thing again so we can't now get back to the previous page and hide this so a better way is not to do this right here when we click on the button but every time that we have an exit transition of a route right because if we do that then no matter what route we'd go to it's gonna then set it to be false so what we could do is go to a pas instead and where we have animate presence we can add on and on exit complete prop and set this equal to B that function instead so let me do that I'm going to say set show modal right here to be false and what this does is every time an exit animation completes so every time we go to another page at that point we set shell model to be false and therefore it should be hidden from the screen as well so now it doesn't matter if we hit back because we're exiting out of your routes a component is exiting and at that point it runs that function so if I go forward again and wait for the modal first of all I'm gonna go back to the home page to see if it works and it doesn't so that's because we've not saved this page it's too pitiless I'll let me go back again to the final page wait for this modal again ok so start again and now it disappears so that works now let's try hitting the back button once the model appears ok let's go back and that works as well awesome so now we're removing the modal no matter which way we go and that is pretty much the modal complete so next up I'm gonna try and show you how to animate SVG's by animating our logo up here

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

Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →