diff --git a/src/components/button.tsx b/src/components/button.tsx new file mode 100644 index 0000000..c9531bf --- /dev/null +++ b/src/components/button.tsx @@ -0,0 +1,116 @@ +"use client"; +import { ReactNode } from "react"; +import Link from "next/link"; + +interface IButtonProps { + title?: string; + size?: "sm" | "md" | "lg"; + variant?: "filled" | "outline" | "text"; + bgColor?: string; + textColor?: string; + borderColor?: string; + borderRadius?: string; + borderWidth?: string; + padding?: string; + icon?: ReactNode; + iconPosition?: "left" | "right"; + as?: "button" | "link"; + href?: string; + onClick?: () => void; + className?: string; + ariaLabel?: string; + type?: "button" | "submit" | "reset"; +} + +const Button = ({ + title, + size = "md", + variant = "filled", + bgColor = "bg-white", + textColor = "text-black", + borderColor = "transparent", + borderRadius = "rounded", + borderWidth = "border", + padding = "px-4 py-2", + icon, + iconPosition = "left", + as = "button", + href, + onClick, + className = "", + ariaLabel, + type = "button", +}: IButtonProps) => { + const sizeMap: Record<"sm" | "md" | "lg", string> = { + sm: "px-3 py-1.5 text-sm", + md: "px-4 py-2 text-base", + lg: "px-5 py-3 text-lg", + }; + const variantMap: Record<"filled" | "outline" | "text", string> = { + filled: `${bgColor} ${textColor} ${borderColor}`, + outline: `bg-transparent ${textColor} ${borderColor}`, + text: `bg-transparent ${textColor} border-transparent`, + }; + + const baseStyle = [ + "inline-flex items-center justify-center font-medium", + sizeMap[size], + variantMap[variant], + padding, + borderRadius, + borderWidth, + className, + ].join(" "); + + const isIconOnly = icon && (!title || title.trim() === ""); + + const content = isIconOnly ? ( + {icon} + ) : ( + <> + {icon && iconPosition === "left" && ( + {icon} + )} + {title} + {icon && iconPosition === "right" && ( + {icon} + )} + + ); + + if (as === "link" && href) { + const isExternalLink = + href.startsWith("http") || + href.startsWith("mailto:") || + href.startsWith("tel:"); + return ( + + {content} + + ); + } + + if (as === "button") { + return ( + + ); + } + + return null; +}; + +export default Button;