Skip to content
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

feat(components): add support for anchor tag buttons #57

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"eslint-plugin-tsdoc": "0.2.17",
"jsdom": "^22.1.0",
"prettier": "^3.0.3",
"prettier-plugin-tailwindcss": "0.5.5",
"prettier-plugin-tailwindcss": "0.5.7",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
6 changes: 6 additions & 0 deletions src/components/button/button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export const WithIcons: Story = {
RightIcon: icons.LockIcon,
},
};
export const AsAnchor: Story = {
args: {
as: "a",
href: "https://www.google.com",
},
};
export const Loading: Story = {
args: { loading: true },
};
Expand Down
35 changes: 21 additions & 14 deletions src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ const iconVariants = {
"danger-secondary": "",
};

export interface ButtonProps extends React.ComponentPropsWithoutRef<"button"> {
type ButtonOrLinkProps =
| (React.ButtonHTMLAttributes<HTMLButtonElement> & {
as?: "button";
href?: undefined;
})
| (React.AnchorHTMLAttributes<HTMLAnchorElement> & {
as: "a";
});

export type ButtonProps = {
variant?: keyof typeof buttonVariants;
loading?: boolean;
LeftIcon?: React.ElementType;
RightIcon?: React.ElementType;
}
} & ButtonOrLinkProps;

const Button = ({
variant = "primary",
Expand All @@ -41,25 +50,23 @@ const Button = ({
RightIcon,
...props
}: ButtonProps) => {
const HtmlTag = (props.as || "button") as React.ElementType;

const commonClasses = classNames(
`group flex h-8 items-center gap-2 whitespace-nowrap rounded px-4 text-xs font-semibold focus:outline-none disabled:cursor-not-allowed`,
buttonVariants[variant],
className
);

return (
<button
className={classNames(
`group flex h-8 items-center gap-2 whitespace-nowrap rounded px-4 text-xs font-semibold focus:outline-none disabled:cursor-not-allowed`,
buttonVariants[variant],
className
)}
{...props}
>
<HtmlTag className={commonClasses} {...props}>
{loading ? <Spinner size="small" /> : null}

{LeftIcon && !loading ? (
<LeftIcon className={`${iconVariants[variant]} h-3 w-3`} />
) : null}

{children}

{RightIcon ? <RightIcon className={`${iconVariants[variant]} h-3 w-3`} /> : null}
</button>
</HtmlTag>
);
};

Expand Down