Button
Actions. One primary per view.
Motion: Scales to 0.97 on press. `loading` keeps colour and width, spinner overlays the label.
ติดตั้ง
npx shadcn@latest add @ecsight/buttonSource
components/ui/button.tsx
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Slot } from "radix-ui"
import { Spinner } from "@/components/ui/spinner"
const buttonVariants = cva(
"group/button relative inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:not-data-loading:scale-[0.97] duration-(--motion-base) ease-(--ease-out) disabled:cursor-not-allowed disabled:opacity-50 data-loading:cursor-progress aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:not-disabled:bg-primary/85",
outline:
"border-border bg-background hover:not-disabled:bg-muted hover:not-disabled:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:not-disabled:bg-input/50",
secondary:
"bg-secondary text-secondary-foreground hover:not-disabled:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)] aria-expanded:bg-secondary aria-expanded:text-secondary-foreground",
ghost:
"hover:not-disabled:bg-muted hover:not-disabled:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:not-disabled:bg-muted/50",
destructive:
"bg-destructive/10 text-destructive hover:not-disabled:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:not-disabled:bg-destructive/30 dark:focus-visible:ring-destructive/40",
link: "text-primary underline-offset-4 hover:not-disabled:underline",
},
size: {
default:
"h-(--control-h) gap-1.5 px-3 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
xs: "h-6 gap-1 rounded-[min(var(--radius-md),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
sm: "h-8 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
lg: "h-10 gap-2 px-4 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
icon: "size-(--control-h)",
"icon-xs":
"size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
"icon-sm":
"size-8 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg",
"icon-lg": "size-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
/**
* `loading` keeps the button's colour, width and focus (it is not disabled):
* the label stays in place invisibly and a spinner sits on top, so nothing
* jumps. Clicks and form submits are ignored while loading.
*/
function Button({
className,
variant = "default",
size = "default",
asChild = false,
loading = false,
children,
...props
}: React.ComponentProps<"button"> &
VariantProps<typeof buttonVariants> & {
asChild?: boolean
loading?: boolean
}) {
const Comp = asChild ? Slot.Root : "button"
const busy = loading && !asChild
return (
<Comp
data-slot="button"
data-variant={variant}
data-size={size}
data-loading={busy || undefined}
aria-busy={busy || undefined}
className={cn(buttonVariants({ variant, size, className }))}
{...props}
{...(busy && {
onClick: (e: React.MouseEvent<HTMLButtonElement>) => e.preventDefault(),
})}
>
{busy ? (
<>
<span className="invisible contents">{children}</span>
<span className="absolute inset-0 grid place-items-center">
<Spinner role="presentation" aria-hidden />
</span>
</>
) : (
children
)}
</Comp>
)
}
export { Button, buttonVariants }