I have a sidebar with moveable panels - you can see in the snippet they move left or right when you click Menu item or Go back.
The problem is that if you click fast on the buttons (You can recreate the issue by clicking fast and many times on the buttons), it will sometimes calculate currentIndex++ (or --) twice on the same panel which will make it stuck, and then it will just keep adding translate amount to the same panel.
In my example there are only 3 panels with width 40, so it should only move somewhere between translate-x-0 to -translate-x-120, but if it gets stuck, it will simply keep adding jumps of 40 to the stuck panels, so you can see something like -translate-x-800 if you just keep clicking
const App = {
data() {
return {
sidebarWidth: 40,
currentIndex: 0,
}
},
computed: {
position() {
if (this.currentIndex === 0) {
return 'translate-x-0'
} else {
return `-translate-x-${this.sidebarWidth * this.currentIndex}`
}
}
}
}
Vue.createApp(App).mount('#app')
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/vue@next"></script>
<div id="app">
<div class="h-screen flex">
<div class="flex container w-40 h-80 bg-black mx-auto my-auto text-black overflow-hidden">
<div class="sidebar container flex transform duration-1000" :class="position">
<div class="sidebar-menu flex-shrink-0 w-40 transform duration-1000 bg-gray-300">
<button @click="currentIndex++">Menu item</button>
</div>
<div class="sidebar-menu flex-shrink-0 w-40 bg-blue-300 transform duration-1000">
<div class="flex flex-col">
<button class="flex justify-start" @click="currentIndex--">
Go back
</button>
<button class="flex justify-start" @click="currentIndex++">
Menu item
</button>
</div>
</div>
<div class="sidebar-menu flex-shrink-0 w-40 transform duration-1000 bg-red-300">
<div class="flex flex-col">
<button class="flex justify-start" @click="currentIndex--">
Go back
</button>
</div>
</div>
</div>
</div>
</div>
</div>