fosrl.pangolin/src/components/ui/button.tsx

75 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-11-02 23:46:08 -04:00
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
2024-10-06 09:55:45 -04:00
2025-01-01 21:41:31 -05:00
import { cn } from "@app/lib/cn";
2024-11-02 23:46:08 -04:00
import { Loader2 } from "lucide-react";
2024-10-06 09:55:45 -04:00
const buttonVariants = cva(
2025-01-04 20:22:01 -05:00
"inline-flex items-center justify-center rounded-full whitespace-nowrap text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
2024-10-06 18:05:20 -04:00
{
variants: {
variant: {
2024-11-02 23:46:08 -04:00
default:
"bg-primary text-primary-foreground hover:bg-primary/90",
2024-10-06 18:05:20 -04:00
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
2025-02-26 21:24:35 -05:00
"border-2 border-input bg-card hover:bg-accent hover:text-accent-foreground",
2024-10-06 18:05:20 -04:00
secondary:
2025-02-26 21:24:35 -05:00
"bg-secondary border border-input border-2 text-secondary-foreground hover:bg-secondary/80",
2024-10-06 18:05:20 -04:00
ghost: "hover:bg-accent hover:text-accent-foreground",
text: "",
2024-10-06 18:05:20 -04:00
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-9 px-4 py-2",
sm: "h-8 rounded-md px-3",
lg: "h-10 rounded-md px-8",
icon: "h-9 w-9",
2025-01-04 20:22:01 -05:00
}
2024-10-06 18:05:20 -04:00
},
defaultVariants: {
variant: "default",
size: "default",
},
}
2024-11-02 23:46:08 -04:00
);
2024-10-06 09:55:45 -04:00
export interface ButtonProps
2024-10-06 18:05:20 -04:00
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
2024-11-02 23:46:08 -04:00
VariantProps<typeof buttonVariants> {
asChild?: boolean;
loading?: boolean; // Add loading prop
2024-10-06 09:55:45 -04:00
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
2024-11-02 23:46:08 -04:00
(
{
className,
variant,
size,
asChild = false,
loading = false,
...props
},
ref
) => {
const Comp = asChild ? Slot : "button";
2024-10-06 18:05:20 -04:00
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
2024-11-02 23:46:08 -04:00
disabled={loading || props.disabled} // Disable button when loading
2024-10-06 18:05:20 -04:00
{...props}
2024-11-02 23:46:08 -04:00
>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{props.children}
</Comp>
);
2024-10-06 18:05:20 -04:00
}
2024-11-02 23:46:08 -04:00
);
Button.displayName = "Button";
2024-10-06 09:55:45 -04:00
2024-11-02 23:46:08 -04:00
export { Button, buttonVariants };