# Icon button

An icon-only button in every style, that can hold on or off as a toggle (bold, italic, a thumbs up) and name itself in a tooltip with its key.

Tier: free. Type: component. Category: actions. Tags: keyboard, motion.

## Install

```bash
npx shadcn@latest add @pixilui/icon-button
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/icon-button.tsx`.

## Preview

Live preview: https://pixilui.com/components/icon-button

Knobs:

- `show`: `styles`, `more`, `sizes`, `states`

Signature: A pressed toggle sets into the surface, its lit top giving way to an inner shade, instead of changing colour.

Styles: `quiet` (default), `lively`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: quiet https://pixilui.com/r/standalone/icon-button.tsx, lively https://pixilui.com/r/standalone/icon-button.lively.tsx.

## Code

`components/pixilui/icon-button.tsx`

```tsx
"use client";

import { forwardRef, useState, type MouseEvent, type ReactNode } from "react";
import { Button, type ButtonProps, type ButtonSize, type ButtonVariant } from "./button";
import { Tooltip } from "./tooltip";

/*
  The locked button with only an icon: a square in quiet, a circle in lively (foundation.css
  section 7). Two things it adds on top of Button:
  toggle   pass pressed or defaultPressed and it holds on or off with aria-pressed. "set" sinks a
           pressed toggle into the surface; "glyph" fills its icon instead, for a thumbs up.
  tooltip  pass tooltip (and a shortcut) and it names itself on hover and focus, with the key.
*/

export type IconButtonProps = Omit<ButtonProps, "children" | "icon" | "aria-label" | "pressed" | "onClick" | "variant" | "size" | "shortcut"> & {
  /** The icon (Lucide, or your own). Pass it as a node; the button sets its size and stroke. */
  icon: ReactNode;
  /** The accessible name, and the tooltip's text unless tooltip says otherwise ("Attach files"). */
  "aria-label": string;
  /** Visual weight, as on Button: primary, secondary (the glass, default), secondary-2 (no fill until hover), danger. */
  variant?: ButtonVariant;
  /** Height and width, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44. */
  size?: ButtonSize;
  /** Show a tooltip on hover and focus: true uses aria-label, a string says something else. */
  tooltip?: boolean | string;
  /** A key hint in the tooltip ("⌘U"). Needs tooltip. */
  shortcut?: string;
  /** Makes it a toggle, controlled. Pair with onPressedChange. */
  pressed?: boolean;
  /** Makes it a toggle, uncontrolled, starting on or off. */
  defaultPressed?: boolean;
  /** Runs with the next state whenever the toggle is pressed. */
  onPressedChange?: (pressed: boolean) => void | Promise<void>;
  /** How a pressed toggle looks: "set" (set into the surface, the default) or "glyph" (the icon fills, for feedback). */
  pressedLook?: "set" | "glyph";
  /** Runs on every press, before a toggle changes. Call event.preventDefault() to keep its state. */
  onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
};

/** An icon-only button in any style, that can hold on or off and name itself with a tooltip and its key. */
export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(function IconButton(
  { icon, tooltip, shortcut, pressed: pressedProp, defaultPressed, onPressedChange, onClick, ...rest },
  ref,
) {
  const toggle = pressedProp !== undefined || defaultPressed !== undefined || onPressedChange !== undefined;
  const [own, setOwn] = useState(Boolean(defaultPressed));
  const pressed = toggle ? (pressedProp ?? own) : undefined;

  const button = (
    <Button
      {...rest}
      ref={ref}
      icon={icon}
      pressed={pressed}
      data-state={toggle ? (pressed ? "on" : "off") : undefined}
      onClick={(event: MouseEvent<HTMLButtonElement>) => {
        onClick?.(event);
        if (!toggle || event.defaultPrevented) return;
        if (pressedProp === undefined) setOwn(!pressed);
        void onPressedChange?.(!pressed);
      }}
    />
  );
  if (!tooltip) return button;
  return (
    <Tooltip label={typeof tooltip === "string" ? tooltip : rest["aria-label"]} shortcut={shortcut}>
      {button}
    </Tooltip>
  );
});
```

## Usage

When to use:

- Actions whose icon is understood at a glance: close, more, attach, send.
- Formatting toggles in an editor or composer: bold, italic, link. Feedback under an answer: good or bad response.
- A dense row where text labels would crowd: name each one with a tooltip and its key.

When not to use:

- An action whose icon needs explaining every time: use Button with a label.
- Exactly one of several options: use the segmented control.
- A row of tools on one tray with arrow-key movement: use Button group.

```tsx
import type { ReactNode } from "react";
import { IconAdd } from "@/components/pixilui/icons/add";
import { IconAttach } from "@/components/pixilui/icons/attach";
import { IconDelete } from "@/components/pixilui/icons/delete";
import { IconDictate } from "@/components/pixilui/icons/dictate";
import { IconDislike } from "@/components/pixilui/icons/dislike";
import { IconLike } from "@/components/pixilui/icons/like";
import { IconLink } from "@/components/pixilui/icons/link";
import { IconMention } from "@/components/pixilui/icons/mention";
import { IconMore } from "@/components/pixilui/icons/more";
import { IconSendStop } from "@/components/pixilui/icons/send-stop";
import { IconShare } from "@/components/pixilui/icons/share";
import { TooltipProvider } from "@/components/pixilui/tooltip";
import { IconButton } from "@/components/pixilui/icon-button";

// The item page's one row, Show: Styles, More styles, Icons and sizes, States (the button pages' layout).
export const knobs = {
  show: ["styles", "more", "sizes", "states"],
} as const;

/** The static Hugeicons cancel-01 (MIT, Copyright (c) 2025 Hugeicons): close has no animated icon. */
function IconClose() {
  return (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
      <path d="M19 5L5 19M5 5l14 14" />
    </svg>
  );
}

const Row = ({ children }: { children: ReactNode }) => <div className="flex flex-wrap items-center justify-center gap-2.5">{children}</div>;
const Cap = ({ children }: { children: ReactNode }) => <p className="-mt-2 text-center text-caption text-muted">{children}</p>;

function Shown({ show }: { show: string }) {
  if (show === "more")
    return (
      <>
        <Row>
          <IconButton variant="tinted" aria-label="Share" icon={<IconShare />} />
          <IconButton variant="soft" aria-label="More actions" icon={<IconMore />} />
          <IconButton variant="inverse" aria-label="New run" icon={<IconAdd data-stroke="heavy" />} />
          <IconButton variant="danger-quiet" aria-label="Delete run" icon={<IconDelete />} />
          <IconButton variant="secondary-2" aria-label="Close" icon={<IconClose />} />
        </Row>
        <Cap>Tinted, Soft, Inverse, Quiet danger, and a close in Secondary 2</Cap>
      </>
    );
  if (show === "sizes")
    return (
      <>
        <Row>
          {(["xs", "sm", "md", "lg", "xl"] as const).map((size) => (
            <IconButton key={size} size={size} aria-label="More actions" icon={<IconMore />} />
          ))}
        </Row>
        <Cap>24, 28, 32 (default), 36, 44</Cap>
        <Row>
          <IconButton aria-label="Bold" icon={<b className="font-medium">B</b>} defaultPressed />
          <IconButton aria-label="Italic" icon={<i className="font-normal">I</i>} defaultPressed={false} />
          <IconButton aria-label="Link" icon={<IconLink />} defaultPressed={false} />
        </Row>
        <Cap>Toggles set into the surface when on</Cap>
      </>
    );
  if (show === "states")
    return (
      <>
        <TooltipProvider>
          <Row>
            <IconButton variant="secondary-2" aria-label="Attach files" icon={<IconAttach />} tooltip shortcut="⌘U" />
            <IconButton variant="secondary-2" aria-label="Mention a file or tool" icon={<IconMention />} tooltip shortcut="@" />
            <IconButton variant="secondary-2" aria-label="Dictate" icon={<IconDictate />} tooltip />
            <IconButton variant="primary" aria-label="Send" icon={<IconSendStop />} tooltip shortcut="↵" />
          </Row>
        </TooltipProvider>
        <Cap>Hover or tab along: each names itself with its key</Cap>
        <Row>
          <IconButton variant="secondary-2" size="sm" aria-label="Good response" icon={<IconLike />} pressedLook="glyph" defaultPressed />
          <IconButton variant="secondary-2" size="sm" aria-label="Bad response" icon={<IconDislike />} pressedLook="glyph" defaultPressed={false} />
          <IconButton aria-label="Delete run" icon={<IconDelete />} aria-disabled="true" />
        </Row>
        <Cap>Feedback fills its glyph; disabled stays focusable</Cap>
      </>
    );
  return (
    <>
      <Row>
        <IconButton variant="primary" size="lg" aria-label="New run" icon={<IconAdd data-stroke="heavy" />} />
        <IconButton size="lg" aria-label="More actions" icon={<IconMore />} />
        <IconButton variant="secondary-2" size="lg" aria-label="Attach files" icon={<IconAttach />} />
        <IconButton variant="danger" size="lg" aria-label="Delete run" icon={<IconDelete />} />
      </Row>
      <Cap>Primary, Secondary (the glass), Secondary 2, Destructive</Cap>
    </>
  );
}

export default function IconButtonDemo({ show = "styles" }: KnobValues<typeof knobs>) {
  return (
    <div key={show} className="grid justify-items-center gap-4.5" aria-live="polite">
      <Shown show={show} />
    </div>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `pressedLook` | `"set" \| "glyph"` |  | How a pressed toggle looks: "set" (set into the surface, the default) or "glyph" (the icon fills, for feedback). |
| `icon` (required) | `ReactNode` |  | The icon (Lucide, or your own). Pass it as a node; the button sets its size and stroke. |
| `aria-label` (required) | `string` |  | The accessible name, and the tooltip's text unless tooltip says otherwise ("Attach files"). |
| `variant` | `ButtonVariant` |  | Visual weight, as on Button: primary, secondary (the glass, default), secondary-2 (no fill until hover), danger. |
| `size` | `ButtonSize` |  | Height and width, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44. |
| `tooltip` | `boolean \| string` |  | Show a tooltip on hover and focus: true uses aria-label, a string says something else. |
| `shortcut` | `string` |  | A key hint in the tooltip ("⌘U"). Needs tooltip. |
| `pressed` | `boolean` |  | Makes it a toggle, controlled. Pair with onPressedChange. |
| `defaultPressed` | `boolean` |  | Makes it a toggle, uncontrolled, starting on or off. |
| `onPressedChange` | `(pressed: boolean) => void \| Promise<void>` |  | Runs with the next state whenever the toggle is pressed. |
| `onClick` | `(event: MouseEvent<HTMLButtonElement>) => void` |  | Runs on every press, before a toggle changes. Call event.preventDefault() to keep its state. |

## Keyboard

- `Tab`: Moves focus to the button; a tooltip, if it has one, opens on keyboard focus.
- `Enter`: Activates it, or turns a toggle on or off.
- `Space`: Same as Enter, on release.
- `Esc`: Closes its tooltip and keeps focus on the button.

## Accessibility

- aria-label is required at the type level; it is also the tooltip's text unless tooltip names something else.
- A toggle sets aria-pressed true or false and keeps the same name in both states ("Bold"). data-state is on or off.
- The icon is aria-hidden; the shortcut in the tooltip is a hint, not part of the name.
- Small sizes reach a 40px hit area on desktop and 44 on touch without growing.

## Motion

- Press scales to .96 in 150ms ease-out; a pressed toggle sets in (an inner shade, no lit top) on the same quick curve.
- The glyph look fills the icon in 150ms. The tooltip waits once, then slides between neighbours.
- Reduced motion: no press scale; state changes and the tooltip land at once.

## Responsive

- A square in quiet, a circle in lively, at the button's five sizes. Touch screens never show tooltips, so the name must not depend on one.

## Performance

- The look is CSS in the foundation; a toggle keeps one boolean when uncontrolled. The tooltip mounts only when it opens.

## Notes for AI

- Always pass aria-label and icon (Lucide at 1.5px, or your own). Add tooltip (true or a string) and shortcut for tools people repeat.
- Toggle: pressed with onPressedChange(next), or defaultPressed. pressedLook="set" for formatting, "glyph" with variant="secondary-2" size="sm" for feedback thumbs.
- Styles as on Button: secondary (default glass), secondary-2 (no fill until hover, for quiet rows), primary (one per surface), danger.
- A plain letter (B, I) can be the icon: pass an element like <b className="font-medium">B</b>.

## Related

- [Button](https://pixilui.com/components/button/markdown): The PixilUI button in four main styles and four more, five sizes from 24 to 44, with icons, loading, disabled, a count and a shortcut.
- [Button group](https://pixilui.com/components/button-group/markdown): Icon buttons on one glass tray, each named by a tooltip with its key hint, with arrow keys to move along the row: the toolbar under a composer or an editor.
- [Message actions](https://pixilui.com/components/message-actions/markdown): The row under an assistant's answer: copy, regenerate, good and bad feedback and more, as quiet icon buttons with tooltips, where copy draws a check and feedback fills its glyph.
