I'm trying to create a simple button component in a Next App using Tailwind CSS where I'm passing some values as background, color, etc in the button props and I want to get this values in my component and then render my button with that props. I've more than one type of button in my app.
That's my colors options:
export type ColorOptions =
| "light"
| "primary"
| "secondary"
| "terciary"
| "details";
Here is my button component:
import { ButtonHTMLAttributes } from "react";
import { ColorOptions, FontOptions } from "@/app/@types/theme";
type ButtonNativeProps = ButtonHTMLAttributes<HTMLButtonElement>;
type ButtonProps = ButtonNativeProps & {
children: React.ReactNode;
onClick: () => void;
color?: ColorOptions;
background?: ColorOptions;
font?: FontOptions;
fontSize?: string;
icon?: React.ReactNode;
styleProps?: string;
};
export default function Button({
children,
onClick,
color = "light",
background = "terciary",
font = "main",
fontSize = "xl",
icon,
styleProps,
...props
}: ButtonProps) {
return (
<button
onClick={onClick}
className={`font-${font} bg-${background}-100 text-${color}-100 text-${fontSize} flex flex-row px-6 py-3 rounded-small font-bold tracking-wide transition hover:bg-${background}-75 animation ${styleProps}`}
{...props}
>
{children}
{icon}
</button>
);
}
And my tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/pages/**/*.{js,ts,jsx,tsx,mdx}",
"./src/components/**/*.{js,ts,jsx,tsx,mdx}",
"./src/app/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {
colors: {
light: {
100: "#FFFFFF",
},
primary: {
100: "#383436",
75: "rgba(56, 52, 54, 0.75)",
50: "rgba(56, 52, 54, 0.5)",
},
secondary: {
100: "#3891A6",
75: "rgba(56, 145, 166, 0.75)",
50: "rgba(56, 145, 166, 0.5)",
},
terciary: {
100: "#DB5461",
75: "rgba(219, 84, 97, 0.75)",
50: "rgba(219, 84, 97, 0.5)",
},
details: {
100: "#61ED46",
75: "rgba(97, 237, 70, 0.75)",
50: "rgba(97, 237, 70, 0.5)",
},
},
fontFamily: {
title: ["Jost", "serif"],
main: ["Nunito Sans", "serif"],
code: ["Fira Code", "sans-serif"],
},
borderRadius: {
small: "0.625rem",
large: "1.25rem",
},
},
},
plugins: [],
darkMode: "class",
};
I'm having the following first result Initial return
But if I put a value in the button component as here my result just disappears
<Button background="details" onClick={() => {}} />
My component don't have the background color
The worst is that the class is present in my html element, as we can check on browser's elements tree.Classes in my component with correctly values