| 38 | ); |
| 39 | |
| 40 | function Button({ |
| 41 | className, |
| 42 | variant = "default", |
| 43 | size = "default", |
| 44 | asChild = false, |
| 45 | loading = false, |
| 46 | disabled, |
| 47 | children, |
| 48 | ...props |
| 49 | }: React.ComponentProps<"button"> & |
| 50 | VariantProps<typeof buttonVariants> & { |
| 51 | asChild?: boolean; |
| 52 | /** Show a centered spinner while keeping the label's footprint, so the |
| 53 | * button (and any row it sits in) does not change width on entering the |
| 54 | * loading state. Ignored with `asChild` (Slot requires a single child). */ |
| 55 | loading?: boolean; |
| 56 | }) { |
| 57 | const Comp = asChild ? Slot.Root : "button"; |
| 58 | const showLoading = loading && !asChild; |
| 59 | |
| 60 | return ( |
| 61 | <Comp |
| 62 | data-slot="button" |
| 63 | data-variant={variant} |
| 64 | data-size={size} |
| 65 | data-loading={showLoading ? "" : undefined} |
| 66 | className={cn(buttonVariants({ variant, size, className }), showLoading && "relative")} |
| 67 | disabled={disabled || loading} |
| 68 | {...props} |
| 69 | > |
| 70 | {showLoading ? ( |
| 71 | <> |
| 72 | {/* Reserve the label's width so the box stays the same size; overlay |
| 73 | the spinner centered on top. */} |
| 74 | <span className="invisible">{children}</span> |
| 75 | <span className="absolute inset-0 flex items-center justify-center"> |
| 76 | <Spinner className="size-3.5" /> |
| 77 | </span> |
| 78 | </> |
| 79 | ) : ( |
| 80 | children |
| 81 | )} |
| 82 | </Comp> |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | export { Button, buttonVariants }; |