|
1
|
import { cva, type VariantProps } from "cva" |
|
2
|
import { Slot } from "radix-ui" |
|
3
|
import * as React from "react" |
|
4
|
|
|
5
|
import { clx } from "@/utils/clx" |
|
6
|
import { Spinner } from "@medusajs/icons" |
|
7
|
|
|
8
|
const buttonVariants = cva({ |
|
9
|
base: clx( |
|
10
|
"transition-fg relative inline-flex w-fit items-center justify-center overflow-hidden rounded-md outline-none", |
|
11
|
"disabled:bg-ui-bg-disabled disabled:border-ui-border-base disabled:text-ui-fg-disabled disabled:shadow-buttons-neutral disabled:after:hidden", |
|
12
|
"after:transition-fg after:absolute after:inset-0 after:content-['']" |
|
13
|
), |
|
14
|
variants: { |
|
15
|
variant: { |
|
16
|
primary: clx( |
|
17
|
"shadow-buttons-inverted text-ui-contrast-fg-primary bg-ui-button-inverted after:button-inverted-gradient", |
|
18
|
"hover:bg-ui-button-inverted-hover hover:after:button-inverted-hover-gradient", |
|
19
|
"active:bg-ui-button-inverted-pressed active:after:button-inverted-pressed-gradient", |
|
20
|
"focus-visible:!shadow-buttons-inverted-focus" |
|
21
|
), |
|
22
|
secondary: clx( |
|
23
|
"shadow-buttons-neutral text-ui-fg-base bg-ui-button-neutral after:button-neutral-gradient", |
|
24
|
"hover:bg-ui-button-neutral-hover hover:after:button-neutral-hover-gradient", |
|
25
|
"active:bg-ui-button-neutral-pressed active:after:button-neutral-pressed-gradient", |
|
26
|
"focus-visible:shadow-buttons-neutral-focus" |
|
27
|
), |
|
28
|
transparent: clx( |
|
29
|
"after:hidden", |
|
30
|
"text-ui-fg-base bg-ui-button-transparent", |
|
31
|
"hover:bg-ui-button-transparent-hover", |
|
32
|
"active:bg-ui-button-transparent-pressed", |
|
33
|
"focus-visible:shadow-buttons-neutral-focus focus-visible:bg-ui-bg-base", |
|
34
|
"disabled:!bg-transparent disabled:!shadow-none" |
|
35
|
), |
|
36
|
danger: clx( |
|
37
|
"shadow-buttons-colored shadow-buttons-danger text-ui-fg-on-color bg-ui-button-danger after:button-danger-gradient", |
|
38
|
"hover:bg-ui-button-danger-hover hover:after:button-danger-hover-gradient", |
|
39
|
"active:bg-ui-button-danger-pressed active:after:button-danger-pressed-gradient", |
|
40
|
"focus-visible:shadow-buttons-danger-focus" |
|
41
|
), |
|
42
|
}, |
|
43
|
size: { |
|
44
|
small: "txt-compact-small-plus gap-x-1.5 px-2 py-1", |
|
45
|
base: "txt-compact-small-plus gap-x-1.5 px-3 py-1.5", |
|
46
|
large: "txt-compact-medium-plus gap-x-1.5 px-4 py-2.5", |
|
47
|
xlarge: "txt-compact-large-plus gap-x-1.5 px-5 py-3.5", |
|
48
|
}, |
|
49
|
}, |
|
50
|
defaultVariants: { |
|
51
|
size: "base", |
|
52
|
variant: "primary", |
|
53
|
}, |
|
54
|
}) |
|
55
|
|
|
56
|
interface ButtonProps |
|
57
|
extends React.ComponentPropsWithoutRef<"button">, |
|
58
|
VariantProps<typeof buttonVariants> { |
|
59
|
isLoading?: boolean |
|
60
|
asChild?: boolean |
|
61
|
} |
|
62
|
|
|
63
|
/** |
|
64
|
* This component is based on the `button` element and supports all of its props |
|
65
|
*/ |
|
66
|
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>( |
|
67
|
( |
|
68
|
{ |
|
69
|
/** |
|
70
|
* The button's style. |
|
71
|
*/ |
|
72
|
variant = "primary", |
|
73
|
/** |
|
74
|
* The button's size. |
|
75
|
*/ |
|
76
|
size = "base", |
|
77
|
className, |
|
78
|
/** |
|
79
|
* Whether to remove the wrapper `button` element and use the |
|
80
|
* passed child element instead. |
|
81
|
*/ |
|
82
|
asChild = false, |
|
83
|
children, |
|
84
|
/** |
|
85
|
* Whether to show a loading spinner. |
|
86
|
*/ |
|
87
|
isLoading = false, |
|
88
|
disabled, |
|
89
|
...props |
|
90
|
}: ButtonProps, |
|
91
|
ref |
|
92
|
) => { |
|
93
|
const Component = asChild ? Slot.Root : "button" |
|
94
|
|
|
95
|
/** |
|
96
|
* In the case of a button where asChild is true, and isLoading is true, we ensure that |
|
97
|
* only on element is passed as a child to the Slot component. This is because the Slot |
|
98
|
* component only accepts a single child. |
|
99
|
*/ |
|
100
|
const renderInner = () => { |
|
101
|
if (isLoading) { |
|
102
|
return ( |
|
103
|
<span className="pointer-events-none"> |
|
104
|
<div |
|
105
|
className={clx( |
|
106
|
"bg-ui-bg-disabled absolute inset-0 flex items-center justify-center rounded-md" |
|
107
|
)} |
|
108
|
> |
|
109
|
<Spinner className="animate-spin" /> |
|
110
|
</div> |
|
111
|
{children} |
|
112
|
</span> |
|
113
|
) |
|
114
|
} |
|
115
|
|
|
116
|
return children |
|
117
|
} |
|
118
|
|
|
119
|
return ( |
|
120
|
<Component |
|
121
|
ref={ref} |
|
122
|
{...props} |
|
123
|
className={clx(buttonVariants({ variant, size }), className)} |
|
124
|
disabled={disabled || isLoading} |
|
125
|
> |
|
126
|
{renderInner()} |
|
127
|
</Component> |
|
128
|
) |
|
129
|
} |
|
130
|
) |
|
131
|
Button.displayName = "Button" |
|
132
|
|
|
133
|
export { Button, buttonVariants } |