How can I conditionally include content without a jarring animation?

49 views Asked by At

In my Vue.js app, I have a structure like this:

<template>
    <Base>
        <RouterView/>
    </Base>
</template>

Where Base looks like:

<template>
    <Header :key="JSON.stringify(route.meta)" class="z-20 bg-white"></Header>
    <div class="flex flex-col h-screen">
        <div class="grow pt-20">
            <slot>
            </slot>
        </div>
        <Footer :key="JSON.stringify(route.meta)"></Footer>
    </div>
</template>

In my Header, I wish to hide certain links depending on path that the router is on.

I can do this doing something like:

<a v-if="route.path === '/pricing'">Home</a>

The problem on the first paint of the webpage, even when on /pricing, the "Home" link is not visible, until it appears rather jarringly as it shifts around other items in the page.

Is there a way to make this work on first paint? Or somehow make things less janky looking?

I know how to conditionally include elements, so I hope this questioned isn't closed by linking to one of those, I just want to conditionally include the content in a nicer way.

1

There are 1 answers

4
shruti On

To avoid a jarring effect when conditionally displaying content in your Vue app, consider using v-show for elements that frequently change visibility, as this keeps them in the DOM but simply toggles their visibility. This can prevent unexpected layout shifts. Additionally, you can use CSS to add subtle transitions, like fading, to make the appearance and disappearance of elements smoother. For example:

<a v-show="route.path === '/pricing'" class="smooth-transition">Home</a>

And in your CSS:

.smooth-transition {
  transition: opacity 0.5s ease;
}

This approach ensures that your content changes are less abrupt and more visually pleasing.