0

I'm using react-transition-group for animations in my app.

I have a Popup where I want to animate between two contents. I want to make the first content fade out and the second content appear in its place.

So far I have a problem where while animating the second content appears below the first one while the first one is still visible. (See the screenshot)

Any help will be much appreciated.

enter image description here

My code

const [stage, setStage] = useState('register');

  function changeStage(stageVal) {
    setStage(stageVal);
  }
...
<Popup togglePopupWindow={togglePopupWindow} setStage={setStage}>
        <CSSTransitionGroup
          transitionName="example"
          transitionEnterTimeout={500}
          transitionLeaveTimeout={300}
        >
          {stage === 'register' && (
          <>
            <Form>
              <h2>Register</h2>
              <Input type="text" placeholder="Login" />
              <Input type="email" placeholder="Email" />
              <Input type="password" placeholder="Password" />
              <Button text="Register" />
            </Form>
            <p>
              <span>Already registered?</span>
              <Button className="popup__login" text="Login" onClick={() => changeStage('login')} />
            </p>
        </>
          )
      }
          {stage === 'login' && (
          <Form>
            <h2>Login</h2>
            <Input type="text" placeholder="Login" />
            <Input type="password" placeholder="Password" />
            <Button text="Register" />
          </Form>
          )
   }
        </CSSTransitionGroup>
      </Popup>

Styles

.example-enter {
    opacity: 0;
    }

.example-enter.example-enter-active {
   opacity: 1;
   transition: opacity 500ms ease-in-out;
}

.example-leave {
   opacity: 1;
}

.example-leave.example-leave-active {
   opacity: 0;
   transition: opacity 300ms ease-in-out;
}
Person
  • 2,190
  • 7
  • 30
  • 58

1 Answers1

0

Try taking out .example-enter and .example-leave on each of the ...-active CSS properties.. You set .example-enter to 0 opacity and then replace the .example-enter to 1.. meaning you're setting 1 on .example-enter with your .example-enter-active after the initial declaration... so the 0 is overwritten.

.example-enter {
    opacity: 0;
    }
.example-enter-active {
   opacity: 1;
   transition: opacity 500ms ease-in-out;
}

.example-leave {
   opacity: 1;
}

.example-leave-active {
   opacity: 0;
   transition: 
}
Bryan
  • 274
  • 3
  • 9
  • Looking at the DOM what is happening is that the second form appears below the first one before the fist one disappears – Person Jun 02 '19 at 08:14