Open History to see the last commit
1import { mergeProps } from "@base-ui/react/merge-props";
2import { useRender } from "@base-ui/react/use-render";
3import { cva, type VariantProps } from "class-variance-authority";
4import type * as React from "react";
5
6import { cn } from "~/lib/utils";
7
8const buttonVariants = cva(
9 "[--control-icon-color:currentColor] [&_svg]:-mx-0.5 relative inline-flex shrink-0 cursor-pointer items-center justify-center gap-2 whitespace-nowrap rounded-[var(--control-radius)] border font-medium text-base outline-none transition-[box-shadow,scale] [&:not([aria-haspopup]):not(:disabled):not([aria-disabled=true])]:will-change-[scale] [&:active:not([aria-haspopup])]:scale-[0.97] before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--control-radius)-1px)] pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 aria-disabled:cursor-not-allowed aria-disabled:opacity-64 sm:text-sm [&_svg:not([class*='text-'])]:text-[var(--control-icon-color)] [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0",
10 {
11 defaultVariants: {
12 size: "default",
13 variant: "default",
14 },
15 variants: {
16 size: {
17 compact:
18 "h-7 gap-1.5 rounded-md px-[calc(--spacing(2)-1px)] text-xs before:rounded-[calc(var(--radius-md)-1px)] [&_svg:not([class*='size-'])]:size-3.5",
19 default: "h-9 px-[calc(--spacing(3)-1px)] sm:h-8",
20 icon: "size-9 sm:size-8",
21 "icon-lg": "size-10 sm:size-9",
22 "icon-micro":
23 "size-5 rounded-sm p-0 before:rounded-[calc(var(--radius-sm)-1px)] [&_svg:not([class*='size-'])]:size-3",
24 "icon-tiny": "size-4 p-0 [&_svg:not([class*='size-'])]:size-3",
25 "icon-sm": "size-8 sm:size-7",
26 "icon-sm-pill": "size-8 rounded-full before:rounded-full sm:size-7",
27 "icon-xl":
28 "size-11 sm:size-10 [&_svg:not([class*='size-'])]:size-5 sm:[&_svg:not([class*='size-'])]:size-4.5",
29 "icon-xs":
30 "size-7 sm:size-6 not-in-data-[slot=input-group]:[&_svg:not([class*='size-'])]:size-4 sm:not-in-data-[slot=input-group]:[&_svg:not([class*='size-'])]:size-3.5",
31 "icon-xs-pill":
32 "size-6 rounded-full before:rounded-full sm:size-5 [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5",
33 lg: "h-10 px-[calc(--spacing(3.5)-1px)] sm:h-9",
34 micro:
35 "h-5 gap-1.5 rounded-sm px-[calc(--spacing(1.5)-1px)] text-[11px] before:rounded-[calc(var(--radius-sm)-1px)] sm:text-[11px] [&_svg:not([class*='size-'])]:size-3 sm:[&_svg:not([class*='size-'])]:size-3",
36 multiline:
37 "min-h-9 px-[calc(--spacing(3)-1px)] py-[calc(--spacing(1.5)-1px)] whitespace-normal sm:min-h-8",
38 sm: "h-8 gap-1.5 px-[calc(--spacing(2.5)-1px)] sm:h-7",
39 "sm-touch": "h-11 gap-1.5 px-[calc(--spacing(2.5)-1px)] sm:h-7",
40 "sm-pill": "h-8 gap-1.5 rounded-full px-2.5 sm:h-7",
41 "sm-multiline":
42 "min-h-8 gap-1.5 px-[calc(--spacing(2.5)-1px)] py-[calc(--spacing(1)-1px)] whitespace-normal sm:min-h-7",
43 xl: "h-11 px-[calc(--spacing(4)-1px)] text-lg sm:h-10 sm:text-base [&_svg:not([class*='size-'])]:size-5 sm:[&_svg:not([class*='size-'])]:size-4.5",
44 xs: "h-7 gap-1.5 px-[calc(--spacing(2)-1px)] text-sm sm:h-6 sm:text-xs [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5",
45 "xs-pill":
46 "h-6 gap-1 rounded-full px-[calc(--spacing(2)-1px)] text-sm before:rounded-full sm:h-5 sm:text-xs [&_svg:not([class*='size-'])]:size-4 sm:[&_svg:not([class*='size-'])]:size-3.5",
47 },
48 variant: {
49 default:
50 "not-disabled:inset-shadow-[0_1px_--theme(--color-white/16%)] border-primary bg-primary text-primary-foreground shadow-primary/24 shadow-xs [:active,[data-pressed]]:inset-shadow-[0_1px_--theme(--color-black/8%)] [:disabled,:active,[data-pressed]]:shadow-none [:hover,[data-pressed]]:bg-primary/90",
51 destructive:
52 "not-disabled:inset-shadow-[0_1px_--theme(--color-white/16%)] border-destructive bg-destructive text-white shadow-destructive/24 shadow-xs [:active,[data-pressed]]:inset-shadow-[0_1px_--theme(--color-black/8%)] [:disabled,:active,[data-pressed]]:shadow-none [:hover,[data-pressed]]:bg-destructive/90",
53 "destructive-outline":
54 "border-input bg-popover not-dark:bg-clip-padding text-destructive-foreground shadow-xs/5 not-disabled:not-active:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:bg-input/32 dark:not-disabled:before:shadow-[0_-1px_--theme(--color-white/2%)] dark:not-disabled:not-active:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/6%)] [:disabled,:active,[data-pressed]]:shadow-none [:hover,[data-pressed]]:border-destructive/32 [:hover,[data-pressed]]:bg-destructive/4",
55 ghost:
56 "[--control-icon-color:var(--contrast-muted-foreground)] border-transparent text-foreground data-pressed:bg-accent [:hover,[data-pressed]]:bg-accent",
57 "ghost-muted":
58 "[--control-icon-color:currentColor] border-transparent text-muted-foreground data-pressed:bg-accent [:hover,[data-pressed]]:bg-accent [:hover,[data-pressed]]:text-foreground",
59 "ghost-destructive":
60 "[--control-icon-color:currentColor] border-transparent text-muted-foreground data-pressed:bg-accent [:hover,[data-pressed]]:bg-accent [:hover,[data-pressed]]:text-destructive",
61 "ghost-warning":
62 "[--control-icon-color:currentColor] border-transparent text-warning-foreground/64 [:hover,[data-pressed]]:bg-warning/24 [:hover,[data-pressed]]:text-warning-foreground",
63 glass:
64 "surface-glass [--control-icon-color:var(--contrast-muted-foreground)] rounded-full border-border/60 text-foreground shadow-sm before:rounded-full [:hover,[data-pressed]]:border-border",
65 link: "border-transparent underline-offset-4 [:hover,[data-pressed]]:underline",
66 "media-close":
67 "[--control-icon-color:currentColor] border-transparent bg-black/65 text-white shadow-sm ring-1 ring-white/20 [:hover,[data-pressed]]:bg-black/80 focus-visible:ring-white",
68 "media-navigation":
69 "[--control-icon-color:currentColor] absolute top-1/2 z-20 -translate-y-1/2 border-transparent text-white/90 [:hover,[data-pressed]]:bg-white/10 [:hover,[data-pressed]]:text-white focus-visible:ring-white focus-visible:ring-offset-transparent",
70 outline:
71 "[--control-icon-color:var(--contrast-muted-foreground)] border-input bg-popover not-dark:bg-clip-padding text-foreground shadow-xs/5 not-disabled:not-active:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:bg-input/32 dark:not-disabled:before:shadow-[0_-1px_--theme(--color-white/2%)] dark:not-disabled:not-active:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/6%)] [:disabled,:active,[data-pressed]]:shadow-none [:hover,[data-pressed]]:bg-accent/50 dark:[:hover,[data-pressed]]:bg-input/64",
72 overlay: "border-transparent bg-black/70 text-white/65 [:hover,[data-pressed]]:bg-black/90",
73 secondary:
74 "border-transparent bg-secondary text-secondary-foreground [:active,[data-pressed]]:bg-secondary/80 [:hover,[data-pressed]]:bg-secondary/90",
75 "warning-outline":
76 "border-warning/32 bg-warning-surface text-warning-foreground shadow-xs/5 [:disabled,:active,[data-pressed]]:shadow-none [:hover,[data-pressed]]:border-warning/40 [:hover,[data-pressed]]:bg-warning/16 dark:[:hover,[data-pressed]]:bg-warning/24",
77 },
78 },
79 },
80);
81
82type ButtonVariant = NonNullable<VariantProps<typeof buttonVariants>["variant"]>;
83type ButtonSize = NonNullable<VariantProps<typeof buttonVariants>["size"]>;
84
85interface ButtonProps extends useRender.ComponentProps<"button"> {
86 variant?: ButtonVariant;
87 size?: ButtonSize;
88}
89
90function Button({ className, variant, size, render, ...props }: ButtonProps) {
91 const typeValue: React.ButtonHTMLAttributes<HTMLButtonElement>["type"] = render
92 ? undefined
93 : "button";
94
95 const defaultProps = {
96 className: cn(buttonVariants({ className, size, variant })),
97 "data-slot": "button",
98 type: typeValue,
99 };
100
101 return useRender({
102 defaultTagName: "button",
103 props: mergeProps<"button">(defaultProps, props),
104 render,
105 });
106}
107
108export { Button, buttonVariants, type ButtonSize, type ButtonVariant };
109
110const inlineButtonVariants = cva(
111 "inline-flex shrink-0 cursor-pointer items-center gap-0.5 font-medium underline-offset-2 [text-align:var(--inline-button-text-align,center)] [white-space:var(--inline-button-white-space,nowrap)] hover:underline focus-visible:outline-2 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-64",
112 {
113 defaultVariants: { tone: "default" },
114 variants: {
115 touchTarget: {
116 padded: "pointer-coarse:min-h-11 pointer-coarse:px-1.5",
117 },
118 tone: {
119 default: "text-foreground",
120 muted: "text-muted-foreground hover:text-foreground",
121 destructive: "text-destructive/80 hover:text-destructive",
122 picker:
123 "gap-1 text-foreground decoration-from-font underline-offset-4 data-popup-open:underline",
124 },
125 },
126 },
127);
128
129export function InlineButton({
130 className,
131 tone,
132 touchTarget,
133 render,
134 ...props
135}: useRender.ComponentProps<"button"> & VariantProps<typeof inlineButtonVariants>) {
136 const defaultProps = {
137 className: cn(inlineButtonVariants({ tone, touchTarget }), className),
138 "data-slot": "inline-button",
139 type: render ? undefined : ("button" as const),
140 };
141 return useRender({
142 defaultTagName: "button",
143 props: mergeProps<"button">(defaultProps, props),
144 render,
145 });
146}