React-Spring-2 useTransition() update style, useGesture() drag, SVG display and useSpring() animation

The useTrasition() style props can be rendered only in an animated.div component.

The Enter and Leave prop only animates the useTransition() elements being added/removed to the DOM, both will be rendered when the element is updated.

//useTransition() leaves-> enter on useState() change
//exitBeforeEnter finishes the leave before the enter
const [mio, setMio] = useState(["another"])

const estilo = useTransition(mio, {
  from: {
    back: "transparent", long: "0%", lefto: "0%",
    color: "black", opacity: 0, width: "20%"
  },
  enter: [
    { back: "pink", color: "green", long: "90%", lefto: "10%", width: "100%", opacity: 1 },
    { back: "orange", color: "blue", long: "50%", lefto: "50%" },
    { back: "red", color: "red", long: "0%", lefto: "100%" }
  ],
  leave: [
    { width: "20%" ,opacity: 0 }
  ],
  exitBeforeEnter: true,
  config:{ duration: 1000 }
})

//We deconstruct and ...rest the custom style properties
//We get the translate--centered-text effect animating width/text-center 
<>
  {estilo(( {back, long, lefto, ...resto} , item) => (
  
    <animated.div className="centra d-flex align-items-center" >
      <animated.div 
        className="sfondo"
        style={{backgroundColor: back, width: long, left: lefto}}
      >
      </animated.div>

      <animated.div className="testo" style={resto}>
        {item}
      </animated.div>
    </animated.div>
  ))}
</>
useTransition() on a double absolute flex container

The update useTransition() styles the elements not updated during the animation but doesn't trigger if the array remains the same.

chevron-rightuseTransition() arrays changed on setTimeout() and update stylinghashtag

The setTimeout() array updates will resolve sequentially, based on their timeout (2000 + 2000 + 1000), not their syntax order.

We use the CSS for the text.

Enter and Leave useTransition() array elements

The exitBeforeEnter property first animates the Leave element, then removes it and renders the Enter element in the same position.

The expires property animates and doesn't remove the Leave element while rendering the enter one.

The useTransition() events can be linked to a specific styler property.

React use-gesture and useSpring()

We npm install @use-gesture/react, a library that binds mouse and touch events to animate useSpring() NODE elements.

The useDrag() returns an object that applies event handlers to animated.div components. Its parameters are the drag action and the movement coordinates, and its gesture data animates the useSpring() style properties.

We spread the useDrag() variable in the animated.div element we want to interact with.

The useDrag() used to animate a useSpring() component
chevron-rightReplacing useSpring() properties with the useDrag() hookhashtag

We create variables for the useSpring() REST style properties.

During useDrag() we replace the REST style properties based on the x-axis position.

We don't drag the cover, we drag its container and animate its x style property, justifySelf and bg are replaced during onDrag().

The CSS style being:

useDrag() used to replace useSpring()

The useDrag() state attribute velocity matches the drag speed of the element, the config.decay triggers the momentum at the drag stop.

The useDrag() state attribute movement records the gesture change of position relative to its starter useSpring(), which gets reset at the end of useDrag()

animated.div onDrag() movement event

The arcTangent (Math.atan2) of the direction coordinates returns the current rotation position.

Waving transitions with animated SVG filters and useSpring()

SVG, short for Scalable Vector Graphics, uses the viewBox attribute to define a visible window area within the SVG element.

The viewBox can scale or pan its element proportionally to the container size (while maintaining its aspect ratio).

circles rendered inside <svg> tag using viewBox

The <filter> tag contains the filter primitive elements, used to create svg visual effects. The <g> tag can render multiple svg elements, its d(ata) attribute defines the path and shape of the element.

We animate the useSpring() between its starting/ending state with the useState() reverse property and dependency. We use the React-spring animated() function to create animatable filter primitive components.

To animate the SVG wave effect, we use the baseFrequency attribute for its frequency and direction (X/Y values) and the scale (factor) for the strength of the displacement effect.

vawe effect on useSpring() svg primitive filters
chevron-rightComplete list of <feDisplacementMap> and <feTurbulence> attributes.hashtag

We repeat the wave effect including all the fiter primitive attributes.

In the ReactJs:

Last updated