Animation duration in React Spring
Asked Answered
S

2

7

I have a fairly simple fade-in scenario, where I want to control the duration of the animation. But cannot wrap around my head around how to accomplish this.

Code excerpt:

function BoxA() {
  return (
    <Spring from={{ opacity: 0.2 }} to={{ opacity: 1 }}>
      {props => (
        <div
          style={{
            height: 100,
            width: 100,
            backgroundColor: "pink",
            color: "#fff",
            ...props
          }}
        >
          1
        </div>
      )}
    </Spring>
  );
}

Complete code example: https://codesandbox.io/s/n7pw660264

Serrate answered 7/1, 2019 at 12:18 Comment(0)
C
11

You have to set the config property for the duration.

<Spring config={{duration: 5000}} from={{ opacity: 0.2 }} to={{ opacity: 1 }}>
Coloring answered 7/1, 2019 at 15:3 Comment(2)
This is correct, duration switches to a linear animation, in this case 5 seconds. Time based often isn't the best choice, though, see: react-spring.surge.sh/gotchasMediatorial
Yes, it is linear by default, but you can change the easing in config as well. And I never used either of them. Most of the time spring animation just works out of the box.Coloring
S
-2

You can use delay property to control the animation,

According to documentation

Delay in ms before the animation starts (config.delay takes precedence if present) */

like this

<Spring from={{ opacity: 0.2 }} delay={1000} to={{ opacity: 1 }}></Spring>

Demo

Source

Sized answered 7/1, 2019 at 12:30 Comment(2)
Hm, it looks like it's only waiting (delaying) 1000 ms with the animation. I was looking for defining a time unit during which the animation would gradually take effect.Serrate
@FellowStranger as far as I know there is no property to support it.Sized

© 2022 - 2024 — McMap. All rights reserved.