React Spring 5 useResize(), useInView(), indexed useSprings()

The useResize(), a useSpring() abstraction, returns the useRef() container width/height props. Its onChange() will trigger on window resize.

//width returned in px
let quad = useRef(null)

const { width, height } = useResize({
  container: quad,
  onChange: ({value: {width} })=> {
    console.log( width )
  }
})

<div>
  <div style={{ width: "50vw", height: "30vh", background: "yellow" }} ref={quad}>
  </div>
</div>

Animated useSpring() with useInView() hook

The useInView() hook tracks an element's visibility relative to the viewpoint. It will return the useRef() for the target element and the boolean result of the Intersection Observer API

chevron-rightMulti directional section with useInView()hashtag

When having multiple useInView() elements keep the threshold below 0.5, to not overlay and repeat the useSpring().

useInView() margin/opacity animated

Rendering useSprings() with index-based style properties

The useSprings() hook renders a number of indexed springs with shared props, their indexes can set their individual properties and be used to animate specific useSpring().

chevron-rightOverlayed useDrag() swipe effect and function from/to useSpring() propshashtag

We use the useSprings() index as arguments for the from/to functions.

The starting animation is a translateY(-1000), we distance (4px) and rotate each card, and the delay adds the stack effect.

We create a useState() set to store the swiped cards (like an array but with no repeated elements and has() method)

We useDrag() the current passed argument index element, on mx only movement, we add() it to the set if it exceeds the minimal velocity after it stops being dragged.

We api.start() only the useDrag() index useSpring(), onDrag() its scale gets bigger, its position and rotation depend on the mx drag distance if it's in the set() then we swipe it out of viewpoint.

When drag is finished and all cards are set() we clean() it and api.start() the last card keyframe (to)

We loop useSprings() into 2 components, with its index in the useDrag() as argument. A relative container for the overlayed absolute cards, with overflow to not expand the width on swipe.

We add a custom circle cursor to the absolute cards, and set the background properties.

We can mimic a useChain() sequential animation with useSprings(), we distance the delay of each element by their index, the effect depends on the duration-delay ratio.

useSpring() index sequential animation.
chevron-rightfrom/to functions on useSprings() indexed elementshashtag

We use the from/to functions to render useSprings() props, the starting point (deck) from is shared between the springs while the endpoint to depends on the index position.

The useState() reverse resets the cards to their starting point.

On click, we reverse animate the card's rotate prop and set() its new index background (set() quickly loads the new image instead of waiting for the duration)

We create the draw-from-top effect by interpolating the z-index prop at the very start of their animation.

We translate(vw, vh) the cards but their width/height is fixed.

useSprings() reverse set() and start()

We can set transition on the style object and the transition style property on the looped map() elements, without using springs().

Transition CSS properties

1

1

1

1

1

Last updated