I’m have an issue where buttons and links jitter on state change (when clicking on the "Sign Out" button). I tried both v-if and v-show and the jitter persists.
Video of the issue: https://www.veed.io/view/89672f51-f55c-411c-883f-440b02cfa4de?panel=share
Reproduction: https://stackblitz.com/edit/nuxt-starter-a8hntf?file=app%2Fcomposables%2FuseStoreAuth.ts.
<div>
<div>
<button @click="setColorTheme()">
<IconDarkMode />
</button>
<div v-if="!signedIn && !pending">
<NuxtLink to="/sign-in">
<span>Sign In</span>
<IconSignIn />
</NuxtLink>
</div>
</div>
<div
v-if="signedIn && !pending">
<NuxtLink to="/profile">
<span>Profile</span>
<IconProfile />
</NuxtLink>
<button to="/sign-in" @click="signOut">
<span>Sign Out</span>
<IconSignOut />
</button>
</div>
</div>
I also have a 300ms delay for signedIn
state in signOut function to wait until page transition ends, but jitter still persists.
if (res.ok) {
const data = await res.json();
state.successMessage = data.message;
if (route.path === '/') {
window.location.reload();
} else {
window.location.href = '/';
}
setTimeout(() => {
state.signedIn = data.signedIn;
}, 300);
}
Looking for a possible solution.
2
Answers
The solution is recommended @Estus Flask in the comments above.
Regarding the UI element jitter:
Regarding the buttons changing before page transition starts:
I think the issue is the button’s element prop "to" with the click event.
here is an example of logic you should try:
First handle your logout logic, on success you handle the route and other states, don’t use the
to="sign-in"
if your logic in theclick
is a asynchronous function.I also recommend adding a fade animation, on the button, it will help prevent the jitter.