Skip to content

Commit

Permalink
Merge pull request #62 from Ferlab-Ste-Justine/loading-buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviercperrier authored Mar 3, 2025
2 parents b00d1b4 + 7acf534 commit 9aa0927
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
17 changes: 14 additions & 3 deletions frontend/components/base/Buttons/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import React from "react";
import { VariantProps } from "tailwind-variants";
import { iconButtonVariants } from "./button.variants";
import { LucideProps } from "lucide-react";
import { Spinner } from "../spinner";

export type IconButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> &
VariantProps<typeof iconButtonVariants> & {
icon: React.ForwardRefExoticComponent<
Omit<LucideProps, "ref"> & React.RefAttributes<SVGSVGElement>
>;
iconClassName?: string;
loading?: boolean;
};

const IconButton = ({
Expand All @@ -17,14 +19,23 @@ const IconButton = ({
variant,
className,
iconClassName,

loading = false,
disabled = false,
...props
}: IconButtonProps) => {
const style = iconButtonVariants({ size, variant });

return (
<button {...props} className={style.base({ className })}>
<Icon className={style.icon({ className: iconClassName })} />
<button
{...props}
className={style.base({ className })}
disabled={disabled || loading}
>
{loading ? (
<Spinner className={style.icon({ className: iconClassName })} />
) : (
<Icon className={style.icon({ className: iconClassName })} />
)}
</button>
);
};
Expand Down
28 changes: 26 additions & 2 deletions frontend/components/base/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,43 @@ import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { buttonVariants } from "../Buttons";
import { VariantProps } from "tailwind-variants";
import { Spinner } from "../spinner";

export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
loading?: boolean;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
(
{
className,
variant,
size,
children,
asChild = false,
loading = false,
disabled = false,
...props
},
ref
) => {
const Comp = asChild ? Slot : "button";
const style = buttonVariants({ variant, size });

return <Comp className={style.base({ className })} ref={ref} {...props} />;
return (
<Comp
ref={ref}
className={style.base({ className })}
disabled={disabled || loading}
{...props}
>
{loading && <Spinner />}
{children}
</Comp>
);
}
);
Button.displayName = "Button";
Expand Down

0 comments on commit 9aa0927

Please sign in to comment.