ldc-bien-etre-animal/resources/ts/front/Components/Layout/CtaButton.tsx
2026-09-01 15:04:11 +02:00

113 lines
2.6 KiB
TypeScript

import { Link } from "@inertiajs/react";
import type { ReactNode } from "react";
type CtaButtonSize = "sm" | "md" | "lg";
type CtaButtonVariant = "filled" | "outline";
interface CtaButtonProps {
children: ReactNode;
href?: string;
onClick?: () => void;
icon?: ReactNode;
iconPosition?: "left" | "right";
color?: "bleu" | "rouge";
variant?: CtaButtonVariant;
size?: CtaButtonSize;
width?: string;
type?: "button" | "submit" | "reset";
rounded?: string;
className?: string;
}
const SIZE_CLASSES: Record<CtaButtonSize, string> = {
sm: "px-4 py-2.5 gap-2",
md: "px-6 py-3.5 gap-3",
lg: "px-8 py-4.5 gap-4",
};
const ICON_SIZE: Record<CtaButtonSize, number> = {
sm: 18,
md: 24,
lg: 28,
};
const COLOR_CLASSES: Record<
"bleu" | "rouge",
Record<CtaButtonVariant, string>
> = {
bleu: {
filled: "bg-bleu hover:bg-bleu/90",
outline: "border-4 border-bleu bg-white hover:bg-bleu/5",
},
rouge: {
filled: "bg-rouge hover:bg-rouge/90",
outline: "border-4 border-rouge bg-white hover:bg-rouge/5",
},
};
export default function CtaButton({
children,
href,
onClick,
icon,
iconPosition = "left",
color = "bleu",
variant = "filled",
size = "md",
width = "",
type = "button",
rounded = "rounded-full",
className = "",
}: Readonly<CtaButtonProps>) {
const colorClasses = COLOR_CLASSES[color][variant];
const textColorClass =
variant === "outline"
? color === "rouge"
? "text-rouge"
: "text-bleu"
: "text-white";
const iconSize = ICON_SIZE[size];
const renderIconWrapper = () => {
if (!icon) return null;
return (
<span
className={`shrink-0 inline-flex items-center justify-center [&>svg]:w-full [&>svg]:h-full ${textColorClass}`}
style={{ width: iconSize, height: iconSize }}
>
{icon}
</span>
);
};
const resolvedIcon = renderIconWrapper();
const content = (
<>
{resolvedIcon && iconPosition === "left" && resolvedIcon}
<span
className={`font-poppins-bold text-base lg:text-cta ${textColorClass}`}
>
{children}
</span>
{resolvedIcon && iconPosition === "right" && resolvedIcon}
</>
);
const classes = `inline-flex items-center justify-center ${rounded} transition-colors cursor-pointer ${SIZE_CLASSES[size]} ${colorClasses} ${width} ${className}`;
if (href) {
return (
<Link href={href} className={classes}>
{content}
</Link>
);
}
return (
<button type={type} onClick={onClick} className={classes}>
{content}
</button>
);
}