import * as React from "react"; import { cva, type VariantProps } from "class-variance-authority"; import { Slot } from "radix-ui"; import { cn } from "../lib/cn"; // The design system's Button under the names the pages already use: // // default → primary (Electric Blue, the one main action) // secondary → secondary (Electric Blue outline: "Annulla" beside a primary) // outline → neutral (white, navy hairline: every other action) // destructive → danger (the confirm of a removal or a revocation, in its // dialog; never the button that opens the dialog) // link → link (a text button that does something here) // ghost → the quiet button of a table row or a toolbar // // A disabled button takes the design system's disabled colours instead of a // lower opacity, and so does one that is only `aria-disabled` (it stays // focusable to explain itself, so it keeps its pointer events). // // No two classes of one call set the same property: pages also put // `buttonVariants()` on a link without `cn()`, where nothing would pick the // winner but the order of the stylesheet. So the border colour is each // variant's, the type size each size's, and the side padding (which a text // button does not share) comes from the pairs below. button.test.tsx holds it. // // The dialogs lay one variant over another (`AlertDialogCancel` is secondary, // `AlertDialogAction` primary, and a page passes `buttonVariants()` of the // one it wants). `default` and `destructive` say the same things in every // state, pressed text included, and everything `secondary` says: either one // laid over the other, or over `secondary`, replaces it whole. Not the other // way round, nor with `outline`, `ghost` or `link` on top. const FILLED = [ "default", "destructive", "outline", "secondary", "ghost", ] as const; const TEXT_SIZES = ["default", "xs", "sm", "lg"] as const; const ICON_SIZES = ["icon", "icon-xs", "icon-sm", "icon-lg"] as const; const buttonVariants = cva( "inline-flex shrink-0 items-center justify-center rounded-md border font-medium whitespace-nowrap transition-colors outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-solid focus-visible:outline-ring disabled:pointer-events-none aria-disabled:cursor-not-allowed aria-invalid:border-semantic-error-100 [&_svg]:pointer-events-none [&_svg]:shrink-0", { variants: { variant: { default: "border-primary bg-primary text-primary-foreground hover:border-primary-electric-blue-120 hover:bg-primary-electric-blue-120 active:border-primary-blue-100 active:bg-primary-blue-100 active:text-primary-foreground disabled:border-transparent disabled:bg-primary-electric-blue-10 disabled:text-primary-electric-blue-40 aria-disabled:border-transparent aria-disabled:bg-primary-electric-blue-10 aria-disabled:text-primary-electric-blue-40", destructive: "border-destructive bg-destructive text-destructive-foreground hover:border-destructive-hover hover:bg-destructive-hover active:border-primary-blue-100 active:bg-primary-blue-100 active:text-destructive-foreground disabled:border-transparent disabled:bg-semantic-error-10 disabled:text-semantic-error-40 aria-disabled:border-transparent aria-disabled:bg-semantic-error-10 aria-disabled:text-semantic-error-40", outline: "border-primary-blue-20 bg-card text-primary-blue-100 hover:border-primary-blue-40 active:bg-primary-blue-5 disabled:border-primary-blue-10 disabled:text-primary-blue-40 aria-disabled:border-primary-blue-10 aria-disabled:bg-card aria-disabled:text-primary-blue-40", // The design system fills the hover with white, which shows on the // tinted page and not on a card or in a dialog. The 10% tint shows on // both; pressed, the 20% one under the darker blue (4.9:1). secondary: "border-primary text-primary hover:bg-accent active:border-primary-electric-blue-120 active:bg-primary-electric-blue-20 active:text-primary-electric-blue-120 disabled:border-primary-electric-blue-40 disabled:text-primary-electric-blue-40 aria-disabled:border-primary-electric-blue-40 aria-disabled:bg-transparent aria-disabled:text-primary-electric-blue-40", ghost: "border-transparent hover:bg-accent active:bg-primary-electric-blue-20 disabled:text-primary-blue-40 aria-disabled:bg-transparent aria-disabled:text-primary-blue-40", link: "border-transparent text-primary underline decoration-1 underline-offset-3 hover:text-primary-electric-blue-120 hover:decoration-2 active:text-primary-blue-100 disabled:text-primary-black-40 aria-disabled:text-primary-black-40 aria-disabled:decoration-1", }, // The design system has 40, 48 and 54px. On a page every button is 40px, // in a table row and a filter bar too, so `sm` is 40px like `default`; // 48px is the main action of a form and the footer of a dialog. `xs` is // the one compact size, for a row that cannot take 40px. size: { default: "h-10 gap-2 text-base [&_svg:not([class*='size-'])]:size-4", xs: "h-8 gap-1.5 text-sm [&_svg:not([class*='size-'])]:size-3.5", sm: "h-10 gap-2 text-base [&_svg:not([class*='size-'])]:size-4", lg: "h-12 gap-2 text-base [&_svg:not([class*='size-'])]:size-4", icon: "size-10 [&_svg:not([class*='size-'])]:size-4", "icon-xs": "size-8 [&_svg:not([class*='size-'])]:size-3.5", "icon-sm": "size-10 [&_svg:not([class*='size-'])]:size-4", "icon-lg": "size-12 [&_svg:not([class*='size-'])]:size-4", }, }, compoundVariants: [ { variant: [...FILLED], size: ["default", "sm"], class: "px-4.5" }, { variant: [...FILLED], size: "lg", class: "px-5" }, { variant: [...FILLED], size: "xs", class: "px-3" }, // A text button takes no more width than its words, and stays as tall // as the others so it lines up beside them. { variant: "link", size: [...TEXT_SIZES], class: "px-1" }, // The quiet button: navy words; as an icon button a navy 80% glyph // that turns navy on hover. { variant: "ghost", size: [...TEXT_SIZES], class: "text-primary-blue-100", }, { variant: "ghost", size: [...ICON_SIZES], class: "text-muted-foreground hover:text-primary-blue-100", }, ], defaultVariants: { variant: "default", size: "default", }, }, ); function Button({ className, variant = "default", size = "default", asChild = false, ...props }: React.ComponentProps<"button"> & VariantProps & { asChild?: boolean; }) { const Comp = asChild ? Slot.Root : "button"; return ( ); } export { Button, buttonVariants };