#View transition with effect of close navbar

3 messages · Page 1 of 1 (latest)

opaque hatchBOT
#

Hi @edgy wraith. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I understand you're having an issue with your mobile navbar and view transitions in Astro. You want the navbar to close first before the page transition happens when navigating between pages.

This is a common challenge when working with Astro's view transitions. To solve this problem, you can use Astro's lifecycle events to control the sequence of actions during navigation.

Specifically, you can use the astro:before-preparation event, which fires at the beginning of navigation before any content is loaded. This would be the perfect place to close your navbar before the transition begins.

Here's how you could implement this:

<script>
  document.addEventListener("astro:before-preparation", () => {
    // Code to close your navbar
    // For example:
    document.querySelector(".nav-links").classList.remove("expanded");
    // Or whatever class/method you use to close your navbar
  });
</script>

You can place this script in your layout component or in a common component that's included on all pages. This will ensure that whenever a navigation starts, your navbar closes first before the transition animation begins.

If you need more control over the navigation process, you can also use other lifecycle events like astro:after-preparation or astro:before-swap depending on exactly when you want the navbar to close in the transition sequence.

For more information about these lifecycle events and how they work with view transitions, you can check the View Transitions guide in the Astro documentation.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@edgy wraith Kapa.ai is still learning and improving, please let me know how I did by reacting below.