Keep it together
This function allows you to create a styled component with different variants based on the props you pass to it.
interface AlertProps {
$severity: "info" | "warning" | "error"
$isActive?: boolean
}
const Alert = rc.div.variants<AlertProps>({
base: "p-4 rounded-md",
variants: {
$severity: {
info: (p) => `bg-blue-100 text-blue-800 ${p.$isActive ? "shadow-lg" : ""}`,
warning: "bg-yellow-100 text-yellow-800",
error: "bg-red-100 text-red-800",
},
},
defaultVariants: {
$severity: "info",
},
})
base
- optional - The base classname of the componentvariants
- required - An object with the different variantsdefaultVariants
- optional - a fallback for when no variant is passed<Alert $severity="warning" $isActive>Warning alert</Alert>
// renders:
// <button type="button" class="p-4 rounded-md bg-yellow-100 text-yellow-800 font-bold">Warning alert</button>