Jump to content
Search Community

Animate Navbar in and out

TaiwoJazz test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi guys, I'm trying to animate my navbar in and out but I'm getting an error on animation.play()
The component I'm trying to animate is in the Menu component while the buttons that will control the animations are in the Navbar component which happens to be the parent component to the Menu component..

Here is a minimal demo on codesandbox.. Thanks.. @Rodrigo

Link to comment
Share on other sites

  • Solution

Hi,

 

The main issue here is what is controlling the state that actually renders the navbar:

{active && <Menu showMenu={handleShowMenu} closeMenu={handleCloseMenu} />}

So as soon as active is falsy the navbar is not rendered any more, so no out transition.

 

One alternative is to control that using the set state method, using an onComplete callback once the out animation is done:

https://codesandbox.io/s/animate-navbar-in-and-out-forked-xi1boj?file=/src/components/Navbar.jsx

 

Another alternative is to use React Transition Group to control that using GSAP for the animations in the onEnter/onExit hooks or the addEndListener method:

https://reactcommunity.org/react-transition-group/transition

 

Let us know if you have more questions.

 

Happy Tweening!

Link to comment
Share on other sites

Hi @Rodrigo I must commend you for helping me out here... I really appreciate it... I have checked your solution and it's pretty impressive... There is no way in hell I'm was ever going to come up with that logic... I will seriously take the time to study your solution... You are the best... Thanks a lot...

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...