1676354520
Animations on web pages involve moving elements on the screen to improve the visual experience of users. These animations are visually appealing and tend to draw users’ attention to buttons, images, and other important information. Overall, it contributes to establishing a solid connection between users and the content on the screen.
In the past, web pages were very plain and lacked interactivity because they were only intended to display information, and there was no capability to create appealing visual experiences. Things have changed dramatically in recent years, thanks to the availability of libraries and packages that make it easier to animate and add visual experiences to your web pages.
This article will teach you how to use the Framer Motion library to animate a React application. Why should you use Framer Motion? And how it works by animating a mini-project.
Framer Motion is an open-source production-ready motion library for React. It allows you to add all forms of animations and transitions directly to Document Object Model (DOM) elements defined in your React components. Its syntax is easy to understand, and with a few lines of code, you will be able to implement awesome animations.
Framer Motion uses an awesome API that gives you access to the motion component. You can plug this component into your React DOM elements giving you access to additional props for animation. For example, the div
element will become motion.div
.
The Framer Motion also has beginner-friendly documentation with lots of practical examples and illustrations showing how to perform simple to complex forms of animations and transitions.
Framer Motion uses the motion
component, which you can attach to any DOM element to implement an animation or transition effect.
<motion.div>
<p> An animated text </p>
</motion.div>
Code language: HTML, XML (xml)
Nothing changes when you attach the motion
component to your element. Instead, it supercharges that element with animation capabilities, giving you access to props, keys, and values you can use to add any animation. For example, you can access the animate
prop, which takes in a dynamic value — an object which contains the different properties and values you want to animate.
<motion.div animate={{x: -10, y: 10}}>
<p> An animated text </p>
</motion.div>
Code language: HTML, XML (xml)
The above animation will cause the div
element to slide 10px
to the left and then 10px
to the bottom when it loads from the original position specified in style.
When units are not specified, calculations are done in pixels. You can also specify your preferred units by attaching them. For example:
animate={{x:"-10rem"}}
.
Framer Motion is a popular library with over 1.6 million weekly downloads. You can install the library into your React project using the command below in your terminal:
$ npm i framer-motion
Once you have installed it, you can verify if it has been installed in your package.json
file’s dependencies
object:
"dependencies": {
// ...
"framer-motion": "^7.5.3",
"react": "^18.2.0",
// ...
},
Code language: JSON / JSON with Comments (json)
Once you have installed the Framer Motion library, you can import the motion
component into a component you wish to use within your project, and everything will work fine.
import { motion } from 'framer-motion';
<motion.h1 animate={{x: 10, y: 20}}>
An animated text
</motion.h1>
Code language: JavaScript (javascript)
The motion
component can be applied to any DOM element within your React component. You can apply it to the container elements, headings, text elements, images, and other elements. This motion component gives you access to props like animate
, which you can use to adjust the color, size, position, display, and add other animation effects to the DOM element.
<motion.div
animate={{
x: 10,
backgroundColor: '#fff',
boxShadow: '10px 10px 0 rgba(0, 0, 0, 0.2)',
}}
>
<h1>Hi</h1>
<img src={myImage} alt="" />
</motion.div>
Code language: HTML, XML (xml)
In the example above, the background color changes to white. A box shadow is added to the div
when it loads, and the elements contained in the div
moves 10px
to the right.
It is also important to know that you can apply any form of animation based on CSS attributes. For example, you use x
, y
, and z
to move elements similar to translateX
, translateY
, and translateZ
.
You can also scale the size of an element using the scale
attribute for both x
and y
or scaleX
and scaleY
if you wish to apply separate values. Example:
<motion.div animate={{ scale: 0.8 }} >
<h1>Hi</h1>
<motion.img animate={{ scaleX: 1.2 }} src={myImage} alt="" />
</motion.div>
Code language: HTML, XML (xml)
The same applies to transformation values like skew
and rotate
. It’s also important to mention that it supports all forms of value types, such as numbers, strings, and colors in Hex, RGB, and HSLA as strings. You can also calculate values using the calc()
function.
<motion.img animate={{ x: "calc(100vw - 50%)" }} src={myImage} alt="" />
Code language: HTML, XML (xml)
At this point, you have only used the animate
prop, which makes you animate elements from the default CSS style to your specified values. But this is not always what you want; sometimes, you want to specify where your animation starts and ends. For example, you might want your text to slide from the left of your screen to the middle.
You can specify initial animation values using the initial
props, which work similarly to the animate
prop and uses similar attributes and values.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)
You will notice that these animations happen fast to the extent that you might not see them unless you pay close attention.
The page animates a block of Lorem ipsum text by indenting it and reverting it to its original position.
You can fix this by adding transition effects using the transition
prop. This transition
prop defines the type of animation used when animating between two values and how these values animate from one state to another.
For example, if you add a duration
of 1
, the animation will run for 1 second.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
transition={{ duration: 1 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)The page animates a block of Lorem ipsum text for a second by indenting it and reverting it to its original position.
There are three major transition types, and they all have their specific effect; they include Tween
, Spring
, or Inertia
. Tween
is the default type for all animation, so if you want a spring form of animation, you will need to specify the transition type as Spring
.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
transition={{ type: 'spring', duration: 1 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)
Each type has some attributes peculiar to them. For example, when you use the transition type of spring
, you have access to an attribute like bounce
which specifies how elastic the spring effect is on the element.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
transition={{ type: 'spring', bounce: 0.6 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)Spring animation.
There are other attributes like delay
, which you can use to set a delay time before the animation renders.
Sometimes, you may want your elements to animate through several different animation values, which may be from left to right, depending on what you want. In CSS, this is handled with keyframes
, but for Framer Motion, you can do this with a square bracket holding as many values as you want.
<motion.button
animate={{ x: [0, -20, 20, -20, 20, 0] }}
transition={{ delay: 1 }}
>
My Button
</motion.button>
The page animates a button to the left after a second from the initial render.
Note that in the example above, the animation waited for 1 second before moving the button to the left 20px
and right 20px
twice before returning to its original position. This can be applied to any animation transform attribute such as x,
y
, scale
, rotate
and others.
<motion.button
animate={{ scale: [1, 1.2, 1, 1.2, 1] }}
transition={{ delay: 1 }}
>
My Button
</motion.button>
Code language: HTML, XML (xml)
You will notice that these animations stop after it has completed the number of times specified. The example above will start at 1, which is the default. It will then scale up and down twice to its default value.
However, you may want the animation effect to repeat by using the transition
property yoyo
and set its value to Infinity
or the number of times you wish your animation repeats.
The transition property
yoyo
lets you specify the number of times you want an animation to repeat.
<motion.button
animate={{ scale: 1.2 }}
transition={{ delay: 1, yoyo: Infinity }}
>
My Button
</motion.button>
Code language: HTML, XML (xml)
In the code block above, you will notice you no longer need the square bracket. All you need is the value it should change to when it leaves the default, 1
.
The page animates a Generate Quote button once the button is hovered on.
You often want to apply hover gestures to buttons, cards, and other elements to draw the user’s attention. You might want a button to scale up and down when a user hovers over the button instead of just scaling up and down without any action from the user. To do this, you can use the whileHover
prop, which works similarly to previous props.
<motion.button
whileHover={{
scale: 1.2,
transition: { yoyo: Infinity },
}}
>
My Button
</motion.button>
Code language: HTML, XML (xml)
The transition property for
whileHover
prop is placed within the curly braces and not outside thewhileHover
prop.
The page animation transitions once the Generate Quote button is hovered on.
At this point, you have learned the basics of Framer Motion, how it works and how you could use it to implement animations directly on your DOM elements. You must have started asking yourself why you should add all animation properties and values directly on each element because it tends to make your code clumsy and not easy to understand.
This is where variants come in. You use variants to remove the props object and place them outside your component, so you only use the object names within your DOM element. This also helps you use the same animation effect for multiple elements within your component to avoid repetition.
import { Link } from 'react-router-dom';
import logo from './../images/logo.svg';
import { motion } from 'framer-motion';
const headerVariants = {
initial: {
y: '-100vh',
},
animate: {
y: 0,
transition: {
delay: 0.5,
duration: 0.5,
type: 'spring',
},
},
};
const Header = () => {
return (
<div className="container">
<motion.div
className="header"
variants={headerVariants}
initial="initial"
animate="animate"
>
<Link to="/">
<img src={logo} alt="" />
</Link>
<h1>Quotes Circle</h1>
</motion.div>
</div>
);
};
export default Header;
Code language: JavaScript (javascript)
In the code block above, an object (variant) is created to hold all the animation properties and values. This object contained two child objects, one to hold the initial
animation and the second to hold the final animation (animate
). You can give them any name:
const headerVariants = {
initial: {
y: '-100vh',
},
animate: {
y: 0,
transition: {
delay: 0.5,
duration: 0.5,
type: 'spring',
},
},
};
Code language: JavaScript (javascript)
To make use of this variant in your React DOM elements, then you will use the variants
prop and pass the variant name into it. Also, you will assign the initial
and animate
prop with the name you used when creating your variant, but this time as a string and not a dynamic value.
<motion.div
variants={headerVariants}
initial="initial"
animate="animate"
>
<Link to="/">
<img src={logo} alt="" />
</Link>
<h1>Quotes Circle</h1>
</motion.div>
Code language: HTML, XML (xml)
You can have as many variants as possible, and if you use the same initial
and animate
value, you would not need to declare that for children elements in your component.
const headerVariants = {
initial: {
y: '-100vh',
},
animate: {
y: 0,
transition: {
delay: 0.5,
duration: 0.5,
type: 'spring',
},
},
};
const logoVariants = {
initial: {
x: '-100vw',
},
animate: {
x: 0,
transition: {
delay: 1,
},
},
};
const Header = () => {
return (
<div className="container">
<motion.div
className="header"
variants={headerVariants}
initial="initial"
animate="animate"
>
<Link to="/">
<motion.img src={logo} variants={logoVariants} />
</Link>
<h1>Quotes Circle</h1>
</motion.div>
</div>
);
};
Code language: JavaScript (javascript)
If you look at the logo image element, you will notice the variants
prop was only defined. There is no need to define the initial
and animate
prop because it has the same name as the previous one you already declared in the parent element.
When building applications that have more than one page, you will want to add animation between each page. These animations may not be conspicuous but can add a nice experience rather than having content suddenly leave the screen.
The page animates a new component once the route changes.
The GIF above almost looks like the routes are animated, but you will notice that it only animates in but not out. This means that the components disappear when they unmount, so you will want to add an animation instead.
This is done with the AnimatePresence
component, which notifies components when they will be unmounted and allows them to defer that unmounting until after the animation is complete. This animation is added with the exit prop to your component’s parent element.
To implement this, you will first import AnimatePresence
component to the App.js
file where you configured your routes or if it’s in the index.js
file. Once that is done, you will wrap the Routes
component with the AnimatePresence
component:
import { Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import QuotesPage from './pages/QuotesPage';
import Header from './components/Header';
import { AnimatePresence } from 'framer-motion';
const App = () => {
return (
<>
<Header />
<AnimatePresence>
<Routes location={location} key={location.key}>
<Route path="/" element={<Home />} />
<Route path="/quote" element={<QuotesPage />} />
</Routes>
</AnimatePresence>
</>
);
};
export default App;
Code language: JavaScript (javascript)
Then you will need to use useLocation
router hook to know when the route changes. This will be attached to the Route
component:
import { Routes, Route, useLocation } from 'react-router-dom';
import Home from './pages/Home';
import QuotesPage from './pages/QuotesPage';
import Header from './components/Header';
import { AnimatePresence } from 'framer-motion';
const App = () => {
const location = useLocation();
return (
<>
<Header />
<AnimatePresence>
<Routes location={location} key={location.key}>
<Route path="/" element={<Home />} />
<Route path="/quote" element={<QuotesPage />} />
</Routes>
</AnimatePresence>
</>
);
};
export default App;
Code language: JavaScript (javascript)
At this point, you can now add exit animation to all your component’s parent elements, and the exit animation will work very fine.
For example, if you want to add it to the Home
component, which is a route as seen above. You can use variants
or add the animation object directly to the exit
prop.
// ...
import { motion } from 'framer-motion';
const Home = () => {
// ...
const containerVariants = {
hidden: {
opacity: 0,
x: '100vw',
},
visible: {
opacity: 1,
x: 0,
transition: {
type: 'spring',
mass: 0.4,
damping: 8,
when: 'beforeChildren',
staggerChildren: 0.4,
},
},
exit: {
x: '-100vw',
transition: {
ease: 'easeInOut',
},
},
};
return (
<motion.div
className="container"
variants={containerVariants}
initial="hidden"
animate="visible"
exit="exit"
>
{ // ... component elements }
</motion.div>
);
};
export default Home;
Code language: JavaScript (javascript)
At this point, your routes will be animated when any component leaves the screen. It will animate out by sliding to the left as specified in the exit object:
exit: {
x: '-100vw',
transition: {
ease: 'easeInOut',
},
},
Code language: JavaScript (javascript)
But you will notice that the animation of the new component already happens when the previous component leaves the screen. You can fix this by adding an exitBeforeEnter
prop to the <AnimatePresence
> component you used to wrap the App.js
component.
// ... imports
const App = () => {
const location = useLocation();
return (
<>
<Header />
<AnimatePresence exitBeforeEnter>
{ // ... routes }
</AnimatePresence>
</>
);
};
export default App;
Code language: JavaScript (javascript)Each page is animated once the route is changed.
Framer Motion allows you to animate your Scalable Vector Graphics (SVGs) via their path (A way of drawing your SVGs). This is possible by attaching the motion component to the path element of your SVG and then adding your preferred animation alongside the pathlength
property with a value of 0
and 1
for when the animation starts and when it ends.
Suppose you have an SVG, you will notice that these SVGs usually have a path or paths defined with so many attributes and values:
const AnimateSVG = () => {
return (
<div className="container">
<svg
className="my-svg"
viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg"
>
<rect fill="none" height="256" width="256" />
<path
d="M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
/>
<path
d="M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
/>
</svg>
</div>
);
};
export default AnimateSVG;
Code language: JavaScript (javascript)
You can attach the motion component to the path element and then use the initial
and animate
props to add animation to the path:
import { motion } from 'framer-motion';
const AnimateSVG = () => {
return (
<div className="container">
<svg
className="my-svg"
viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg"
>
<rect fill="none" height="256" width="256" />
<motion.path
d="M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
initial={{ pathLength: 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 2 }}
/>
<motion.path
d="M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
initial={{ pathLength: 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 2 }}
/>
</svg>
</div>
);
};
export default AnimateSVG;
Code language: JavaScript (javascript)
In the code block above, you will notice that pathLength
is used to add animation to the SVG. The initial animation is set to 0
, meaning nothing shows, and then within 2 seconds, the SVG will draw.
Animated SVG.
Instead of adding the animation directly, you can use variants
and add that single variant to both paths. You can also add more forms of animation, such as opacity
if you wish.
import { motion } from 'framer-motion';
const AnimateSVG = () => {
const pathVariants = {
hidden: {
opacity: 0,
pathLength: 0,
},
visible: {
opacity: 1,
pathLength: 1,
transition: {
duration: 2,
ease: 'easeInOut',
},
},
};
return (
<div className="container">
<motion.svg
className="my-svg"
viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg"
>
<rect fill="none" height="256" width="256" />
<motion.path
d="M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
variants={pathVariants}
initial="hidden"
animate="visible"
/>
<motion.path
d="M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
variants={pathVariants}
initial="hidden"
animate="visible"
/>
</motion.svg>
</div>
);
};
export default AnimateSVG;
Code language: JavaScript (javascript)
Run the sandbox to preview the application in action:
In this guide, you have learned how to use Framer Motion to add animation to your React application elements, routes, and SVGs. It is important to note that the Framer Motion offers more than what has been covered in this guide, with many more animation options.
This guide has laid a proper foundation to help you navigate through and use the library in your React application for the first time. You can now check out the Framer Motion documentation to understand more options and animate your React application better. You can also get the animated demo project from this article’s GitHub repository.
Have fun coding!
I’m Joel Olawanle, a frontend developer and technical writer interested in making the web accessible to everyone by always looking for ways to give back to the tech community. Follow me and connect with me on Twitter.
Original article source at: https://coderpad.io
1598839687
If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?
In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.
Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.
React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.
Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.
Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.
The popularity of React Native comes from its advantages. Some of its advantages are as follows:
Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.
React Native is very close to native. Consider the following aspects as described on the React Native website:
Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.
#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native
1676354520
Animations on web pages involve moving elements on the screen to improve the visual experience of users. These animations are visually appealing and tend to draw users’ attention to buttons, images, and other important information. Overall, it contributes to establishing a solid connection between users and the content on the screen.
In the past, web pages were very plain and lacked interactivity because they were only intended to display information, and there was no capability to create appealing visual experiences. Things have changed dramatically in recent years, thanks to the availability of libraries and packages that make it easier to animate and add visual experiences to your web pages.
This article will teach you how to use the Framer Motion library to animate a React application. Why should you use Framer Motion? And how it works by animating a mini-project.
Framer Motion is an open-source production-ready motion library for React. It allows you to add all forms of animations and transitions directly to Document Object Model (DOM) elements defined in your React components. Its syntax is easy to understand, and with a few lines of code, you will be able to implement awesome animations.
Framer Motion uses an awesome API that gives you access to the motion component. You can plug this component into your React DOM elements giving you access to additional props for animation. For example, the div
element will become motion.div
.
The Framer Motion also has beginner-friendly documentation with lots of practical examples and illustrations showing how to perform simple to complex forms of animations and transitions.
Framer Motion uses the motion
component, which you can attach to any DOM element to implement an animation or transition effect.
<motion.div>
<p> An animated text </p>
</motion.div>
Code language: HTML, XML (xml)
Nothing changes when you attach the motion
component to your element. Instead, it supercharges that element with animation capabilities, giving you access to props, keys, and values you can use to add any animation. For example, you can access the animate
prop, which takes in a dynamic value — an object which contains the different properties and values you want to animate.
<motion.div animate={{x: -10, y: 10}}>
<p> An animated text </p>
</motion.div>
Code language: HTML, XML (xml)
The above animation will cause the div
element to slide 10px
to the left and then 10px
to the bottom when it loads from the original position specified in style.
When units are not specified, calculations are done in pixels. You can also specify your preferred units by attaching them. For example:
animate={{x:"-10rem"}}
.
Framer Motion is a popular library with over 1.6 million weekly downloads. You can install the library into your React project using the command below in your terminal:
$ npm i framer-motion
Once you have installed it, you can verify if it has been installed in your package.json
file’s dependencies
object:
"dependencies": {
// ...
"framer-motion": "^7.5.3",
"react": "^18.2.0",
// ...
},
Code language: JSON / JSON with Comments (json)
Once you have installed the Framer Motion library, you can import the motion
component into a component you wish to use within your project, and everything will work fine.
import { motion } from 'framer-motion';
<motion.h1 animate={{x: 10, y: 20}}>
An animated text
</motion.h1>
Code language: JavaScript (javascript)
The motion
component can be applied to any DOM element within your React component. You can apply it to the container elements, headings, text elements, images, and other elements. This motion component gives you access to props like animate
, which you can use to adjust the color, size, position, display, and add other animation effects to the DOM element.
<motion.div
animate={{
x: 10,
backgroundColor: '#fff',
boxShadow: '10px 10px 0 rgba(0, 0, 0, 0.2)',
}}
>
<h1>Hi</h1>
<img src={myImage} alt="" />
</motion.div>
Code language: HTML, XML (xml)
In the example above, the background color changes to white. A box shadow is added to the div
when it loads, and the elements contained in the div
moves 10px
to the right.
It is also important to know that you can apply any form of animation based on CSS attributes. For example, you use x
, y
, and z
to move elements similar to translateX
, translateY
, and translateZ
.
You can also scale the size of an element using the scale
attribute for both x
and y
or scaleX
and scaleY
if you wish to apply separate values. Example:
<motion.div animate={{ scale: 0.8 }} >
<h1>Hi</h1>
<motion.img animate={{ scaleX: 1.2 }} src={myImage} alt="" />
</motion.div>
Code language: HTML, XML (xml)
The same applies to transformation values like skew
and rotate
. It’s also important to mention that it supports all forms of value types, such as numbers, strings, and colors in Hex, RGB, and HSLA as strings. You can also calculate values using the calc()
function.
<motion.img animate={{ x: "calc(100vw - 50%)" }} src={myImage} alt="" />
Code language: HTML, XML (xml)
At this point, you have only used the animate
prop, which makes you animate elements from the default CSS style to your specified values. But this is not always what you want; sometimes, you want to specify where your animation starts and ends. For example, you might want your text to slide from the left of your screen to the middle.
You can specify initial animation values using the initial
props, which work similarly to the animate
prop and uses similar attributes and values.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)
You will notice that these animations happen fast to the extent that you might not see them unless you pay close attention.
The page animates a block of Lorem ipsum text by indenting it and reverting it to its original position.
You can fix this by adding transition effects using the transition
prop. This transition
prop defines the type of animation used when animating between two values and how these values animate from one state to another.
For example, if you add a duration
of 1
, the animation will run for 1 second.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
transition={{ duration: 1 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)The page animates a block of Lorem ipsum text for a second by indenting it and reverting it to its original position.
There are three major transition types, and they all have their specific effect; they include Tween
, Spring
, or Inertia
. Tween
is the default type for all animation, so if you want a spring form of animation, you will need to specify the transition type as Spring
.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
transition={{ type: 'spring', duration: 1 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)
Each type has some attributes peculiar to them. For example, when you use the transition type of spring
, you have access to an attribute like bounce
which specifies how elastic the spring effect is on the element.
<motion.div
initial={{ opacity: 0, x: '-100vh' }}
animate={{ opacity: 1, x: 0 }}
transition={{ type: 'spring', bounce: 0.6 }}
>
<h1>Hi</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt
perspiciatis voluptates nihil dolores eum architecto eligendi
</p>
</motion.div>
Code language: HTML, XML (xml)Spring animation.
There are other attributes like delay
, which you can use to set a delay time before the animation renders.
Sometimes, you may want your elements to animate through several different animation values, which may be from left to right, depending on what you want. In CSS, this is handled with keyframes
, but for Framer Motion, you can do this with a square bracket holding as many values as you want.
<motion.button
animate={{ x: [0, -20, 20, -20, 20, 0] }}
transition={{ delay: 1 }}
>
My Button
</motion.button>
The page animates a button to the left after a second from the initial render.
Note that in the example above, the animation waited for 1 second before moving the button to the left 20px
and right 20px
twice before returning to its original position. This can be applied to any animation transform attribute such as x,
y
, scale
, rotate
and others.
<motion.button
animate={{ scale: [1, 1.2, 1, 1.2, 1] }}
transition={{ delay: 1 }}
>
My Button
</motion.button>
Code language: HTML, XML (xml)
You will notice that these animations stop after it has completed the number of times specified. The example above will start at 1, which is the default. It will then scale up and down twice to its default value.
However, you may want the animation effect to repeat by using the transition
property yoyo
and set its value to Infinity
or the number of times you wish your animation repeats.
The transition property
yoyo
lets you specify the number of times you want an animation to repeat.
<motion.button
animate={{ scale: 1.2 }}
transition={{ delay: 1, yoyo: Infinity }}
>
My Button
</motion.button>
Code language: HTML, XML (xml)
In the code block above, you will notice you no longer need the square bracket. All you need is the value it should change to when it leaves the default, 1
.
The page animates a Generate Quote button once the button is hovered on.
You often want to apply hover gestures to buttons, cards, and other elements to draw the user’s attention. You might want a button to scale up and down when a user hovers over the button instead of just scaling up and down without any action from the user. To do this, you can use the whileHover
prop, which works similarly to previous props.
<motion.button
whileHover={{
scale: 1.2,
transition: { yoyo: Infinity },
}}
>
My Button
</motion.button>
Code language: HTML, XML (xml)
The transition property for
whileHover
prop is placed within the curly braces and not outside thewhileHover
prop.
The page animation transitions once the Generate Quote button is hovered on.
At this point, you have learned the basics of Framer Motion, how it works and how you could use it to implement animations directly on your DOM elements. You must have started asking yourself why you should add all animation properties and values directly on each element because it tends to make your code clumsy and not easy to understand.
This is where variants come in. You use variants to remove the props object and place them outside your component, so you only use the object names within your DOM element. This also helps you use the same animation effect for multiple elements within your component to avoid repetition.
import { Link } from 'react-router-dom';
import logo from './../images/logo.svg';
import { motion } from 'framer-motion';
const headerVariants = {
initial: {
y: '-100vh',
},
animate: {
y: 0,
transition: {
delay: 0.5,
duration: 0.5,
type: 'spring',
},
},
};
const Header = () => {
return (
<div className="container">
<motion.div
className="header"
variants={headerVariants}
initial="initial"
animate="animate"
>
<Link to="/">
<img src={logo} alt="" />
</Link>
<h1>Quotes Circle</h1>
</motion.div>
</div>
);
};
export default Header;
Code language: JavaScript (javascript)
In the code block above, an object (variant) is created to hold all the animation properties and values. This object contained two child objects, one to hold the initial
animation and the second to hold the final animation (animate
). You can give them any name:
const headerVariants = {
initial: {
y: '-100vh',
},
animate: {
y: 0,
transition: {
delay: 0.5,
duration: 0.5,
type: 'spring',
},
},
};
Code language: JavaScript (javascript)
To make use of this variant in your React DOM elements, then you will use the variants
prop and pass the variant name into it. Also, you will assign the initial
and animate
prop with the name you used when creating your variant, but this time as a string and not a dynamic value.
<motion.div
variants={headerVariants}
initial="initial"
animate="animate"
>
<Link to="/">
<img src={logo} alt="" />
</Link>
<h1>Quotes Circle</h1>
</motion.div>
Code language: HTML, XML (xml)
You can have as many variants as possible, and if you use the same initial
and animate
value, you would not need to declare that for children elements in your component.
const headerVariants = {
initial: {
y: '-100vh',
},
animate: {
y: 0,
transition: {
delay: 0.5,
duration: 0.5,
type: 'spring',
},
},
};
const logoVariants = {
initial: {
x: '-100vw',
},
animate: {
x: 0,
transition: {
delay: 1,
},
},
};
const Header = () => {
return (
<div className="container">
<motion.div
className="header"
variants={headerVariants}
initial="initial"
animate="animate"
>
<Link to="/">
<motion.img src={logo} variants={logoVariants} />
</Link>
<h1>Quotes Circle</h1>
</motion.div>
</div>
);
};
Code language: JavaScript (javascript)
If you look at the logo image element, you will notice the variants
prop was only defined. There is no need to define the initial
and animate
prop because it has the same name as the previous one you already declared in the parent element.
When building applications that have more than one page, you will want to add animation between each page. These animations may not be conspicuous but can add a nice experience rather than having content suddenly leave the screen.
The page animates a new component once the route changes.
The GIF above almost looks like the routes are animated, but you will notice that it only animates in but not out. This means that the components disappear when they unmount, so you will want to add an animation instead.
This is done with the AnimatePresence
component, which notifies components when they will be unmounted and allows them to defer that unmounting until after the animation is complete. This animation is added with the exit prop to your component’s parent element.
To implement this, you will first import AnimatePresence
component to the App.js
file where you configured your routes or if it’s in the index.js
file. Once that is done, you will wrap the Routes
component with the AnimatePresence
component:
import { Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import QuotesPage from './pages/QuotesPage';
import Header from './components/Header';
import { AnimatePresence } from 'framer-motion';
const App = () => {
return (
<>
<Header />
<AnimatePresence>
<Routes location={location} key={location.key}>
<Route path="/" element={<Home />} />
<Route path="/quote" element={<QuotesPage />} />
</Routes>
</AnimatePresence>
</>
);
};
export default App;
Code language: JavaScript (javascript)
Then you will need to use useLocation
router hook to know when the route changes. This will be attached to the Route
component:
import { Routes, Route, useLocation } from 'react-router-dom';
import Home from './pages/Home';
import QuotesPage from './pages/QuotesPage';
import Header from './components/Header';
import { AnimatePresence } from 'framer-motion';
const App = () => {
const location = useLocation();
return (
<>
<Header />
<AnimatePresence>
<Routes location={location} key={location.key}>
<Route path="/" element={<Home />} />
<Route path="/quote" element={<QuotesPage />} />
</Routes>
</AnimatePresence>
</>
);
};
export default App;
Code language: JavaScript (javascript)
At this point, you can now add exit animation to all your component’s parent elements, and the exit animation will work very fine.
For example, if you want to add it to the Home
component, which is a route as seen above. You can use variants
or add the animation object directly to the exit
prop.
// ...
import { motion } from 'framer-motion';
const Home = () => {
// ...
const containerVariants = {
hidden: {
opacity: 0,
x: '100vw',
},
visible: {
opacity: 1,
x: 0,
transition: {
type: 'spring',
mass: 0.4,
damping: 8,
when: 'beforeChildren',
staggerChildren: 0.4,
},
},
exit: {
x: '-100vw',
transition: {
ease: 'easeInOut',
},
},
};
return (
<motion.div
className="container"
variants={containerVariants}
initial="hidden"
animate="visible"
exit="exit"
>
{ // ... component elements }
</motion.div>
);
};
export default Home;
Code language: JavaScript (javascript)
At this point, your routes will be animated when any component leaves the screen. It will animate out by sliding to the left as specified in the exit object:
exit: {
x: '-100vw',
transition: {
ease: 'easeInOut',
},
},
Code language: JavaScript (javascript)
But you will notice that the animation of the new component already happens when the previous component leaves the screen. You can fix this by adding an exitBeforeEnter
prop to the <AnimatePresence
> component you used to wrap the App.js
component.
// ... imports
const App = () => {
const location = useLocation();
return (
<>
<Header />
<AnimatePresence exitBeforeEnter>
{ // ... routes }
</AnimatePresence>
</>
);
};
export default App;
Code language: JavaScript (javascript)Each page is animated once the route is changed.
Framer Motion allows you to animate your Scalable Vector Graphics (SVGs) via their path (A way of drawing your SVGs). This is possible by attaching the motion component to the path element of your SVG and then adding your preferred animation alongside the pathlength
property with a value of 0
and 1
for when the animation starts and when it ends.
Suppose you have an SVG, you will notice that these SVGs usually have a path or paths defined with so many attributes and values:
const AnimateSVG = () => {
return (
<div className="container">
<svg
className="my-svg"
viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg"
>
<rect fill="none" height="256" width="256" />
<path
d="M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
/>
<path
d="M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
/>
</svg>
</div>
);
};
export default AnimateSVG;
Code language: JavaScript (javascript)
You can attach the motion component to the path element and then use the initial
and animate
props to add animation to the path:
import { motion } from 'framer-motion';
const AnimateSVG = () => {
return (
<div className="container">
<svg
className="my-svg"
viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg"
>
<rect fill="none" height="256" width="256" />
<motion.path
d="M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
initial={{ pathLength: 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 2 }}
/>
<motion.path
d="M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
initial={{ pathLength: 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 2 }}
/>
</svg>
</div>
);
};
export default AnimateSVG;
Code language: JavaScript (javascript)
In the code block above, you will notice that pathLength
is used to add animation to the SVG. The initial animation is set to 0
, meaning nothing shows, and then within 2 seconds, the SVG will draw.
Animated SVG.
Instead of adding the animation directly, you can use variants
and add that single variant to both paths. You can also add more forms of animation, such as opacity
if you wish.
import { motion } from 'framer-motion';
const AnimateSVG = () => {
const pathVariants = {
hidden: {
opacity: 0,
pathLength: 0,
},
visible: {
opacity: 1,
pathLength: 1,
transition: {
duration: 2,
ease: 'easeInOut',
},
},
};
return (
<div className="container">
<motion.svg
className="my-svg"
viewBox="0 0 256 256"
xmlns="http://www.w3.org/2000/svg"
>
<rect fill="none" height="256" width="256" />
<motion.path
d="M108,144H40a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
variants={pathVariants}
initial="hidden"
animate="visible"
/>
<motion.path
d="M224,144H156a8,8,0,0,1-8-8V72a8,8,0,0,1,8-8h60a8,8,0,0,1,8,8v88a40,40,0,0,1-40,40"
fill="none"
stroke="#000"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="12"
variants={pathVariants}
initial="hidden"
animate="visible"
/>
</motion.svg>
</div>
);
};
export default AnimateSVG;
Code language: JavaScript (javascript)
Run the sandbox to preview the application in action:
In this guide, you have learned how to use Framer Motion to add animation to your React application elements, routes, and SVGs. It is important to note that the Framer Motion offers more than what has been covered in this guide, with many more animation options.
This guide has laid a proper foundation to help you navigate through and use the library in your React application for the first time. You can now check out the Framer Motion documentation to understand more options and animate your React application better. You can also get the animated demo project from this article’s GitHub repository.
Have fun coding!
I’m Joel Olawanle, a frontend developer and technical writer interested in making the web accessible to everyone by always looking for ways to give back to the tech community. Follow me and connect with me on Twitter.
Original article source at: https://coderpad.io
1615544450
Since March 2020 reached 556 million monthly downloads have increased, It shows that React JS has been steadily growing. React.js also provides a desirable amount of pliancy and efficiency for developing innovative solutions with interactive user interfaces. It’s no surprise that an increasing number of businesses are adopting this technology. How do you select and recruit React.js developers who will propel your project forward? How much does a React developer make? We’ll bring you here all the details you need.
Facebook built and maintains React.js, an open-source JavaScript library for designing development tools. React.js is used to create single-page applications (SPAs) that can be used in conjunction with React Native to develop native cross-platform apps.
In the United States, the average React developer salary is $94,205 a year, or $30-$48 per hour, This is one of the highest among JavaScript developers. The starting salary for junior React.js developers is $60,510 per year, rising to $112,480 for senior roles.
In context of software developer wage rates, the United States continues to lead. In high-tech cities like San Francisco and New York, average React developer salaries will hit $98K and $114per year, overall.
However, the need for React.js and React Native developer is outpacing local labour markets. As a result, many businesses have difficulty locating and recruiting them locally.
It’s no surprise that for US and European companies looking for professional and budget engineers, offshore regions like India are becoming especially interesting. This area has a large number of app development companies, a good rate with quality, and a good pool of React.js front-end developers.
As per Linkedin, the country’s IT industry employs over a million React specialists. Furthermore, for the same or less money than hiring a React.js programmer locally, you may recruit someone with much expertise and a broader technical stack.
React is a very strong framework. React.js makes use of a powerful synchronization method known as Virtual DOM, which compares the current page architecture to the expected page architecture and updates the appropriate components as long as the user input.
React is scalable. it utilises a single language, For server-client side, and mobile platform.
React is steady.React.js is completely adaptable, which means it seldom, if ever, updates the user interface. This enables legacy projects to be updated to the most new edition of React.js without having to change the codebase or make a few small changes.
React is adaptable. It can be conveniently paired with various state administrators (e.g., Redux, Flux, Alt or Reflux) and can be used to implement a number of architectural patterns.
Is there a market for React.js programmers?
The need for React.js developers is rising at an unparalleled rate. React.js is currently used by over one million websites around the world. React is used by Fortune 400+ businesses and popular companies such as Facebook, Twitter, Glassdoor and Cloudflare.
As you’ve seen, locating and Hire React js Developer and Hire React Native developer is a difficult challenge. You will have less challenges selecting the correct fit for your projects if you identify growing offshore locations (e.g. India) and take into consideration the details above.
If you want to make this process easier, You can visit our website for more, or else to write a email, we’ll help you to finding top rated React.js and React Native developers easier and with strives to create this operation
#hire-react-js-developer #hire-react-native-developer #react #react-native #react-js #hire-react-js-programmer
1651604400
React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Express, GraphQL and React, containing modern web development tools such as Webpack, Babel and Browsersync. Helping you to stay productive following the best practices. A solid starting point for both professionals and newcomers to the industry.
See getting started guide, demo, docs, roadmap | Join #react-starter-kit chat room on Gitter | Visit our sponsors:
The master
branch of React Starter Kit doesn't include a Flux implementation or any other advanced integrations. Nevertheless, we have some integrations available to you in feature branches that you can use either as a reference or merge into your project:
master
)feature/redux
)feature/apollo
)master
)You can see status of most reasonable merge combination as PRs labeled as TRACKING
If you think that any of these features should be on master
, or vice versa, some features should removed from the master
branch, please let us know. We love your feedback!
React Starter Kit
| React Static Boilerplate
| ASP.NET Core Starter Kit
| |
---|---|---|---|
App type | Isomorphic (universal) | Single-page application | Single-page application |
Frontend | |||
Language | JavaScript (ES2015+, JSX) | JavaScript (ES2015+, JSX) | JavaScript (ES2015+, JSX) |
Libraries | React, History, Universal Router | React, History, Redux | React, History, Redux |
Routes | Imperative (functional) | Declarative | Declarative, cross-stack |
Backend | |||
Language | JavaScript (ES2015+, JSX) | n/a | C#, F# |
Libraries | Node.js, Express, Sequelize, GraphQL | n/a | ASP.NET Core, EF Core, ASP.NET Identity |
SSR | Yes | n/a | n/a |
Data API | GraphQL | n/a | Web API |
♥ React Starter Kit? Help us keep it alive by donating funds to cover project expenses via OpenCollective or Bountysource!
Anyone and everyone is welcome to contribute to this project. The best way to start is by checking our open issues, submit a new issue or feature request, participate in discussions, upvote or downvote the issues you like or dislike, send pull requests.
Copyright © 2014-present Kriasoft, LLC. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.
Author: kriasoft
Source Code: https://github.com/kriasoft/react-starter-kit
License: MIT License
1621573085
Expand your user base by using react-native apps developed by our expert team for various platforms like Android, Android TV, iOS, macOS, tvOS, the Web, Windows, and UWP.
We help businesses to scale up the process and achieve greater performance by providing the best react native app development services. Our skilled and experienced team’s apps have delivered all the expected results for our clients across the world.
To achieve growth for your business, hire react native app developers in India. You can count on us for all the technical services and support.
#react native app development company india #react native app developers india #hire react native developers india #react native app development company #react native app developers #hire react native developers