langfuse
84 строки · 2.3 Кб
1import * as React from "react";
2import { Slot } from "@radix-ui/react-slot";
3import { cva, type VariantProps } from "class-variance-authority";
4
5import { cn } from "@/src/utils/tailwind";
6import { Loader2 } from "lucide-react";
7
8const buttonVariants = cva(
9"inline-flex items-center justify-center whitespace-nowrap rounded-md 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",
10{
11variants: {
12variant: {
13default: "bg-primary text-primary-foreground hover:bg-primary/90",
14destructive:
15"bg-destructive text-destructive-foreground hover:bg-destructive/90",
16outline:
17"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
18secondary:
19"bg-secondary text-secondary-foreground hover:bg-secondary/80",
20ghost: "hover:bg-accent hover:text-accent-foreground",
21link: "text-primary underline-offset-4 hover:underline",
22},
23size: {
24default: "h-10 px-4 py-2",
25xs: "h-6 px-1 rounded-sm",
26sm: "h-9 rounded-md px-3",
27lg: "h-11 rounded-md px-8",
28icon: "h-10 w-10",
29},
30},
31defaultVariants: {
32variant: "default",
33size: "default",
34},
35},
36);
37
38export interface ButtonProps
39extends React.ButtonHTMLAttributes<HTMLButtonElement>,
40VariantProps<typeof buttonVariants> {
41asChild?: boolean;
42loading?: boolean;
43}
44
45const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
46(
47{
48className,
49variant,
50size,
51asChild = false,
52loading = false,
53disabled,
54onClick,
55children,
56...props
57},
58ref,
59) => {
60const Comp = asChild ? Slot : "button";
61return (
62<Comp
63className={cn(buttonVariants({ variant, size, className }))}
64ref={ref}
65disabled={disabled || loading}
66onClick={loading || disabled ? undefined : onClick}
67{...props}
68>
69{loading ? <Spinner /> : children}
70</Comp>
71);
72},
73);
74Button.displayName = "Button";
75
76export { Button, buttonVariants };
77
78function Spinner() {
79return (
80<div className="flex h-1/2 items-center justify-center">
81<Loader2 className="h-full w-full animate-spin" />
82</div>
83);
84}
85