I am looking to create an animated tab using React and Tailwind CSS. Here is the code I have so far:
import React from 'react'
import clsx from 'clsx'
export const Modal = () => {
const [theme, setTheme] = React.useState<'light' | 'dark' | 'system'>('light')
return (
<div className="flex mx-2 mt-2 rounded-md bg-blue-gray-100">
<div
className={clsx('flex-1 py-1 my-2 ml-2 text-center rounded-md', {
'bg-white': theme === 'light',
'transition duration-1000 ease-out transform translate-x-10':
theme !== 'light',
})}
>
<button
className={clsx(
'w-full text-sm cursor-pointer select-none focus:outline-none',
{
'font-bold text-blue-gray-900': theme === 'light',
'text-blue-gray-600': theme !== 'light',
}
)}
onClick={() => {
setTheme('light')
}}
>
Light
</button>
</div>
<div
className={clsx('flex-1 py-1 my-2 ml-2 text-center rounded-md', {
'bg-white': theme === 'dark',
})}
>
<button
className={clsx(
'w-full text-sm cursor-pointer select-none focus:outline-none',
{
'font-bold text-blue-gray-900': theme === 'dark',
'text-blue-gray-600': theme !== 'dark',
}
)}
onClick={() => {
setTheme('dark')
}}
>
Dark
</button>
</div>
<div
className={clsx('flex-1 py-1 my-2 mr-2 text-center rounded-md', {
'bg-white': theme === 'system',
})}
>
<button
className={clsx(
'w-full text-sm cursor-pointer select-none focus:outline-none',
{
'font-bold text-blue-gray-900': theme === 'system',
'text-blue-gray-600': theme !== 'system',
}
)}
onClick={() => {
setTheme('system')
}}
>
System
</button>
</div>
</div>
)
}
However, the current implementation causes the text to move when the theme is not 'light' due to the use of translate-x-10
.
I want the UI to remain consistent while still having animated tabs like in the example above. Any suggestions on how to achieve this?
You can view a minimal Codesandbox demo here → https://codesandbox.io/s/mobx-theme-change-n1nvg?file=/src/App.tsx
Any guidance would be appreciated!