# Button

> A versatile button component with multiple variants, sizes, shapes, morphing press feedback, loading state, and ripple.

Canonical URL: https://taut-ui.com/docs/components/button

```tsx
import { Button } from "@/components/ui/button";

export default function Particle() {
  return <Button>Button</Button>;
}
```

## Installation [#installation]

**CLI**

```bash
npx shadcn@latest add https://taut-ui.com/r/button.json
```

**Manual**

- Install the following dependencies:

```bash
npm install @base-ui/react class-variance-authority
```

- Copy and paste the following code into your project.

```tsx title="components/ui/button.tsx"
"use client";

import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";
import {
  type MorphShape,
  morphClasses,
  morphRadius,
  tautSize,
} from "@/lib/morph";
import { Ripple } from "@/lib/ripple";
import { cn } from "@/lib/utils";
import { Spinner } from "@/components/ui/spinner";

export const buttonVariants = cva(
  [
    "group relative inline-flex shrink-0 cursor-pointer select-none items-center justify-center whitespace-nowrap",
    "border-solid font-medium outline-none",
    "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
    "disabled:pointer-events-none disabled:opacity-38 data-disabled:pointer-events-none data-disabled:opacity-38",
    "[&_svg:not([class*='size-'])]:size-(--icon) [&_svg]:pointer-events-none [&_svg]:shrink-0",
    "pointer-coarse:after:-translate-1/2 pointer-coarse:after:absolute pointer-coarse:after:top-1/2 pointer-coarse:after:left-1/2 pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11",
  ],
  {
    defaultVariants: { variant: "default" },
    variants: {
      variant: {
        default:
          "bg-primary text-primary-foreground data-[selected=false]:bg-muted data-[selected=false]:text-foreground",
        filled:
          "bg-primary text-primary-foreground data-[selected=false]:bg-muted data-[selected=false]:text-foreground",
        secondary:
          "bg-secondary text-secondary-foreground data-[selected=false]:bg-muted",
        tonal:
          "bg-secondary text-secondary-foreground data-[selected=false]:bg-muted",
        elevated: [
          "bg-[color-mix(in_oklab,var(--color-card),var(--color-primary)_5%)] text-primary",
          "shadow-[0_0.0625rem_0.125rem_0_rgb(0_0_0/0.3),0_0.0625rem_0.1875rem_0.0625rem_rgb(0_0_0/0.15)]",
          "hover:shadow-[0_0.0625rem_0.125rem_0_rgb(0_0_0/0.3),0_0.125rem_0.375rem_0.125rem_rgb(0_0_0/0.15)]",
          "active:shadow-[0_0.0625rem_0.125rem_0_rgb(0_0_0/0.3),0_0.0625rem_0.1875rem_0.0625rem_rgb(0_0_0/0.15)]",
          "disabled:shadow-none data-disabled:shadow-none",
          "data-[selected=true]:bg-primary data-[selected=true]:text-primary-foreground",
        ],
        outline:
          "border border-border text-foreground data-[selected=true]:border-transparent data-[size=icon-xl]:border-2 data-[size=xl]:border-2 data-[selected=true]:bg-secondary data-[selected=true]:text-secondary-foreground",
        ghost: "text-foreground",
        text: "text-foreground",
        link: "text-primary underline-offset-4 hover:underline",
        destructive: "bg-destructive text-destructive-foreground",
        "destructive-outline":
          "border border-destructive/40 text-destructive data-[size=icon-xl]:border-2 data-[size=xl]:border-2",
      },
    },
  },
);

export interface ButtonProps extends useRender.ComponentProps<"button"> {
  variant?: VariantProps<typeof buttonVariants>["variant"];
  size?: VariantProps<typeof tautSize>["size"];
  width?: VariantProps<typeof tautSize>["width"];
  shape?: MorphShape;
  morph?: boolean;
  loading?: boolean;
  selected?: boolean;
}

export function Button({
  className,
  variant = "default",
  size = "default",
  width,
  shape = "round",
  morph = false,
  render,
  children,
  loading = false,
  selected,
  disabled: disabledProp,
  ...props
}: ButtonProps): React.ReactElement {
  const isDisabled = Boolean(loading || disabledProp);
  const hasRenderElementChildren =
    React.isValidElement<{ children?: React.ReactNode }>(render) &&
    render.props.children !== undefined;
  const resolvedRender = hasRenderElementChildren
    ? React.cloneElement(render, { children: undefined })
    : render;
  const resolvedChildren =
    children ?? (hasRenderElementChildren ? render.props.children : undefined);

  const defaultProps = {
    children: (
      <>
        <Ripple disabled={isDisabled} />
        <span
          className={cn(
            "relative z-10 inline-flex items-center justify-center [gap:inherit]",
            loading && "invisible",
          )}
        >
          {resolvedChildren}
        </span>
        {loading && (
          <Spinner
            className="pointer-events-none absolute z-20 size-(--icon)"
            data-slot="button-loading-indicator"
          />
        )}
      </>
    ),
    className: cn(
      buttonVariants({ variant }),
      tautSize({ size, width }),
      morphClasses({
        press: morph,
        radius: morphRadius(shape, selected, morph),
      }),
      className,
    ),
    "aria-disabled": isDisabled || undefined,
    "aria-pressed": selected,
    "data-disabled": isDisabled ? "" : undefined,
    "data-loading": loading ? "" : undefined,
    "data-selected":
      selected === undefined ? undefined : selected ? "true" : "false",
    "data-size": size,
    "data-slot": "button",
    "data-variant": variant,
    disabled: resolvedRender ? undefined : isDisabled,
    type: (resolvedRender ? undefined : "button") as "button" | undefined,
  };

  return useRender({
    defaultTagName: "button",
    props: mergeProps<"button">(defaultProps, props),
    render: resolvedRender,
  });
}
```

- Update the import paths to match your project setup.

## Usage [#usage]

```tsx
import { Button } from "@/components/ui/button"
```

```tsx
<Button>Button</Button>
```

## Examples [#examples]

### Secondary [#secondary]

```tsx
import { Button } from "@/components/ui/button";

export default function Particle() {
  return <Button variant="secondary">Secondary</Button>;
}
```

### Elevated [#elevated]

```tsx
import { Button } from "@/components/ui/button";

export default function Particle() {
  return <Button variant="elevated">Elevated</Button>;
}
```

### Outline [#outline]

```tsx
import { Button } from "@/components/ui/button";

export default function Particle() {
  return <Button variant="outline">Outline</Button>;
}
```

### Ghost [#ghost]

```tsx
import { Button } from "@/components/ui/button";

export default function Particle() {
  return <Button variant="ghost">Ghost</Button>;
}
```

### Destructive [#destructive]

```tsx
import { Button } from "@/components/ui/button";

export default function Particle() {
  return <Button variant="destructive">Delete</Button>;
}
```

### With Icon [#with-icon]

```tsx
import { MailIcon } from "lucide-react";
import { Button } from "@/components/ui/button";

export default function Particle() {
  return (
    <Button>
      <MailIcon />
      Send email
    </Button>
  );
}
```

### Icon Only [#icon-only]

```tsx
import { PlusIcon } from "lucide-react";
import { Button } from "@/components/ui/button";

export default function Particle() {
  return (
    <Button aria-label="Add item" size="icon" variant="secondary">
      <PlusIcon />
    </Button>
  );
}
```

### Loading [#loading]

```tsx
import { Button } from "@/components/ui/button";

export default function Particle() {
  return <Button loading>Saving</Button>;
}
```

### Morph & Selected [#morph--selected]

```tsx
"use client";

import { HeartIcon } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";

export default function Particle() {
  const [selected, setSelected] = useState(false);

  return (
    <Button
      morph
      onClick={() => setSelected((prev) => !prev)}
      selected={selected}
      variant="secondary"
    >
      <HeartIcon className={selected ? "fill-current" : undefined} />
      Favorite
    </Button>
  );
}
```

### As Link [#as-link]

```tsx
"use client";

import { ArrowUpRightIcon } from "lucide-react";
import Link from "next/link";
import { Button } from "@/components/ui/button";

export default function Particle() {
  return (
    <Button
      render={
        <Link
          href="#as-link"
          onClick={(e) => e.preventDefault()}
          prefetch={false}
        />
      }
      variant="outline"
    >
      Documentation
      <ArrowUpRightIcon />
    </Button>
  );
}
```

## API Reference [#api-reference]

### Button [#button]

Renders an interactive button element with ripple feedback, optional shape morphing, and polymorphic rendering via Base UI `useRender`.

| Prop        | Type                                                                                                                          | Default     | Description                                                        |
| ----------- | ----------------------------------------------------------------------------------------------------------------------------- | ----------- | ------------------------------------------------------------------ |
| `variant`   | `"default" \| "filled" \| "secondary" \| "tonal" \| "elevated" \| "outline" \| "ghost" \| "text" \| "link" \| "destructive" \| "destructive-outline"` | `"default"` | Visual style variant                                               |
| `size`      | `"xs" \| "sm" \| "default" \| "md" \| "lg" \| "xl" \| "icon-xs" \| "icon-sm" \| "icon" \| "icon-md" \| "icon-lg" \| "icon-xl"` | `"default"` | Size of the button, controlling height, padding, and icon sizing   |
| `width`     | `"narrow" \| "wide"`                                                                                                          | -           | Adjusts the width of icon-only buttons                             |
| `shape`     | `"round" \| "square"`                                                                                                         | `"round"`   | Base corner radius shape                                           |
| `morph`     | `boolean`                                                                                                                     | `false`     | Enables spring corner morphing on press and when `selected`        |
| `selected`  | `boolean`                                                                                                                     | -           | Sets `aria-pressed` and `data-selected` for toggleable buttons     |
| `loading`   | `boolean`                                                                                                                     | `false`     | Shows a centered `Spinner` and disables interaction while `true`   |
| `disabled`  | `boolean`                                                                                                                     | `false`     | Disables the button natively and sets `aria-disabled`              |
| `render`    | `React.ReactElement \| ((props, state) => React.ReactElement)`                                                                | -           | Custom element to render (e.g. `<Link />` or `<a />`)              |
| `className` | `string`                                                                                                                      | -           | Additional CSS classes                                             |
| `...props`  | `React.ComponentProps<"button">`                                                                                              | -           | All standard button attributes are supported                       |

### buttonVariants [#buttonvariants]

A `cva` helper exported for composing button variant styles on custom elements (pair with `tautSize` for sizing).

```tsx
import { cn } from "@/lib/utils"
import { tautSize } from "@/lib/morph"
import { buttonVariants } from "@/components/ui/button"

<a
  className={cn(buttonVariants({ variant: "outline" }), tautSize({ size: "sm" }))}
  href="/docs"
>
  Read the docs
</a>
```
