Can You Make A Jesture Toggle An Animation
How to Create a Toggle Animation with Framer Motion and React
An article on how to create your ain animated toggle using Framer Motility with React.
Contents
- Intro
- Framer Motion
- Getting Started + Starter Files
- Gestures
- Animate + Transition
- onTap()
- Color Transition
- Solution
Intro
In this commodity + guide nosotros will cover all the necessary steps, knowledge, and techniques required to create an blithe toggle. This will include a smooth transition from ane side of the toggle to the other, to even updating the color of the toggle depending on the position.
Framer Motility
Permit'south talk a petty about Framer Motion and what it is, Framer Motion provides a simple API that makes transitions and animations simple nevertheless powerful. Since information technology makes things so easy you can do some boggling things with it and actually take animations to the adjacent level.
If this is your first experience with Framer Motion it's not required but I recommend going through my article "How to Use Framer Motion with React" first to understand more of the basics. Link at the lesser.
Getting Started
Let'south get to it! Beginning, we need to install our dependencies into our React project.
Framer-Move requires that you are using React version sixteen.8 or greater.
If you want to follow forth exactly like me clone my starter files. This includes the toggle element without any animations.
Starter Files:
git clone https://github.com/StevenCreates/framer-motion-toggle-starter-files.git
The starter files volition give you this toggle (see prototype) that isn't operation withal. That's where Framer-Motion and the tools I give you lot come into play.
If you aren't using my starter files you volition demand to get your react project gear up with Framer Move. Nonetheless, if you lot are using my starter files you can now motility down to the Gestures section of the article.
Inside your react app run the following command to install our framer motion dependency.
npm install --relieve framer-motility
At present that it is installed, you can import motion into your components via:
import { motility } from "framer-motion"
I will also be using styled-components to make the layout of my page prettier. Now, this isn't required yous tin easily add the CSS in a cascading mode sheet. Simply if you'd similar to have the same code as I get ahead and install styled components into your project.
npm install --save styled-components
Perfect your React project at present has everything information technology needs to become to coding and animating.
Gestures
Gestures are how the user interacts with your webpage or app. An easy caption of a gesture is when a user is on your webpage and hovers over an chemical element on the page you have your code do something when they hover over that element. Like change the color of the background or testify a tooltip.
Framer Movement works seamlessly with Reacts events and supports an array of gestures (hover, tap, pan, and drag).
We will be using the tap (click) gesture to animate our toggle. When the user taps (clicks) our toggle we volition capture that event and animate our toggle.
Animate + Transition
Now we could create a toggle that just moves from one side to the other, but we desire one that feels alive and that brings an experience.
I've talked about Blitheness and Transition in my other articles, but allow'southward bear upon on it again if this is your first Framer-Motion article by me.
Animations
Animations are when you accept an element that is brought to life and moves in a way that follows the laws of physics.
Framer Motility makes animations simple equally passing animate props to your JSX elements. Once given these props, framer motion volition generate the blitheness for you lot.
Transitions
Transitions happen while the animation is happening. This can really make or intermission your animations.
If you don't like the default animations from framer move, you can pass your element or SVG the transition prop and customize it.
Let'south add some of these to our toggle to get it to menstruum from left to right in a articulate toggle-similar fashion.
Starting with animate, I'm going to tell it to move on the x-centrality 20.
If we but add the animate with the 10-axis you can see how fast it moves from left to right. Not really the animation nosotros are wanting however.
To fix that we volition demand to modify the transition, as talked virtually before transitions are the menstruum of fourth dimension when an animation is happening. Add the following lawmaking and see how nosotros've really brought information technology to life.
To break down what we are doing in the transition let's take a look at each prop we are passing it.
Spring
The type: "jump"
adds stimulating physic and realistic motion to animations.
Framer-Motion defaults its animations for concrete values like ten
, y
, scale
and rotate
.
Stiffness
Adding stiffness will affect the spring motion with a more than sudden movement depending on how loftier the value is.
Default it is set to 100.
Damping
Damping provides resistance or strength of the opposing forcefulness (substantially slowing it downwards). If damping is fix to 0, spring volition oscillate indefinitely, and Framer-Motility sets it to 10 equally default.
I highly recommend you messing with all of these to get a full understanding of what is happening. As well as making it your own.
onTap()
onTap(issue, info): void
At present that we have our toggle animating, the upshot is that it'due south animating on load and zilch is happening to it when nosotros click. Allow'south set that with Framer-Motions onTap() prop.
This prop provides a Callback when the tap gesture successfully ends on an chemical element. Substantially the same thing as an onClick().
To attain the onTap() and animating the toggle we but created we volition need to practice a few things.
Kickoff, let's utilize the React claw useState to hold the current position value.
So let's create a function that when chosen information technology updates the position value from 0 to twenty and vice versa.
Now we need to utilise our onTap() prop that we just learned about and pass information technology the toggleOn() office we just created to both <ToggleBackground> and <ToggleCircle> the reason for doing both is I want no affair where at on the toggle yous click information technology animates.
Last but not least we now demand to change our animate prop to use the new useState value toggleDirection
instead of the 20, we were passing it earlier.
Congrats our toggle is now animative onTap/onClick!
Color Transition
Now, this step isn't required, if yous're happy with how the toggle is now, that's awesome! But I want to add together in simply a little actress flair.
At present that nosotros accept our toggleDirection value nosotros tin can utilize this in other aspects of our toggle.
I'm going to use the style prop and ternary to accomplish this.
Now when the toggle is clicked, depending on the toggleDirection the colour will change.
Solution
This is how your toggle should now be functioning!
If you missed any of the code here is the concluding file! Happy coding friends!
Thanks for reading "How to Create a Toggle Blitheness with Framer Move and React".
Bank check out my other articles on Framer Move:
Thanks again for reading!
More content at plainenglish.io
Source: https://javascript.plainenglish.io/how-to-create-a-toggle-animation-with-framer-motion-and-react-20ac3dae110f
Posted by: elkinsextur1962.blogspot.com
0 Response to "Can You Make A Jesture Toggle An Animation"
Post a Comment