React-Spring-1 useSpring(), config, To() interpolate render, useTransition(), React-use-measure.

React-spring is a library to create animated UI components.

npm i react-spring

App.js
import { animated, useSpring } from '@react-spring/web'

We import the animated higher-order component, a function that takes a component and returns a new one.

We useSpring({}) hook to set up the animation object properties (from, to), then we deconstruct it on the style property to trigger it on mount.

//we append the component on the animated high-order tag
function MyComponent() {

  const springs = useSpring({
    from: { x: 0 },
    to: { x: 100 },
  })

  return (
    <animated.div
      style={{
        width: 80, height: 80,
        background: '#ff6d6d',
        borderRadius: 8,
        ...springs
      }}
    />
  )
}

<div> <MyComponent /> </div>
Spring animation object + onClick api

The <animated> component can be used directly on the JSX with a useString() as a style prop.

We can use the useSpringRef() hook to reference the imperative API on event handlers.

We need to declare the api in the useSpring() and the function syntax, if we want the useState() dependency to work.

Any difference between the useSpring() and the api.method from:{} will be skipped.

useSpring(9 state and event handler function with useSringRef()

We can useSpringRef() to pause/resume and stop animations.

The useSpring() config property contains mass, tension, friction, easing, and duration (used for its "timing function")

chevron-rightList of useString() presets configshashtag

We can use both a preset and a custom config property.

There are the presets configs.

We adapt the CSS @keyframe{} animation with useSpring(), using the {easings.steps()} hook for the animation-timing-function.

chevron-rightAbsolute window and single sprite animation.hashtag

On a container we animate a position-absolute window, for the actual movement, and the sprite image, for the steps() animation.

We can modify the background-image sprite, but we must keep its proportions in the container (48/26 = 96/52).

easing.steps() sprite animations

To() interpolate style and text render

Using the to method, we interpolate the springValue data on a different style property (we can animate both), and access its pure value (without the spring Object).

We use the springValue as a breakpoint to animate a style property.

We useState() a conditional springValue and the to() method to animate style props.

Range are the animated springValues breakpoints, output are the style property values on each breakpoint, and both only accept integers in their array.

Animated style properties onClick()

We can trigger a CSS animation with useSpring() by interpolating a class string. The delay is equal to the current CSS animation duration.

CSS animation on an useSpring() interpolated string

A useState() conditional useSpering() prop.

We use an array of to style objects for multiple animations, the array has to include the from object (any object before it will be ignored).

We animate a useSpring() background image using only output (will work on its first 2 values).

When animating a CSS background image, we use inset to cover the X/Y empty margins.

Animated useSpring() background image

We can directly destruct a useSpring() object and re-assign property names.

chevron-rightDestruct useSpring() rotateX property on 2 display absolute imageshashtag

The 2 absolute images are overlayed in the same container.

We useState() the useSpring() destructured style properties, and use them on the animated components.

Both images share the useSpring() style properties, the front image opacity needs to be opposite from the back one, we edit it using the to method.

rotateX and opacity useString() on 2 absolute images

The useTransition() animation styles and methods

The useTransition(array, config) hook sequentially animates datasets of elements on the DOM.

useTransition() triggers automatically, we useEffect() to bind it to events.

We create a useState() index and an array of components to render with the useTransition()

The useTransition() onRest method triggers each time an animated transition is completed.

The useTransition() renders one element at a time and the onRest() method updates its index to re-trigger the transition.

The useTransition() hook returns the style object and its array content (an useState() index)

automatic useTransition() updated onRest()
useTransition() index useState() to render different components

To render multiple <animated> elements we put an array as a useTransition() argument.

useTransition() multiple elements

Changing the order of its useState() array doesn't trigger the useTransition() animation by itself.

We need to dynamically create style properties, from external variables, during the array methods, which we extract in the enter object.

The update() property animates its extracted style properties each time the array argument changes, it also requires the property value to be set using an external counter.

chevron-rightuseTransition() to set and update style properties with imported objects.hashtag

We npm install lodash (javascript library) and import the shuffle method.

We useState() an array of style objects to render and animate with useTransition(). We useEffect() setInterval() to trigger the shuffle method.

We update the counter on the array.method argument and to set the style property (x).

We use the counter to style the container, the spring object to style the <animated>, and the imported object properties to style the single-child components.

The components share the same space (width) with position: absolute.

useTransition() on {x} updated style property

React-use-measure on events and react-spring animations

The React-use-measure tool references DOM elements to measure their boundaries.

Relative Cursor position on measured DOM element

We access the from/to useSpring() properties with:

chevron-rightAnimating measures with useSpring() conditional values.hashtag

We animate a react-use-measure width with a useState() trigger, using a ternary operator to change the useSpring() target value (to).

The relative>absolute child component is animated using its parent measured width.

The children CSS properties set its starter animation condition (from).

The useString() api for the event works too, but we can't reset the (from) property yet.

Animated width property on useState()

The matchMedia() method returns true if the document matches (or is higher) than its media-query string.

We useMeasure() to dynamically set the column's width on media queries.

columns and with on resize

We import an array of image objects as a useState(), to use in the useTransition().

We import the lodash.shuffle method to useEffect() shuffle the useState(Data).

We set the (container) height and the useTransition() images objects array on a useMemo().

On shuffle the images translate-X/Y, change column array position, and the (max) height container is updated.

useTransition() image grid
codeSandbox of the github example

Last updated