Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/components/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"use client";
import React, { ReactNode } from "react";
Comment thread
rfaria107 marked this conversation as resolved.
import Link from "next/link";

interface ButtonProps {
Comment thread
rfaria107 marked this conversation as resolved.
Outdated
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;
}

const Button = ({
title,
size = "md",
variant = "filled",
bgColor = "white",
textColor = "black",
Comment thread
rfaria107 marked this conversation as resolved.
Outdated
borderColor = "transparent",
borderRadius = "rounded",
borderWidth = "border",
padding = "px-4 py-2",
icon,
iconPosition = "left",
as = "button",
Comment thread
rfaria107 marked this conversation as resolved.
Outdated
href,
onClick,
className = "",
}: ButtonProps) => {
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`,
};
Comment thread
rfaria107 marked this conversation as resolved.
Outdated

const baseStyle = [
"inline-flex items-center justify-center font-medium transition ease-in-out duration-200 hover:scale-105 active:scale-95",
Comment thread
rfaria107 marked this conversation as resolved.
Outdated
sizeMap[size],
variantMap[variant],
padding,
borderRadius,
borderWidth,
className,
Comment thread
rfaria107 marked this conversation as resolved.
Outdated
].join(" ");

const content = (
<>
{icon && iconPosition === "left" && (
<span className="mr-2 flex">{icon}</span>
)}
<span>{title}</span>
{icon && iconPosition === "right" && (
<span className="ml-2 flex">{icon}</span>
)}
</>
);

if (as === "link" && href) {
const isExternalLink =
href.startsWith("http") ||
href.startsWith("mailto:") ||
href.startsWith("tel:");
return (
<Link
href={href}
className={baseStyle}
{...(isExternalLink
? { target: "_blank", rel: "noopener noreferrer" }
: {})}
>
{content}
</Link>
);
}

if (as === "button") {
return (
<button onClick={onClick} className={baseStyle}>
Comment thread
rfaria107 marked this conversation as resolved.
Outdated
{content}
</button>
);
}

return null;
};

export default Button;