Button
Displays a button or a component that looks like a button.
Preview
import { Button } from "@/components/ui/Button";
import { MixerHorizontalIcon } from "@radix-ui/react-icons";
export function ButtonDemo() {
return (
<div className="flex flex-col gap-4 items-start">
<Button variant="default">Default</Button>
<Button variant="fill">Fill</Button>
<Button variant="pill">Pill</Button>
<Button variant="link">Link</Button>
<Button variant="menu">Menu</Button>
</div>
)
}Installation
Make sure that namespace is set in your component.json file. Namespace docs: Learn more about namespaces
pnpm dlx shadcn@latest add @aura/buttonManual
Install the following dependencies:
pnpm install @radix-ui/react-icons @radix-ui/react-slot class-variance-authorityCopy and paste the class names utility into your utils/class-names.ts file.
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Copy and paste the spinner component into your components/ui/Spinner.tsx file.
"use client";
/**
* @description A status spinner. Buttons reuse it while a submit is in flight.
*/
import * as React from "react";
import { SymbolIcon } from "@radix-ui/react-icons";
import { cn } from "@/utils/class-names";
function Spinner({
className,
label,
...props
}: React.ComponentProps<"span"> & {
label?: string;
}) {
return (
<span
data-slot="spinner"
role={label ? "status" : undefined}
className={cn("inline-flex", className)}
{...props}
>
<SymbolIcon
className="icon animate-spin motion-reduce:animate-none"
aria-hidden
/>
{label ? <span className="sr-only">{label}</span> : null}
</span>
);
}
export { Spinner };
Copy and paste the Button component into your components/ui/Button.tsx file.
/**
* @description Displays a button or a component that looks like a button.
*
* `mode` is a deprecated alias of `variant`. `variant="default"` is a
* deprecated alias of `fill`. Both still render and warn in development.
* Disabled chrome (opacity and cursor) lives only in `.button:disabled`.
* Loading sets `disabled` so the control stays non-interactive, and
* `data-loading` so it does not fade to the disabled opacity.
*/
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/utils/class-names";
import { Spinner } from "@/components/ui/Spinner";
const buttonVariants = cva(
"button focus-visible:[outline:var(--aura-outline)] [&_svg]:size-[1em]",
{
variants: {
variant: {
default: "button-fill",
fill: "button-fill",
pill: "button-pill border border-gray-6 text-gray-11 bg-gray-2 hover:bg-gray-3",
link: "button-link",
menu: "button-menu",
split: "button-fill button-split-action",
splitMenu: "button-fill button-split-menu",
},
size: {
default: "h-4",
xs: "h-2.5",
sm: "h-3",
md: "h-4",
lg: "h-5",
xl: "h-6",
// `icon` and `icon-sm` are the 39px (3 × 13px) icon button. `icon-md` is 52px.
icon: "w-3 h-3 p-0",
"icon-sm": "w-3 h-3 p-0",
"icon-md": "w-4 h-4 p-0",
},
},
defaultVariants: {
variant: "fill",
size: "default",
},
},
);
interface ButtonProps
extends React.ComponentProps<"button">,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
isDisabled?: boolean;
isLoading?: boolean;
isLoadingText?: string | React.ReactNode;
/** @deprecated Use `variant`. Still applied, and warns in development. */
mode?: VariantProps<typeof buttonVariants>["variant"];
label?: string | React.ReactNode;
}
const deprecationWarnings = new Set<string>();
function warnOnce(message: string) {
if (process.env.NODE_ENV === "production") return;
if (deprecationWarnings.has(message)) return;
deprecationWarnings.add(message);
console.warn(message);
}
function resolveButtonContent(
label: React.ReactNode,
children: React.ReactNode,
): React.ReactNode {
const hasLabel = label != null && label !== false;
const hasChildren = children != null && children !== false;
if (hasLabel && hasChildren) {
warnOnce(
"Button: `label` and `children` both set the visible text. Pass one. `children` is shown.",
);
return children;
}
return hasChildren ? children : label;
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
(props: ButtonProps, ref) => {
const {
className,
variant,
mode,
size,
asChild = false,
isDisabled,
isLoading,
isLoadingText,
children,
label,
disabled: disabledProp,
...rest
} = props;
const Comp = asChild ? Slot : "button";
const explicitlyDisabled = Boolean(isDisabled || disabledProp);
const loading = Boolean(isLoading);
const nonInteractive = explicitlyDisabled || loading;
const effectiveVariant = variant ?? mode;
if (mode !== undefined) {
warnOnce(
"Button: `mode` is deprecated and duplicates `variant`. Pass `variant` instead. `mode` still works.",
);
}
if (variant === "default" || mode === "default") {
warnOnce(
'Button: variant "default" is deprecated and matches "fill". Pass `variant="fill"`. "default" still works.',
);
}
const content = resolveButtonContent(label, children);
return (
<Comp
data-slot="button"
className={cn(
buttonVariants({ variant: effectiveVariant, size, className }),
)}
ref={ref}
{...rest}
data-loading={loading && !explicitlyDisabled ? true : undefined}
aria-busy={loading ? true : undefined}
disabled={nonInteractive}
>
{asChild ? (
children
) : (
<>
{isLoading && isLoadingText ? (
isLoadingText
) : (
<>
{isLoading ? <Spinner /> : null}
{content}
</>
)}
</>
)}
</Comp>
);
},
);
Button.displayName = "Button";
export { Button, buttonVariants };
export type { ButtonProps };
export default Button;Deprecated aliases
mode duplicates variant. Pass variant. mode still applies and prints a development warning.
variant="default" matches fill. Pass variant="fill". default still renders the fill button and prints a development warning.
Disabled opacity and the default cursor come from .button:disabled only. The component no longer adds opacity-50 or cursor-not-allowed.
Loading sets disabled so the button cannot be activated again, and data-loading so it does not use the disabled 50% opacity. The spinner stays at full contrast.
Usage
Fill
export const Fill = () => <Button variant="fill">Fill Button</Button>;Pill
export const Pill = () => <Button variant="pill">Pill Button</Button>;Link
export const Link = () => <Button variant="link">Link Button</Button>;Menu
export const Menu = () => <Button variant="menu">Menu Button</Button>;Sizes
export const Sizes = () => (
<div className="flex flex-col gap-4 items-start">
<Button size="xs">Extra Small</Button>
<Button size="sm">Small</Button>
<Button size="md">Medium</Button>
<Button size="lg">Large</Button>
<Button size="xl">Extra Large</Button>
<Button size="icon" aria-label="Small icon, 39px">
<MixerHorizontalIcon className="icon" />
</Button>
<Button size="icon-sm" aria-label="Small icon, 39px">
<MixerHorizontalIcon className="icon" />
</Button>
<Button size="icon-md" aria-label="Medium icon, 52px">
<MixerHorizontalIcon className="icon" />
</Button>
</div>
)size="icon" and size="icon-sm" are the 39px icon button (w-3, 3 × 13px). size="icon-md" is 52px. Icon-only buttons need an aria-label.
Disabled
export const Disabled = () => <Button isDisabled>Disabled Button</Button>;Loading
export const Loading = () => <Button isLoading>Loading Button...</Button>;States
export const States = () => (
<div className="flex flex-col gap-4 items-start">
<Button>Normal</Button>
<Button isDisabled>Disabled</Button>
<Button isLoading>Loading</Button>
</div>
)ModeProp
export const ModeProp = () => (
<div className="flex flex-col gap-4 items-start">
<Button mode="default">Default (mode)</Button>
<Button mode="fill">Fill (mode)</Button>
<Button mode="pill">Pill (mode)</Button>
<Button mode="link">Link (mode)</Button>
<Button mode="menu">Menu (mode)</Button>
</div>
)