# IconButton

> A square icon-only button with customizable appearance, size, and shape. Supports loading state and renders as anchor when href is provided.

Source: https://vaneui.com/docs/basic-components/icon-button

Import: `import { IconButton, Col, Row, Text, ThemeProvider } from "@vaneui/ui"`

IconButton is for a toolbar action or a dismiss control that has no room for a text label. It renders a `<button>` with the same `sm` default size as [Button](/docs/basic-components/button), so it lines up with a Button placed beside it. When the action needs visible text, use Button; for a glyph that does nothing on click, use [Icon](/docs/basic-components/icon). Like Button, IconButton sets no `type`, so inside a `<form>` it submits the form unless it has `type="button"` (see [Forms](/docs/form-components/forms)).

## Basic usage

Drop an icon directly inside the `IconButton`. Each `appearance` prop maps to a semantic color from the active theme.

```tsx demo
<Row flexWrap>
  <IconButton aria-label="Favorite"><Star /></IconButton>
  <IconButton secondary aria-label="Like"><Heart /></IconButton>
  <IconButton success aria-label="Confirm"><Check /></IconButton>
  <IconButton danger aria-label="Delete"><Trash2 /></IconButton>
  <IconButton warning aria-label="Notifications"><Bell /></IconButton>
  <IconButton info aria-label="Show info"><Info /></IconButton>
</Row>
```

## Accessibility

An icon-only button has no visible text, so assistive technology has nothing to announce. Give every `IconButton` an accessible name with `aria-label`. A development build logs a console warning for an IconButton that has no `aria-label`, `aria-labelledby` or `title`.

```tsx demo
<Row flexWrap>
  <IconButton aria-label="Add to favorites"><Star /></IconButton>
  <IconButton secondary aria-label="Like"><Heart /></IconButton>
  <IconButton danger aria-label="Delete"><Trash2 /></IconButton>
</Row>
```

## Sizes

IconButton supports five sizes: `xs`, `sm` (default), `md`, `lg`, `xl`. Size drives the button's dimensions, padding, and border-radius; the aspect ratio stays square at every size.

```tsx demo
<Row flexWrap itemsEnd>
  <Col itemsCenter>
    <IconButton xs aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>xs</Text>
  </Col>
  <Col itemsCenter>
    <IconButton sm aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>sm</Text>
  </Col>
  <Col itemsCenter>
    <IconButton md aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>md</Text>
  </Col>
  <Col itemsCenter>
    <IconButton lg aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>lg</Text>
  </Col>
  <Col itemsCenter>
    <IconButton xl aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>xl</Text>
  </Col>
</Row>
```

## Variants

IconButton renders as `outline` (default) or `filled`.

```tsx demo
<Col>
  <Row flexWrap>
    <IconButton aria-label="Favorite"><Star /></IconButton>
    <IconButton secondary aria-label="Like"><Heart /></IconButton>
    <IconButton success aria-label="Confirm"><Check /></IconButton>
    <IconButton danger aria-label="Delete"><Trash2 /></IconButton>
    <IconButton warning aria-label="Notifications"><Bell /></IconButton>
    <IconButton info aria-label="Show info"><Info /></IconButton>
  </Row>
  <Row flexWrap>
    <IconButton filled aria-label="Favorite"><Star /></IconButton>
    <IconButton filled secondary aria-label="Like"><Heart /></IconButton>
    <IconButton filled success aria-label="Confirm"><Check /></IconButton>
    <IconButton filled danger aria-label="Delete"><Trash2 /></IconButton>
    <IconButton filled warning aria-label="Notifications"><Bell /></IconButton>
    <IconButton filled info aria-label="Show info"><Info /></IconButton>
  </Row>
</Col>
```

## Shapes

IconButton supports `rounded` (default), `pill`, and `sharp`. `pill` is especially well suited to icon-only buttons.

```tsx demo
<Row flexWrap>
  <Col itemsCenter>
    <IconButton filled aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>rounded</Text>
  </Col>
  <Col itemsCenter>
    <IconButton pill filled aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>pill</Text>
  </Col>
  <Col itemsCenter>
    <IconButton sharp filled aria-label="Favorite"><Star /></IconButton>
    <Text sm secondary>sharp</Text>
  </Col>
</Row>
```

## As Link and disabled

Pass `href` to render the IconButton as an `<a>` for navigation. Use `disabled` to prevent interaction.

```tsx demo
<Row flexWrap>
  <IconButton
    href="https://github.com"
    target="_blank"
    aria-label="GitHub (opens in a new tab)"
  >
    <ExternalLink />
  </IconButton>
  <IconButton filled success href="#" aria-label="Open link">
    <ExternalLink />
  </IconButton>
  <IconButton disabled aria-label="Favorite"><Star /></IconButton>
  <IconButton filled danger disabled aria-label="Delete"><Trash2 /></IconButton>
</Row>
```

## Next.js Link integration

Use the `tag` prop to render the IconButton as a Next.js `Link` for client-side navigation.

```tsx
import Link from 'next/link';
import { IconButton } from '@vaneui/ui';
import { Settings } from 'react-feather';

<IconButton href="/settings" tag={Link} aria-label="Settings"><Settings /></IconButton>
```

## Loading state

Set `loading` to replace the icon with a spinner and auto-disable the button.

```tsx demo
<Row flexWrap itemsCenter>
  <IconButton loading aria-label="Refreshing"><RefreshCw /></IconButton>
  <IconButton loading filled danger aria-label="Deleting"><Trash2 /></IconButton>
  <IconButton loading lg aria-label="Saving"><Star /></IconButton>
</Row>
```

The ring is the `Spinner` component, sized from the button and coloured by the button's own text colour, so it reads correctly on every variant. Keep the `aria-label` describing the action: the button carries `aria-busy` while it spins, and the Spinner itself is decorative here.

```tsx demo
const [loading, setLoading] = useState(false);

return (
  <IconButton loading={loading} onClick={() => setLoading(true)} aria-label="Favorite">
    <Star />
  </IconButton>
);
```

## Customizing

Set app-wide IconButton defaults with `ThemeProvider`'s `themeDefaults`:

```tsx demo
<ThemeProvider themeDefaults={{
  iconButton: { secondary: true, pill: true, lg: true },
}}>
  <IconButton aria-label="Favorite"><Star /></IconButton>
</ThemeProvider>
```

## Frequently asked questions

### Why don't my Button theme defaults apply to IconButton?

IconButton has its own theme node, `iconButton`, so defaults set on `button.main` never reach it. Set both when the two should match: `themeDefaults={{ button: { main: { pill: true } }, iconButton: { pill: true } }}`. The `iconButton` node is flat, with no `main` level, and the same split applies to `extraClasses`.

### Does a Tooltip give an IconButton its accessible name?

No. A [Tooltip](/docs/overlay-components/tooltip) describes its trigger through `aria-describedby`, and only while it is open, so it adds a description rather than a name. Keep the `aria-label` on the IconButton and add the Tooltip for sighted pointer and keyboard users. The label and the hint can use the same words.

### Why does my icon's `size` prop have no effect inside an IconButton?

IconButton sizes every SVG inside it from its own size prop, with CSS `width` and `height`, and CSS wins over the `width` and `height` attributes an icon component sets from its `size` prop. Change the button's size instead (`lg`, `xl`), or give the SVG a `size-*` class, which opts it out of that rule. `Button` and `Icon` size their SVGs the same way.

### Can an IconButton use the `ghost` variant?

Yes. IconButton takes the same three variants as Button: `outline` (the default), `filled` and `ghost`. `ghost` renders a transparent background with no border and an appearance-colored icon, and tints the background on hover. It suits a close control or a row of secondary toolbar actions; the [removable chips](/docs/basic-components/chip#removable-chips) example uses `<IconButton xs ghost>`.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance | yes | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance |  | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance |  | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `disabled` | Disabled |  | Disable the component — reduces opacity, changes cursor to not-allowed, and prevents interaction |
| `flex1` | Flex |  | Take up remaining space (= `flex-1`, i.e. `flex: 1 1 0%`) |
| `flexAuto` | Flex |  | Grow but respect intrinsic size (= `flex-auto`, i.e. `flex: 1 1 auto`) |
| `flexNone` | Flex |  | Don't grow and don't shrink (= `flex-none`, i.e. `flex: none`) |
| `fontSans` | Font Family |  | Sans-serif font family (default) — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family |  | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `italic` | Font Style |  | Italic font style |
| `notItalic` | Font Style |  | Not italic (normal) font style |
| `fontThin` | Font Weight |  | Thin font weight (100) — emits font-thin |
| `fontExtralight` | Font Weight |  | Extra light font weight (200) — emits font-extralight |
| `fontLight` | Font Weight |  | Light font weight (300) — emits font-light |
| `fontNormal` | Font Weight |  | Normal font weight (400) — emits font-normal |
| `fontMedium` | Font Weight |  | Medium font weight (500) — emits font-medium |
| `fontSemibold` | Font Weight |  | Semibold font weight (600) — emits font-semibold |
| `fontBold` | Font Weight |  | Bold font weight (700) — emits font-bold |
| `fontExtrabold` | Font Weight |  | Extra bold font weight (800) — emits font-extrabold |
| `fontBlack` | Font Weight |  | Black font weight (900) — emits font-black |
| `inheritSize` | Inherit Size |  | Inherit font-size and line-height from the nearest parent typography element |
| `noInheritSize` | Inherit Size |  | Keep own font-size; do not inherit from parent |
| `padding` | Padding | yes | Enable internal padding |
| `paddingX` | Padding |  | Enable only horizontal padding |
| `paddingY` | Padding |  | Enable only vertical padding |
| `noPadding` | Padding |  | Disable internal padding |
| `pill` | Shape |  | Fully rounded corners (circular) |
| `sharp` | Shape |  | No rounded corners (square) |
| `rounded` | Shape | yes | Medium rounded corners (default) |
| `noShrink` | Shrink |  | Prevent the flex item from shrinking below its content size (= `shrink-0`) |
| `xs` | Size |  | Extra small size |
| `sm` | Size | yes | Small size |
| `md` | Size |  | Medium size (default) |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |
| `textLeft` | Text Align |  | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `underline` | Text Decoration |  | Add underline decoration below text |
| `lineThrough` | Text Decoration |  | Add strikethrough/line-through decoration across text |
| `noUnderline` | Text Decoration |  | Remove ALL text-decoration — underline, line-through, and overline (text-decoration: none) |
| `overline` | Text Decoration |  | Add overline decoration above text |
| `uppercase` | Text Transform |  | Transform text to uppercase |
| `lowercase` | Text Transform |  | Transform text to lowercase |
| `capitalize` | Text Transform |  | Capitalize first letter of each word |
| `normalCase` | Text Transform |  | Normal text case (no transformation) |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text (default) |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |
| `breakNormal` | Word Break |  | Reset breaking - overflow-wrap and word-break normal |
| `breakWords` | Word Break |  | Break long words to prevent overflow - overflow-wrap: break-word |
| `breakAll` | Word Break |  | Break between any two characters - word-break: break-all |
| `breakKeep` | Word Break |  | Do not break CJK text - word-break: keep-all |

Layout and utility props (Border, Cursor, Display, Flex Direction, Focus Visible, Gap, Height, Hide, Items, Justify, Overflow, Position, Responsive Sizing, Responsive Width, Ring, Shadow, Show, Transition, Transparent, Whitespace, Width, Wrap) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
