# Button

> A button that triggers an action, with Dify variants, sizes and a pending state.

```tsx title="hero.tsx"
import { Button } from '@langgenius/dify-ui/button'

export function ButtonHeroDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button>Save draft</Button>
      <Button variant="primary">
        <span aria-hidden="true" className="i-ri-rocket-line size-4" />
        Publish
      </Button>
    </div>
  )
}
```

## Usage guidelines

- Use `Button` for an action with a visible text label. An icon-only command uses [`IconButton`](/components/icon-button.md), a persistent pressed state uses `Toggle`, and navigation uses a native link.
- Keep one `primary` button per surface for the action the user most likely wants. Supporting actions use `secondary`; low-emphasis actions in toolbars and dense rows use `ghost` or `tertiary`.
- Mark an action that deletes or discards data with `tone="destructive"`. The accent variants exist for highlighted secondary actions and do not take a destructive tone.
- `Button` renders `type="button"`. Set `type="submit"` when it submits a form.
- Do not render a link through `Button`: Base UI applies button semantics, keyboard interaction and disabled behavior to the rendered element. Keep navigation on a native anchor or routing link and reuse the look with `buttonVariants`.
- Use `render` with `nativeButton={false}` only when a non-button element intentionally needs button semantics. It is not a link mode.
- Use `loading` for an action that was triggered and is now pending, and `disabled` for an action that is unavailable. They are separate facts and can both apply: pass the pending state only to `loading` and keep independent availability conditions in `disabled`.
- Pass `focusableWhenDisabled={false}` only when a loading button should opt into native disabled behavior and may leave the tab order. Pass `focusableWhenDisabled` on a `disabled` button only when focus must still land on it: the return target of a closing surface, or a control whose tooltip explains why it is unavailable.
- Keep a non-empty visible label throughout loading; the spinner is decorative. Do not replace a changing label with `aria-label`. See [Accessible names](/guides/accessible-names.md) for choosing a naming source.
- `Button` does not add `aria-busy`, which describes an element whose own updates assistive technology may defer, not a pending action. Long-running announcements and progress stay with the feature's status, live-region or progress owner.

## Anatomy

```tsx
import { Button } from '@langgenius/dify-ui/button'
```

```tsx
<Button>
  <span aria-hidden="true" className="i-ri-add-line size-4" />
  Label
</Button>
```

`Button` owns the spacing between its children, so call sites do not add icon margins or a `gap-*` class. Override the gap through `className` only for a documented layout exception.

## Examples

### Variants and tones

Six variants set the emphasis. `primary`, `secondary`, `tertiary` and `ghost` also take `tone="destructive"`.

```tsx title="variants.tsx"
import { Button } from '@langgenius/dify-ui/button'
import * as React from 'react'

export function ButtonVariantsDemo() {
  const defaultId = React.useId()
  const accentId = React.useId()
  const destructiveId = React.useId()

  return (
    <div className="flex flex-col gap-4">
      <div role="group" aria-labelledby={defaultId} className="flex flex-col gap-2">
        <span id={defaultId} className="system-xs-medium text-text-tertiary">
          Default
        </span>
        <div className="flex flex-wrap items-center gap-3">
          <Button variant="primary">Primary</Button>
          <Button variant="secondary">Secondary</Button>
          <Button variant="tertiary">Tertiary</Button>
          <Button variant="ghost">Ghost</Button>
        </div>
      </div>

      <div role="group" aria-labelledby={accentId} className="flex flex-col gap-2">
        <span id={accentId} className="system-xs-medium text-text-tertiary">
          Accent
        </span>
        <div className="flex flex-wrap items-center gap-3">
          <Button variant="secondary-accent">Secondary accent</Button>
          <Button variant="ghost-accent">Ghost accent</Button>
        </div>
      </div>

      <div role="group" aria-labelledby={destructiveId} className="flex flex-col gap-2">
        <span id={destructiveId} className="system-xs-medium text-text-tertiary">
          Destructive
        </span>
        <div className="flex flex-wrap items-center gap-3">
          <Button variant="primary" tone="destructive">
            Primary
          </Button>
          <Button variant="secondary" tone="destructive">
            Secondary
          </Button>
          <Button variant="tertiary" tone="destructive">
            Tertiary
          </Button>
          <Button variant="ghost" tone="destructive">
            Ghost
          </Button>
        </div>
      </div>
    </div>
  )
}
```

### Sizes

`small`, `medium` (the default) and `large` set the height, padding and gap together. `medium` and `large` use 4px and 6px gaps; `small` uses 3px for `primary` and 4px for the other variants.

```tsx title="sizes.tsx"
import { Button } from '@langgenius/dify-ui/button'

export function ButtonSizesDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button size="small">
        <span aria-hidden="true" className="i-ri-add-line size-3.5" />
        Small
      </Button>
      <Button size="medium">
        <span aria-hidden="true" className="i-ri-add-line size-4" />
        Medium
      </Button>
      <Button size="large">
        <span aria-hidden="true" className="i-ri-add-line size-4" />
        Large
      </Button>
    </div>
  )
}
```

### Loading

`loading` blocks activation while the button keeps focus, and shows a decorative spinner. When the visible label changes during loading, give the label a stable ID and reference it with `aria-labelledby` so screen readers announce the new name; some browser and screen-reader combinations do not reliably announce changes to a focused button's descendant text. The consumer owns this relationship because only it knows whether the label changes and whether other visible context must also be referenced. A label that stays the same keeps naming the button through its text.

```tsx title="loading.tsx"
import { Button } from '@langgenius/dify-ui/button'
import * as React from 'react'

export function ButtonLoadingDemo() {
  const [saving, setSaving] = React.useState(false)
  const labelId = React.useId()
  const timerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null)

  React.useEffect(() => {
    return () => {
      if (timerRef.current) clearTimeout(timerRef.current)
    }
  }, [])

  return (
    <Button
      variant="primary"
      loading={saving}
      aria-labelledby={labelId}
      onClick={() => {
        setSaving(true)
        timerRef.current = setTimeout(() => setSaving(false), 2000)
      }}
    >
      <span id={labelId}>{saving ? 'Saving…' : 'Save'}</span>
    </Button>
  )
}
```

### Disabled

`disabled` removes the button from the tab order. Add `focusableWhenDisabled` when focus must still reach it, such as a disabled action whose [`Tooltip`](/components/tooltip.md) explains why it is unavailable.

```tsx title="disabled.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'

export function ButtonDisabledDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button disabled>Save draft</Button>
      <Tooltip>
        <TooltipTrigger render={<Button variant="primary" disabled focusableWhenDisabled />}>
          Publish
        </TooltipTrigger>
        <TooltipContent>Add at least one node before publishing</TooltipContent>
      </Tooltip>
    </div>
  )
}
```

### Link

Keep navigation on a native anchor or router link and apply `buttonVariants` to reuse the button appearance without turning the link into a button.

```tsx title="link.tsx"
import { buttonVariants } from '@langgenius/dify-ui/button'

export function ButtonLinkDemo() {
  return (
    <a
      href="https://docs.dify.ai"
      target="_blank"
      rel="noreferrer"
      className={buttonVariants({ variant: 'secondary' })}
    >
      Read docs
      <span aria-hidden="true" className="i-ri-external-link-line size-4" />
    </a>
  )
}
```

## Keyboard

| Key     | Action                                                                                                                                       |
| ------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `Enter` | Activates the button.                                                                                                                        |
| `Space` | Activates the button.                                                                                                                        |
| `Tab`   | Moves focus to the next control. A loading button stays in the tab order; a disabled button leaves it unless `focusableWhenDisabled` is set. |

## API reference

### Button

A styled button that triggers an action. Renders a `<button>` element.

Setting `loading` disables activation and keeps the button focusable by default.

**Props**

| Prop       | Type                                                                                        | Default       | Description                                                                                                                                                                                                                                                            |
| ---------- | ------------------------------------------------------------------------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `size?`    | `'large' \| 'medium' \| 'small'`                                                            | `'medium'`    | The height, padding, and type scale of the button.                                                                                                                                                                                                                     |
| `variant?` | `'ghost' \| 'ghost-accent' \| 'primary' \| 'secondary' \| 'secondary-accent' \| 'tertiary'` | `'secondary'` | The visual emphasis of the button. The accent variants cannot be combined with `tone="destructive"`.                                                                                                                                                                   |
| `tone?`    | `'default' \| 'destructive'`                                                                | `'default'`   | Whether the button represents a destructive action.                                                                                                                                                                                                                    |
| `loading?` | `boolean`                                                                                   | —             | Marks an action as pending, blocks activation, and keeps the button focusable by default. Keep a non-empty visible label. If that label changes while loading, give it a stable ID and reference it with `aria-labelledby` so it remains the explicit accessible name. |

Valid prop combinations:

- `variant?: 'ghost' | 'primary' | 'secondary' | 'tertiary'`, `tone?: 'default' | 'destructive'`
- `variant: 'ghost-accent' | 'secondary-accent'`, `tone?: 'default'`

Inherits `focusableWhenDisabled`, `className`, `render`, `style`, `nativeButton` from [Base UI Button](https://base-ui.com/react/components/button).

**Data attributes**

| Attribute       | Description                          |
| --------------- | ------------------------------------ |
| `data-disabled` | Present when the button is disabled. |

### buttonVariants

```ts
buttonVariants(
  props?: (ConfigVariants<{ variant: { primary: string[]; secondary: string[]; 'secondary-accent': string[]; tertiary: string[]; ghost: string[]; 'ghost-accent': string[]; }; size: { small: string; medium: string; large: string; }; tone: { default: string; destructive: string; }; }> & ClassProp),
): string
```

**Parameters**

| Parameter | Type                                                                                                                                                                                                                                                                                             | Description |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- |
| `props?`  | `(ConfigVariants<{ variant: { primary: string[]; secondary: string[]; 'secondary-accent': string[]; tertiary: string[]; ghost: string[]; 'ghost-accent': string[]; }; size: { small: string; medium: string; large: string; }; tone: { default: string; destructive: string; }; }> & ClassProp)` | —           |

**Returns:** `string`
