Skip to content

Instantly share code, notes, and snippets.

@devinschulz
Created February 20, 2022 01:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinschulz/0f3a522e5baec0318fb21ed13fa6ffe4 to your computer and use it in GitHub Desktop.
Save devinschulz/0f3a522e5baec0318fb21ed13fa6ffe4 to your computer and use it in GitHub Desktop.
Button component variants with TypeScript
import { FC } from "react";
import clsx from "clsx";
type ButtonSize = "small" | "medium";
type ButtonVariant = "primary" | "secondary";
interface ButtonProps {
size?: ButtonSize;
variant?: ButtonVariant;
}
const buttonSizeClasses: Record<ButtonSize, string> = {
small: "py-1 px-2",
medium: "py-2 px-4",
};
const buttonVariantClasses: Record<ButtonVariant, string> = {
primary: "bg-blue-700 text-white",
secondary: "border border-gray-300 text-black",
};
const Button: FC<ButtonProps> = ({
children,
size = "small",
variant = "primary",
}) => (
<button
className={clsx(buttonSizeClasses[size], buttonVariantClasses[variant])}
>
{children}
</button>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment