Skip to content

feat: button component #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
116 changes: 116 additions & 0 deletions src/components/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"use client";
import { ReactNode } from "react";
import Link from "next/link";

interface IButtonProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
interface IButtonProps {
interface IButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {

By doing this we could remove the props from lines 19-22. Consider changing it so we can clean up the code a bit.

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";
Comment on lines +19 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you follow the precious suggestion, you'll want to remove this.

Suggested change
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",
Comment on lines +28 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these props are replaceable for the className

icon,
iconPosition = "left",
as = "button",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need that as I will explain later

Suggested change
as = "button",

href,
onClick,
className = "",
ariaLabel,
type = "button",
Comment on lines +39 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may remove this since you'll have the button props extended into the interface, and it already includes everything.

Suggested change
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`,
};
Comment on lines +49 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove these props, remove also the variantMap.


const baseStyle = [
"inline-flex items-center justify-center font-medium",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"inline-flex items-center justify-center font-medium",
"font-medium rounded-md transition-colors duration-200 cursor-pointer border",

sizeMap[size],
variantMap[variant],
padding,
borderRadius,
borderWidth,
className,
Comment on lines +57 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sizeMap[size],
variantMap[variant],
padding,
borderRadius,
borderWidth,
className,
sizeMap[size],

].join(" ");

const isIconOnly = icon && (!title || title.trim() === "");

const content = isIconOnly ? (
<span className="flex items-center justify-center">{icon}</span>
) : (
<>
{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}
aria-label={ariaLabel}
{...(isExternalLink
? { target: "_blank", rel: "noopener noreferrer" }
: {})}
>
{content}
</Link>
);
}

if (as === "button") {
return (
<button
type={type}
onClick={onClick}
className={baseStyle}
aria-label={ariaLabel}
>
{content}
</button>
);
}

return null;
};

Comment on lines +82 to +115
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const isExternalLink =
href.startsWith("http") ||
href.startsWith("mailto:") ||
href.startsWith("tel:");
return (
<Link
href={href}
className={baseStyle}
aria-label={ariaLabel}
{...(isExternalLink
? { target: "_blank", rel: "noopener noreferrer" }
: {})}
>
{content}
</Link>
);
}
if (as === "button") {
return (
<button
type={type}
onClick={onClick}
className={baseStyle}
aria-label={ariaLabel}
>
{content}
</button>
);
}
return null;
};
if (href) {
const isExternalLink =
href.startsWith("http") ||
href.startsWith("mailto:") ||
href.startsWith("tel:");
return (
<Link
href={href}
className={baseStyle}
aria-label={ariaLabel}
{...(isExternalLink
? { target: "_blank", rel: "noopener noreferrer" }
: {})}
>
{content}
</Link>
);
} else {
return (
<button
type={type}
onClick={onClick}
className={baseStyle}
aria-label={ariaLabel}
>
{content}
</button>
);
}
};

export default Button;