【发布时间】:2021-05-22 13:39:15
【问题描述】:
我想制作一个像这样的动画标签:
我正在使用带有 Tailwind 的 React。这是我的代码:
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>
)
}
但它看起来像:
当主题不是light 时,我使用translate-x-10,因此文本也会移动。
我希望 UI 与上面的完全一样,同时仍然使用实际选项卡的按钮。
最小代码沙盒 → https://codesandbox.io/s/mobx-theme-change-n1nvg?file=/src/App.tsx
我该怎么做?
【问题讨论】:
-
将动画移动到
<span>并应用translate-x-10怎么样? -
@markmead
span应该在哪里?我在button上方尝试过,没有包围它,但我什么也没看到。 -
@markmead 这里是代码框 → codesandbox.io/s/mobx-theme-change-n1nvg
标签: html css reactjs css-transitions tailwind-css