react-transition-group icon indicating copy to clipboard operation
react-transition-group copied to clipboard

Delay before animation begins

Open Richardinho opened this issue 8 years ago • 36 comments

In the first example given in the documentation there is a noticeable delay between clicking the button and the animation starting. This delay seems to correspond to the duration of the timeout property. Clearly, one would desire the animation to begin straight away, normally, as well as being able to configure a delay in some cases. It's not so obvious in the example because the timeout isn't very large, but when you set it to a bigger number you begin to see the issue.

In my own experiment I set start and end properties for the respective entering and exit states.

const transitionStyles = {
  entering: { left: 0 },
  entered: { left: '100px' },
};

What then happens is that the start property is immediately set, but that then a delay occurs before the animation kicks in.

I get the desired effect if I set the property to be the same in both states (and set the default to 0), but this doesn't seem right.

const defaultStyles = {
  left: 0,
}
const transitionStyles = {
  entering: { left: '100px' },
  entered: { left: '100px' },
};

Here is the full code for the my experiment when there is an unwanted delay at the start of the animation:

import ReactDOM from 'react-dom';
import React from 'react';
import { Transition } from 'react-transition-group';

const duration = 1600;

const defaultStyle = {
  transition: `left ${duration}ms linear`,
  position: 'absolute',
  top: 200,
  width: '100px',
  height: '100px',
}

const transitionStyles = {
  entering: { left: '0' },
  entered:  { left: '400px'},
};

class Foo extends React.Component {
  constructor () {
    super();
    this.state = {
      in: false,
      duration
    };
    this.onClick = this.onClick.bind(this);
    this.onClickOff = this.onClickOff.bind(this);
  }

  onClick() {
    this.setState({
      in: true,
    });
  }

  onClickOff() {
    this.setState({
      in: false,
    });
  }

  render() {
   return (
     <div>
       <button onClick={this.onClick}>clickOn</button>
       <button onClick={this.onClickOff}>clickOff</button>
       <Transition
        onEntering={() => { console.log('on entering')}}
        onEntered={() => { console.log('on entered')}}
       in={this.state.in}
       timeout={this.state.duration}>
       {(state) => (
         <div style={{
           background: 'blue',
             ...defaultStyle,
             ...transitionStyles[state]
         }}>
         <div>
         <h2>This is Bar</h2>
         </div>
         </div>
       )}
       </Transition>
     </div>
   );
  }
}

ReactDOM.render(
  <Foo/>, document.getElementById('app')
);

Richardinho avatar Feb 01 '18 13:02 Richardinho

I had this same issue. I think that using duration for both transition: left ${duration}ms linear and timeout={duration}. I solved the problem in my particular use case by setting timeout={0}.

k-funk avatar Mar 03 '18 01:03 k-funk

Change to:

const transitionStyles = {
  entering: { left: '400px' },
  entered:  { left: '400px' },
};

And the delay would be gone.

yaodingyd avatar Mar 09 '18 21:03 yaodingyd

I had the same issue. timeout={0} could fix it partially but sometimes enter transition doesn't work. I guess timeout = {0} makes entered state apply right after mounted before entering state apply to dom element.

Godsenal avatar Mar 19 '18 06:03 Godsenal

I personally find very confusing the notions of timeout and duration in this lib. It seems that timeout does a little bit of both, when it probably should not.

In my opinion, timeout should only control the actual timeouts between each state, and NOT the duration of the animation.

bleucitron avatar Mar 21 '18 16:03 bleucitron

I'm running into the exact same issue. I've tried both setting timeout to 0 and setting the entering and entered transitionStyles to the same value, but there's still a noticeable delay before the transition begins.

VincentNewkirk avatar Mar 30 '18 01:03 VincentNewkirk

Timeout is the duration of the animation. Setting it to 0 isn't the right approach,even if it appears to work. Underneath the state is all out of wack. A delay in something starting is probably due an improperly set up css transition, or failure to reflow the browser at the appropriate time. For most folks we recommend using the CSSTransition instead of the lower level Transition component unless you're familiar with the various complexities around doing case transitions in js

jquense avatar Mar 30 '18 01:03 jquense

Also, this comment might shed some more light on this behavior: https://github.com/reactjs/react-transition-group/issues/307#issuecomment-377073024

silvenon avatar Mar 31 '18 11:03 silvenon

It's good to know that timeout is actually the duration of the animation, but what do I do if I actually want a delay before the animation? For example, delay (don't do anything) for 500ms, then timeout (run animation) for 500ms.

XcrossD avatar Apr 12 '18 08:04 XcrossD

You can use setTimeout to delay whatever is setting the in prop to true.

silvenon avatar Apr 12 '18 16:04 silvenon

:tada: This issue has been resolved in version 2.3.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

jquense avatar Apr 14 '18 13:04 jquense

@jquense it has? Just updated, same problem remains...

vidjuheffex avatar Apr 16 '18 22:04 vidjuheffex

It’s an automatic message. Let me know if there’s a specific example where it’s hard to make this delay with the current API.

silvenon avatar Apr 17 '18 12:04 silvenon

Same problem, using v2.4.0

It's more convinient to to use Transition because it provides state. For example I made HOC:

const withTransition = (inPredicate, timeout) => (BaseComponent) => {
  const factory = React.createFactory(BaseComponent);

  const WithTransition = props => (
    <Transition in={inPredicate(props)} timeout={timeout}>
      {transitionState => factory({ ...props, transitionState })}
    </Transition>
  );

  if (process.env.NODE_ENV !== 'production') {
    WithTransition.displayName = `withTransition(${BaseComponent.displayName})`;
  }

  return WithTransition;
};

And use it like so:

const Root = ({ classes, transitionState }) => (
  <div className={classNames(classes.root, classes[transitionState])}>Root</div>
);
withTransition(() => {/* some logic returns true to show component */}, 1000)(Root);

In this case we can use JSS and keep appearing logic, view and classes separated. Classes prop comes to Root, but not withTransition HOC.

P.S.: Anyway, I tried CSSTransition, it has the same delay before animation starts.

loginov-rocks avatar Jul 12 '18 00:07 loginov-rocks

Please open a new issue with a complete demo and description.

silvenon avatar Jul 12 '18 07:07 silvenon

I'm not interested in, I described case when Transition preferred over CSSTransition. This is just a reminder that the issue still exists. The issue is already described above.

timeout prop adds a delay before animation. We had to use timeout={0} as the only solution despite it's wrong approach :ok_woman:

loginov-rocks avatar Jul 13 '18 11:07 loginov-rocks

CSSTransition inherits all the properties of Transition, including state. See our demo for an example of this. Basically:

<CSSTransition in={inProp} timeout={1000} classNames="fade">
  {state => <div>{state}</div>}
</CSSTransition>

Animating the appearance is tricky. Unless you use CSSTransition or find another way to trigger a reflow yourself, there's going to be a delay. Let me know if using CSSTransition somehow isn't an option in your case.

silvenon avatar Jul 16 '18 23:07 silvenon

Thanks @silvenon !

I tried CSSTransition, but the problem was because .entering should have the same style as .entered to start animation right after state changes. So I set the same style as guys above. I'll stick with Transition temporary until find some issues with this approach. 👍

loginov-rocks avatar Jul 17 '18 13:07 loginov-rocks

I'm a bit lost in the HOC logic above, but the -active classes are the ones you need to apply the transitions to. Anyway, I hope you managed to get it working without resorting to timeout={0}.

silvenon avatar Jul 17 '18 13:07 silvenon

Yes, HOC above works with timeout. It passes state down to the component (as transitionState prop), so the component can use state value to apply different classes:

const Root = ({ classes, transitionState }) => (
  <div className={classNames(classes.root, classes[transitionState])}>Root</div>
);

As I can understand, state can be entering, entered, exiting and exited... The idea is to specify classes in the component and to have transition and other logic seprated.

loginov-rocks avatar Jul 17 '18 15:07 loginov-rocks

You shouldn't apply classes using the state, CSSTransition manages classes on its own, you should use those. I know that's not in the spirit of CSS-in-JS, but ATM you won't be able to achieve the desired result any other way.

I'm open to suggestions for a more flexible API.

silvenon avatar Jul 17 '18 17:07 silvenon