(I) Slide Around
What we're gonna be building
Code
import { animated, useSpring } from "@react-spring/web";
import React, { useEffect, useState } from "react";
const SlideAround = () => {
const [animate, setAnimate] = useState(false);
const { translation } = useSpring({
from: { translation: animate ? 0 : 100 },
to: { translation: animate ? 100 : 0 },
});
useEffect(() => {
const timeout = setTimeout(() => {
setAnimate(!animate);
}, 1000);
return () => {
clearTimeout(timeout);
};
}, [animate]);
return (
<animated.div
style={{
width: 80,
background: "#ff6d6d",
height: 80,
borderRadius: 8,
x: translation,
}}
/>
);
};
Code Breakdown
The above code can be divided into 3 sections
Animation definition
const [animate, setAnimate] = useState(false);
const { translation } = useSpring({
from: { translation: animate ? 0 : 100 },
to: { translation: animate ? 100 : 0 },
});
react-spring provides a
useSpringhook, which is a animation controller of sorts.it provides configuration of variety of parameters, but to keep things simple, we will use the
toandfromparameters for setting-up an initial and final position for our animation and let react-spring take care of the rest.with a mutable state variable, we will tell react-spring that we want to either move from 0 to 100 or move from 100 to 0 based on what the
animatevariable is set to, where 0 and 100 refer to the range between which we will apply our transform translate value in CSS.
(NOTE: this is done via the animated component, by react-spring, as mentioned below)notice how we're destructuring
translationfrom theuseSpringhook, this is because, useSpring is actually computing a realtime value for us, based on ourtoandfromparameters, on, thetranslationkey of the object
(NOTE: this key can be anything that the user specifies).
This key is then returned to us, by theuseSpringhook, in the form of a continuously changing (animating) value.
learn more aboutuseSpring()hook
Animation control
useEffect(() => {
const timeout = setTimeout(() => {
setAnimate(!animate);
}, 1000);
return () => {
clearTimeout(timeout);
};
}, [animate]);
This code block is simply meant to trigger the animation every 1 second.
By calling
setAnimate, with atrueorfalsealternatively, we force ouruseSpringhook, to return us a value
that triggers animation, between the to and from specified.
component declaration
<animated.div
style={{
width: 80,
background: "#ff6d6d",
height: 80,
borderRadius: 8,
x: translation,
}}
/>
finally, we simply apply the transformation (stored on
transitionvariable) onto our animated component, in the axis we want the component to translate along.we use the
animated.divcomponent provided by react-spring library, as it consumes the provided transformation, via theuseSpringhook, and tries to apply a valid animation onto the specified style property.
In our case, we use the thexproperty, as it refers to thetranslateproperty in CSS, for x-axis.
(NOTE: this interpretation is done by react-spring automatically)
learn more about<animated />elements
Conclusion
I hope that cleared up the basics of working with react-spring, if so, lets move onto the next tutorial, where we make a box shrink and expand!