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