# Button
Starts an action, for people and agents.
Source page: https://dinachi.dev/docs/components/button

## Installation
```bash
npx @dinachi/cli@latest add button
```
Adds to your project:
- `components/ui/button/button.tsx`
- `components/ui/button/index.ts`
Installs:
- `@base-ui/react` ^1.9.0
- `class-variance-authority` ^0.7.1

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

export function SaveButton() {
  return <Button onClick={() => save()}>Save changes</Button>
}
```

## Examples

### Variants
Each visual style, from default to destructive.
```tsx
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <div className="flex gap-2 flex-wrap">
      <Button variant="default">Default</Button>
      <Button variant="secondary">Secondary</Button>
      <Button variant="outline">Outline</Button>
      <Button variant="ghost">Ghost</Button>
      <Button variant="link">Link</Button>
      <Button variant="destructive">Destructive</Button>
    </div>
  );
}
```

### Default
A button with the default style.
```tsx
import { Button } from '@/components/ui/button';

export function Example() {
  return <Button>Click me</Button>;
}
```

### Sizes
Small, default and large.
```tsx
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <div className="flex gap-2 items-center">
      <Button size="sm">Small</Button>
      <Button size="default">Default</Button>
      <Button size="lg">Large</Button>
    </div>
  );
}
```

### Icon
Square buttons that hold a single icon.
```tsx
import { Button } from '@/components/ui/button';
import { Bold, Italic, Plus, Search, Trash2, Underline } from 'lucide-react';

export function Example() {
  return (
    <div className="flex gap-2 items-center flex-wrap">
      <Button variant="outline" size="icon">
        <Plus className="h-4 w-4" />
      </Button>
      <Button variant="outline" size="icon">
        <Search className="h-4 w-4" />
      </Button>
      <Button variant="ghost" size="icon">
        <Bold className="h-4 w-4" />
      </Button>
      <Button variant="ghost" size="icon">
        <Italic className="h-4 w-4" />
      </Button>
      <Button variant="ghost" size="icon">
        <Underline className="h-4 w-4" />
      </Button>
      <Button variant="destructive" size="icon">
        <Trash2 className="h-4 w-4" />
      </Button>
    </div>
  );
}
```

### With tooltip
Icon buttons with tooltips, through the render prop.
```tsx
import { Button } from '@/components/ui/button';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { Bold, Mail, Search, Trash2 } from 'lucide-react';

export function Example() {
  return (
    <div className="flex gap-2 items-center flex-wrap">
      <Tooltip>
        <TooltipTrigger render={<Button variant="outline" size="icon" />}>
          <Mail className="h-4 w-4" />
        </TooltipTrigger>
        <TooltipContent>
          <p>Send email</p>
        </TooltipContent>
      </Tooltip>
      <Tooltip>
        <TooltipTrigger render={<Button variant="outline" size="icon" />}>
          <Search className="h-4 w-4" />
        </TooltipTrigger>
        <TooltipContent>
          <p>Search</p>
        </TooltipContent>
      </Tooltip>
      <Tooltip>
        <TooltipTrigger render={<Button variant="ghost" size="icon" />}>
          <Bold className="h-4 w-4" />
        </TooltipTrigger>
        <TooltipContent>
          <p>Bold</p>
        </TooltipContent>
      </Tooltip>
      <Tooltip>
        <TooltipTrigger render={<Button variant="destructive" size="icon" />}>
          <Trash2 className="h-4 w-4" />
        </TooltipTrigger>
        <TooltipContent>
          <p>Delete</p>
        </TooltipContent>
      </Tooltip>
    </div>
  );
}
```

## Notes

### Submitting a form
Unlike a native `<button>`, Button does not submit by default. Pass `type="submit"`.

### Rendering another element
- With `render`, `nativeButton` defaults to false and the element gets `role="button"` and keyboard handling.
- Pass `nativeButton` when `render` is itself a `<button>`, such as another component's trigger.
- For a link, style an `<a>` with `buttonVariants()`. Rendered through Button, it is announced as a button.

### Loading states
Pair `disabled` with `focusableWhenDisabled` to keep focus on the button. If its text changes, point `aria-labelledby` at the text so the change is announced.

### Agents
- Not registered with the agent bridge. Inside an [`AgentProvider`](/docs/components/agent) it is read from the page as a `button`, named by its text or `aria-label`, and takes `press`.
- A disabled button refuses.
- Set `data-agent-confirm` to a reason to require approval for each press.

## API reference

### Button
Base UI's button with the Dinachi variants.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"default" \| "destructive" \| "outline" \| "secondary" \| "ghost" \| "link"` | `"default"` | The visual style. |
| `size` | `"default" \| "sm" \| "lg" \| "icon"` | `"default"` | Height and padding. `icon` is square, for a single icon. |
| `render` | `ReactElement \| (props, state) => ReactElement` | - | Swap the element, keep the behaviour. |
| `nativeButton` | `boolean` | `true, or false with render` | Whether the rendered element is a real `<button>`. Set true when `render` is a button. |
| `disabled` **agent** | `boolean` | `false` | Blocks clicks and agent presses. |
| `focusableWhenDisabled` | `boolean` | `false` | Keeps a disabled button in the tab order, for loading states. |
| `type` | `"button" \| "submit" \| "reset"` | `"button"` | Pass `"submit"` to submit the form. |
| `className` | `string` | - | Merged over the variant classes. |

## Accessibility
- A native button, or role="button" when `nativeButton` is false.
- Name an icon-only button with aria-label; a tooltip does not name it. The agent reads the same name.
- A disabled button leaves the tab order unless `focusableWhenDisabled` is set.
- Reduced motion turns off the press animation.
### Keyboard
| Keys | Does |
| --- | --- |
| `Enter` / `Space` | Activates the button |

## Source
`components/ui/button/button.tsx`
```tsx
import * as React from "react"
import { Button as BaseButton } from "@base-ui/react/button"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"

const buttonVariants = cva(
  [
    "inline-flex items-center cursor-pointer justify-center whitespace-nowrap rounded-md text-sm font-medium",
    "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
    // `transform` and `scale` are both named because Tailwind 3 compiles scale-* into
    // `transform` while Tailwind 4 compiles it into `scale`, and a copied component has
    // no say in which major its project runs.
    "transition-[color,background-color,border-color,transform,scale] duration-[var(--motion-duration-fast,150ms)]",
    // The button gives way under the press and comes back when released. 0.97 is small
    // enough to read as the surface yielding rather than the button resizing.
    "active:scale-[0.97]",
    // Faster down than up: the press is the system answering the user, the release is
    // the button settling. Equal timings in both directions read as mushy.
    "active:duration-100",
    // The colour change on hover and press already carries the feedback, so dropping
    // the movement costs nothing.
    "motion-reduce:active:scale-100",
  ],
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground hover:bg-primary/90",
        destructive:
          "bg-destructive text-destructive-foreground hover:bg-destructive/90",
        outline:
          "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
        secondary:
          "bg-secondary text-secondary-foreground hover:bg-secondary/80",
        ghost: "hover:bg-accent hover:text-accent-foreground",
        // A run of text is not a surface, so there is nothing to press into.
        link: "text-primary underline-offset-4 hover:underline active:scale-100",
      },
      size: {
        default: "h-10 px-4 py-2",
        sm: "h-9 rounded-md px-3",
        lg: "h-11 rounded-md px-8",
        icon: "h-10 w-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

export interface ButtonProps
  extends Omit<React.ComponentPropsWithoutRef<typeof BaseButton>, 'className'>,
    VariantProps<typeof buttonVariants> {
  className?: string
}

const Button = React.forwardRef<HTMLElement, ButtonProps>(
  ({ className, variant, size, render, nativeButton, ...props }, ref) => {
    return (
      <BaseButton
        data-slot="button"
        className={cn(buttonVariants({ variant, size, className }))}
        ref={ref}
        render={render}
        nativeButton={render ? (nativeButton ?? false) : nativeButton}
        {...props}
      />
    )
  }
)
Button.displayName = "Button"

export { Button, buttonVariants }
```
