Jov*_*nka 3 next.js tailwind-css
我在下一个 js 中在顺风上创建了一个自定义颜色。在 localhost 上,定义的颜色看起来不错,但是当我部署到 vercel 时,颜色没有出现。
这是本地主机的图片
vercel生产
tailwind.config.js
const colors = require('tailwindcss/colors');
module.exports = {
purge: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}'
],
darkMode: false, // or 'media' or 'class'
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
black: {
DEFAULT: '#23232D'
},
white: colors.white,
gray: {
DEFAULT: '#A1A1A1',
},
...
}
},
variants: {
extend: {},
},
plugins: [],
}
Run Code Online (Sandbox Code Playgroud)
按钮颜色/index.js
import PropTypes from 'prop-types';
import { motion } from 'framer-motion';
function ButtonColor({ color, isOpen, onClick }) {
const variants = {
open: { width: '100%' },
closed: { width: '50%' },
}
return (
<motion.div
className={`bg-${color} h-6 cursor-pointer`}
onClick={onClick}
animate={isOpen ? "open" : "closed"}
variants={variants}
>
</motion.div>
)
}
ButtonColor.propTypes = {
color: PropTypes.string.isRequired,
isOpen: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
}
export default ButtonColor;
Run Code Online (Sandbox Code Playgroud)
这种情况有什么解决方案吗?谢谢。
您不能使用字符串连接来创建 CSS 类名,因为 PurgeCSS 不知道在构建过程中保留您的自定义类。
className={`${color === 'red' ? 'bg-red' : 'bg-blue'} h-6 cursor-pointer`}
Run Code Online (Sandbox Code Playgroud)
或者,您可以在 Next.js 中启用SCSS并创建自定义全局 SCSS文件。在该文件中,通过定义您的样式tailwindcss指令而故意不在包装您的样式@layer指令。不使用该@layer指令会告诉 Tailwind 在构建时不要清除这些类。您需要自己管理这些类的清除。
global.scss
button {
@apply h-6;
@apply cursor-pointer;
&.red{
@apply bg-red-700 dark:bg-red-900;
@apply text-white;
@apply hover:bg-red-800 dark:hover:bg-red-800;
}
&.gray {
@apply bg-gray-300 dark:bg-gray-600;
@apply text-gray-900 dark:text-gray-200;
@apply hover:bg-gray-400 dark:hover:bg-gray-500;
}
}
Run Code Online (Sandbox Code Playgroud)
<motion.button className={`bg-${color}`}> ...
Run Code Online (Sandbox Code Playgroud)
旁注 -motion.div应该是motion.button
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |