-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathbutton.tsx
More file actions
107 lines (101 loc) · 3.44 KB
/
Copy pathbutton.tsx
File metadata and controls
107 lines (101 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";
import { cn } from "@/lib/utils";
const buttonVariants = cva(
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-lg text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:opacity-50 disabled:pointer-events-none disabled:select-none [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary-hover",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-border hover:bg-muted hover:text-accent-foreground disabled:bg-muted disabled:!border-muted",
primary:
"border bg-background text-secondary-foreground hover:bg-muted hover:shadow-sm",
warning: "bg-warning text-warning-foreground hover:bg-warning/90",
secondary:
"border border-muted bg-muted text-secondary-foreground hover:bg-secondary-foreground/5",
ghost:
"text-foreground hover:bg-accent hover:text-accent-foreground disabled:!bg-transparent",
ghostActive:
"bg-muted text-foreground hover:bg-secondary-hover hover:text-accent-foreground",
link: "underline-offset-4 hover:underline text-primary",
},
size: {
default: "h-10 py-2 px-4",
md: "h-8 py-2 px-4",
sm: "h-9 px-3 rounded-md",
xs: "py-0.5 px-3 rounded-md",
lg: "h-11 px-8 rounded-md",
iconMd: "p-1.5 rounded-md",
icon: "p-1 rounded-md",
iconSm: "p-0.5 rounded-md",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
},
);
function toTitleCase(text: string) {
return text
?.split(" ")
?.map(
(word) => word?.charAt(0)?.toUpperCase() + word?.slice(1)?.toLowerCase(),
)
?.join(" ");
}
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
loading?: boolean;
ignoreTitleCase?: boolean;
}
function Button({
className,
variant,
size,
loading,
disabled,
asChild = false,
children,
ignoreTitleCase = false,
ref,
...props
}: ButtonProps & React.RefAttributes<HTMLButtonElement>) {
const Comp = asChild ? Slot : "button";
let newChildren = children;
if (typeof children === "string") {
newChildren = ignoreTitleCase ? children : toTitleCase(children);
}
const shouldScale = props["aria-haspopup"] !== "dialog";
return (
<Comp
className={cn(
buttonVariants({ variant, size, className }),
shouldScale && "active:scale-[0.97]",
)}
disabled={loading || disabled}
ref={ref}
{...props}
>
{loading ? (
<span className="relative flex items-center justify-center">
<span className="invisible flex items-center justify-center gap-2">
{newChildren}
</span>
<span className="absolute inset-0 flex items-center justify-center">
<div className="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent" />
</span>
</span>
) : (
newChildren
)}
</Comp>
);
}
export { Button, buttonVariants };