81 lines · 3.3 KBRaw
| 1 | import { CircleNotchIcon } from "../icons"; |
| 2 | import type { ComponentProps, ReactNode } from "react"; |
| 3 | import { Link } from "../provider"; |
| 4 | import { cn } from "./cn"; |
| 5 | |
| 6 | export type ButtonVariant = "primary" | "secondary" | "ghost" | "destructive" | "secondary-destructive"; |
| 7 | export type ButtonSize = "sm" | "base" | "lg"; |
| 8 | |
| 9 | interface Look { |
| 10 | variant?: ButtonVariant; |
| 11 | size?: ButtonSize; |
| 12 | shape?: "square"; |
| 13 | icon?: ReactNode; |
| 14 | loading?: boolean; |
| 15 | } |
| 16 | |
| 17 | const VARIANT: Record<ButtonVariant, string> = { |
| 18 | primary: "bg-primary text-on-primary shadow-[inset_0_1px_0_rgb(255_255_255/0.16),0_1px_2px_rgb(0_0_0/0.2)] hover:bg-primary-hover", |
| 19 | secondary: "bg-control text-ink shadow-control hover:bg-control-hover", |
| 20 | ghost: "bg-transparent text-muted hover:bg-hover hover:text-ink", |
| 21 | destructive: "bg-danger text-white shadow-[inset_0_1px_0_rgb(255_255_255/0.16),0_1px_2px_rgb(0_0_0/0.2)] hover:brightness-110", |
| 22 | "secondary-destructive": "bg-control text-danger shadow-[0_0_0_1px_color-mix(in_oklab,var(--danger)_45%,transparent)] hover:bg-danger-tint", |
| 23 | }; |
| 24 | |
| 25 | const SIZE: Record<ButtonSize, string> = { |
| 26 | sm: "h-7 gap-1.5 rounded-chip px-2.5 text-compact pointer-coarse:h-9", |
| 27 | base: "h-8 gap-1.5 rounded-control px-3 text-compact pointer-coarse:h-10", |
| 28 | lg: "h-10 gap-2 rounded-control px-4 text-callout pointer-coarse:h-12", |
| 29 | }; |
| 30 | |
| 31 | const SQUARE: Record<ButtonSize, string> = { sm: "size-7 px-0 pointer-coarse:size-9", base: "size-8 px-0 pointer-coarse:size-10", lg: "size-10 px-0 pointer-coarse:size-12" }; |
| 32 | |
| 33 | function classes({ variant = "secondary", size = "base", shape }: Look, className?: string): string { |
| 34 | return cn( |
| 35 | "pressable inline-flex shrink-0 cursor-pointer items-center justify-center font-medium whitespace-nowrap select-none", |
| 36 | "disabled:cursor-not-allowed disabled:opacity-45 aria-disabled:cursor-not-allowed aria-disabled:opacity-45", |
| 37 | "[&_svg]:size-4 [&_svg]:shrink-0 [&_svg]:opacity-80", |
| 38 | VARIANT[variant], |
| 39 | SIZE[size], |
| 40 | shape === "square" && SQUARE[size], |
| 41 | className, |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | function Content({ icon, loading, children }: { icon?: ReactNode; loading?: boolean; children?: ReactNode }) { |
| 46 | return ( |
| 47 | <> |
| 48 | {loading ? <CircleNotchIcon className="animate-spin" aria-hidden /> : icon} |
| 49 | {children} |
| 50 | </> |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | export type ButtonProps = Look & ComponentProps<"button">; |
| 55 | |
| 56 | /** |
| 57 | * A pressable control. Primary is the one ink action on a surface; secondary sits on a soft ring. On a touch |
| 58 | * screen every size grows, because a finger needs a larger target than a pointer. |
| 59 | */ |
| 60 | export function Button({ variant, size, shape, icon, loading = false, className, children, disabled, type = "button", ...props }: ButtonProps) { |
| 61 | return ( |
| 62 | <button type={type} disabled={disabled || loading} aria-busy={loading || undefined} className={classes({ variant, size, shape }, className)} {...props}> |
| 63 | <Content icon={icon} loading={loading}> |
| 64 | {children} |
| 65 | </Content> |
| 66 | </button> |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | export type LinkButtonProps = Look & ComponentProps<"a"> & { href: string }; |
| 71 | |
| 72 | /** A link styled as a button; it navigates through the app's router. */ |
| 73 | export function LinkButton({ variant, size, shape, icon, loading, className, children, href, ...props }: LinkButtonProps) { |
| 74 | return ( |
| 75 | <Link href={href} className={classes({ variant, size, shape }, className)} {...props}> |
| 76 | <Content icon={icon} loading={loading}> |
| 77 | {children} |
| 78 | </Content> |
| 79 | </Link> |
| 80 | ); |
| 81 | } |