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