# Icon Button

> An icon-only command button with a required accessible name.

```tsx title="hero.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Separator } from '@langgenius/dify-ui/separator'
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@langgenius/dify-ui/tooltip'

const actions = [
  { label: 'Run this step', icon: 'i-ri-play-large-line' },
  { label: 'View logs', icon: 'i-ri-file-list-3-line' },
  { label: 'Duplicate node', icon: 'i-ri-file-copy-line' },
] as const

export function IconButtonHeroDemo() {
  return (
    <TooltipProvider>
      <div className="flex items-center gap-1">
        {actions.map(({ label, icon }) => (
          <Tooltip key={label}>
            <TooltipTrigger
              render={
                <IconButton aria-label={label}>
                  <span aria-hidden="true" className={`${icon} size-4`} />
                </IconButton>
              }
            />
            <TooltipContent>{label}</TooltipContent>
          </Tooltip>
        ))}
        <Separator orientation="vertical" className="mx-1 h-4" />
        <IconButton aria-label="Close">
          <span aria-hidden="true" className="i-ri-close-line size-4" />
        </IconButton>
      </div>
    </TooltipProvider>
  )
}
```

## Usage guidelines

- Use `IconButton` for a command shown as one icon with no visible text. An action with a visible label, with or without an icon, uses [`Button`](/components/button.md); navigation stays on a native link.
- Give every icon button exactly one accessible-name source: `aria-label`, or `aria-labelledby` when suitable visible text exists elsewhere. The types reject a button with neither or both.
- Pair most icon buttons with a [`Tooltip`](/components/tooltip.md) that shows the same words as the name. Compose it through the `render` prop of `TooltipTrigger`. A close (X) button and a "more" menu button go without one, because their icons are universally understood.
- A tooltip is a visual hint, never the accessible name. Keep `aria-label` or `aria-labelledby` on the button even when a tooltip shows the same text.
- Pass exactly one React element as the child: the glyph, with `aria-hidden="true"`. The child owns the glyph and its optical size; `IconButton` owns the button size, radius, colors, hover, disabled and focus-visible styles.
- Omit `variant` for the neutral icon-button appearance. The other variants match `Button`. Use `tone="destructive"` for destructive intent; the accent variants do not take it.
- Use `className` for external layout or for selectors driven by a composed primitive, such as `data-pressed` or `data-popup-open`. Do not recreate an existing variant with classes.
- When `Toggle`, `Menu`, `Popover`, `Tooltip` or `Collapsible` owns the interaction state, keep that primitive outside and compose `IconButton` through its `render` prop. The owner keeps its pressed, open and expanded state, events and ref; do not mirror that state on the icon button.
- Do not render navigation through `IconButton`, whose button behavior is for commands. Put `iconButtonVariants` on a native anchor or router link, which owns its own name and navigation.

## Anatomy

```tsx
import { IconButton, iconButtonVariants } from '@langgenius/dify-ui/icon-button'
```

```tsx
<IconButton aria-label="Close">
  <span aria-hidden="true" className="i-ri-close-line size-4" />
</IconButton>
```

## Examples

### Variants and tones

The default variant is neutral and suits dense toolbars. `primary`, `secondary`, `secondary-accent`, `tertiary`, `ghost` and `ghost-accent` match `Button`. Every variant except the two accent ones also takes `tone="destructive"`. A disabled button keeps its variant and dims.

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

const defaultVariants = [
  { variant: 'default', label: 'Edit (default)', icon: 'i-ri-edit-line' },
  { variant: 'primary', label: 'Create (primary)', icon: 'i-ri-add-line' },
  { variant: 'secondary', label: 'Refresh (secondary)', icon: 'i-ri-refresh-line' },
  { variant: 'tertiary', label: 'Filter (tertiary)', icon: 'i-ri-filter-3-line' },
  { variant: 'ghost', label: 'Expand (ghost)', icon: 'i-ri-expand-diagonal-line' },
] as const

const accentVariants = [
  { variant: 'secondary-accent', label: 'Share (secondary-accent)', icon: 'i-ri-share-line' },
  {
    variant: 'ghost-accent',
    label: 'Generate with AI (ghost-accent)',
    icon: 'i-ri-sparkling-line',
  },
] as const

const destructiveVariants = [
  { variant: 'default', label: 'Delete (default destructive)' },
  { variant: 'primary', label: 'Delete (primary destructive)' },
  { variant: 'secondary', label: 'Delete (secondary destructive)' },
  { variant: 'tertiary', label: 'Delete (tertiary destructive)' },
  { variant: 'ghost', label: 'Delete (ghost destructive)' },
] as const

const disabledVariants = [
  { variant: 'default', label: 'Disabled (default)' },
  { variant: 'primary', label: 'Disabled (primary)' },
  { variant: 'secondary', label: 'Disabled (secondary)' },
  { variant: 'tertiary', label: 'Disabled (tertiary)' },
] as const

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

  return (
    <div className="flex w-full max-w-md flex-col gap-5">
      <div role="group" aria-labelledby={defaultId} className="flex flex-col gap-2.5">
        <span id={defaultId} className="system-xs-medium text-text-tertiary">
          Default
        </span>
        <div className="flex flex-wrap items-start gap-x-5 gap-y-4">
          {defaultVariants.map(({ variant, label, icon }) => (
            <div key={variant} className="flex flex-col items-center gap-1.5">
              <IconButton aria-label={label} size="lg" variant={variant}>
                <span aria-hidden="true" className={`${icon} size-4`} />
              </IconButton>
              <span className="font-mono system-xs-regular text-text-tertiary">{variant}</span>
            </div>
          ))}
        </div>
      </div>

      <div role="group" aria-labelledby={accentId} className="flex flex-col gap-2.5">
        <span id={accentId} className="system-xs-medium text-text-tertiary">
          Accent
        </span>
        <div className="flex flex-wrap items-start gap-x-5 gap-y-4">
          {accentVariants.map(({ variant, label, icon }) => (
            <div key={variant} className="flex flex-col items-center gap-1.5">
              <IconButton aria-label={label} size="lg" variant={variant}>
                <span aria-hidden="true" className={`${icon} size-4`} />
              </IconButton>
              <span className="font-mono system-xs-regular text-text-tertiary">{variant}</span>
            </div>
          ))}
        </div>
      </div>

      <div role="group" aria-labelledby={destructiveId} className="flex flex-col gap-2.5">
        <span id={destructiveId} className="system-xs-medium text-text-tertiary">
          Destructive
        </span>
        <div className="flex flex-wrap items-start gap-x-5 gap-y-4">
          {destructiveVariants.map(({ variant, label }) => (
            <div key={variant} className="flex flex-col items-center gap-1.5">
              <IconButton aria-label={label} size="lg" variant={variant} tone="destructive">
                <span aria-hidden="true" className="i-ri-delete-bin-line size-4" />
              </IconButton>
              <span className="font-mono system-xs-regular text-text-tertiary">{variant}</span>
            </div>
          ))}
        </div>
      </div>

      <div role="group" aria-labelledby={disabledId} className="flex flex-col gap-2.5">
        <span id={disabledId} className="system-xs-medium text-text-tertiary">
          Disabled
        </span>
        <div className="flex flex-wrap items-start gap-x-5 gap-y-4">
          {disabledVariants.map(({ variant, label }) => (
            <div key={variant} className="flex flex-col items-center gap-1.5">
              <IconButton aria-label={label} size="lg" variant={variant} disabled>
                <span aria-hidden="true" className="i-ri-add-line size-4" />
              </IconButton>
              <span className="font-mono system-xs-regular text-text-tertiary">{variant}</span>
            </div>
          ))}
        </div>
      </div>
    </div>
  )
}
```

### Sizes

`xs` through `xl` set the button box and radius. Size the glyph in its own class; the larger sizes fit a `size-4` or `size-5` icon.

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

const sizes = [
  { size: 'xs', icon: 'size-3' },
  { size: 'sm', icon: 'size-3.5' },
  { size: 'md', icon: 'size-4' },
  { size: 'lg', icon: 'size-4' },
  { size: 'xl', icon: 'size-5' },
] as const

export function IconButtonSizesDemo() {
  return (
    <div className="flex flex-wrap items-end justify-center gap-6">
      {sizes.map(({ size, icon }) => (
        <div key={size} className="flex flex-col items-center gap-2">
          <IconButton aria-label={`Add variable (${size})`} size={size} variant="secondary">
            <span aria-hidden="true" className={`i-ri-add-line ${icon}`} />
          </IconButton>
          <span className="font-mono system-xs-regular text-text-tertiary">{size}</span>
        </div>
      ))}
    </div>
  )
}
```

### Composing with a stateful primitive

`Toggle` owns the pressed state of the pin button and `Menu` owns the open state of the "more" button; each renders `IconButton` through `render`. The pin button keeps a constant name and reports its state through `aria-pressed`. The "more" button has no tooltip.

```tsx title="composition.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Menu, MenuContent, MenuItem, MenuSeparator, MenuTrigger } from '@langgenius/dify-ui/menu'
import { Toggle } from '@langgenius/dify-ui/toggle'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'

export function IconButtonCompositionDemo() {
  return (
    <div className="flex items-center gap-1.5">
      <Tooltip>
        <TooltipTrigger
          render={
            <Toggle
              className="group data-pressed:bg-state-accent-active data-pressed:text-text-accent data-pressed:hover:bg-state-accent-active-alt"
              render={
                <IconButton aria-label="Pin app" size="lg">
                  <span
                    aria-hidden="true"
                    className="group-not-data-pressed:i-ri-pushpin-line group-not-data-pressed:size-4 group-data-pressed:i-ri-pushpin-fill group-data-pressed:size-4"
                  />
                </IconButton>
              }
            />
          }
        />
        <TooltipContent>Pin app</TooltipContent>
      </Tooltip>
      <Menu>
        <MenuTrigger
          render={
            <IconButton
              aria-label="More actions"
              size="lg"
              className="data-popup-open:bg-state-base-hover data-popup-open:text-text-secondary"
            >
              <span aria-hidden="true" className="i-ri-more-fill size-4" />
            </IconButton>
          }
        />
        <MenuContent align="end">
          <MenuItem>Duplicate</MenuItem>
          <MenuItem>Export DSL</MenuItem>
          <MenuSeparator />
          <MenuItem variant="destructive">Delete</MenuItem>
        </MenuContent>
      </Menu>
    </div>
  )
}
```

### Icon link

A link that looks like an icon button stays a native anchor with the classes from `iconButtonVariants`. The anchor carries its own `aria-label`.

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

export function IconButtonLinkDemo() {
  return (
    <Tooltip>
      <TooltipTrigger
        render={
          <a
            href="https://docs.dify.ai"
            target="_blank"
            rel="noreferrer"
            aria-label="Dify documentation"
            className={iconButtonVariants({ size: 'lg', variant: 'secondary' })}
          >
            <span aria-hidden="true" className="i-ri-book-open-line size-4" />
          </a>
        }
      />
      <TooltipContent>Dify documentation</TooltipContent>
    </Tooltip>
  )
}
```

## Keyboard

| Key              | Action                                                                                                         |
| ---------------- | -------------------------------------------------------------------------------------------------------------- |
| `Enter`, `Space` | Activates the button.                                                                                          |
| `Tab`            | Moves focus to the next control. A disabled button leaves the tab order unless `focusableWhenDisabled` is set. |

## API reference

### IconButton

An icon-only command button that requires a single icon element and an accessible name. Renders a `<button>` element.

Requires `aria-label` or `aria-labelledby`.

**Props**

| Prop               | Type                                                                                                     | Default     | Description                                                                                          |
| ------------------ | -------------------------------------------------------------------------------------------------------- | ----------- | ---------------------------------------------------------------------------------------------------- |
| `aria-label?`      | `string`                                                                                                 | —           | Names the element for assistive technology. Pass either this or `aria-labelledby`.                   |
| `aria-labelledby?` | `string`                                                                                                 | —           | Identifies the element that names this one. Pass either this or `aria-label`.                        |
| `size?`            | `'lg' \| 'md' \| 'sm' \| 'xl' \| 'xs'`                                                                   | `'md'`      | The square size of the button.                                                                       |
| `variant?`         | `'default' \| 'ghost' \| 'ghost-accent' \| 'primary' \| 'secondary' \| 'secondary-accent' \| 'tertiary'` | `'default'` | 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.                                                  |
| `children`         | `React.ReactElement<unknown, string \| React.JSXElementConstructor<any>>`                                | —           | A single decorative icon element. Hide it from assistive technology with `aria-hidden`.              |

Valid prop combinations:

- `aria-label: string`, `aria-labelledby?: undefined`, `variant?: 'default' | 'ghost' | 'primary' | 'secondary' | 'tertiary'`, `tone?: 'default' | 'destructive'`
- `aria-label: string`, `aria-labelledby?: undefined`, `variant: 'ghost-accent' | 'secondary-accent'`, `tone?: 'default'`
- `aria-label?: undefined`, `aria-labelledby: string`, `variant?: 'default' | 'ghost' | 'primary' | 'secondary' | 'tertiary'`, `tone?: 'default' | 'destructive'`
- `aria-label?: undefined`, `aria-labelledby: string`, `variant: 'ghost-accent' | 'secondary-accent'`, `tone?: 'default'`

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

**Data attributes**

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

### iconButtonVariants

```ts
iconButtonVariants(
  props?: (ConfigVariants<{ variant: { default: string[]; primary: string[]; secondary: string[]; 'secondary-accent': string[]; tertiary: string[]; ghost: string[]; 'ghost-accent': string[]; }; tone: { default: string; destructive: string; }; size: { xs: string; sm: string; md: string; lg: string; xl: string; }; }> & ClassProp),
): string
```

**Parameters**

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

**Returns:** `string`
