Icon button
A square button for one icon: refresh, download, delete. It is the button with fixed sides, and its type refuses to compile without an aria-label. For an action that needs words use the button; for an icon that stays pressed use the toggle.
Install
npx shadcn@latest add https://thrumui.dev/r/icon-button.jsonAdds components/ui/icon-button.tsx. Nothing else in your project changes.
import { IconButton } from "@/components/ui/icon-button";Props
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | Required | What the button does, in words. It is the button's only name, so the type requires it. |
variant | "ghost" | "outline" | "secondary" | "primary" | "destructive" | Default "ghost" | The button variant. ghost suits a toolbar, outline a button that stands alone. |
size | "sm" | "md" | "lg" | Default "md" | sm is a 28 pixel square, md 32, lg 40. The icon is 16 pixels wide, 20 in lg. |
round | boolean | Default false | A circle instead of a rounded square. With the theme's radius at 0 it stays square. |
loading | boolean | Default false | Shows a spinner in place of the icon and ignores presses. The button keeps its name and the focus, and is announced as busy. |
disabled | boolean | Default false | The native attribute: the button leaves the tab order and cannot be pressed. |
children | ReactNode | – | The icon: one inline SVG that draws with currentColor. The button sets its size. |
className | string | – | Extra classes, merged last. |
...props | ComponentProps<"button"> | – | Every other button attribute and event handler is passed through. |
Motion and sound
Motion
- Background, border and icon colour change over 150 ms on hover.
- A press scales the button to 97% and back over 150 ms. A disabled or loading button does not move.
- Reduced motion: colours and scale change at once and the spinner stands still.
Sound
Silent. This component makes no sound.
Accessibility
- It is a native button element. Its name is the aria-label, which the type makes mandatory.
- The icon is decoration: give the SVG aria-hidden so it is not read twice.
- A sighted reader gets no name from an icon. Pair it with the tooltip when the icon is not a familiar one.
- The smallest size is 28 pixels square, above the 24 pixel minimum for pointer targets.
- The focus ring is 2 pixels of the theme's ring colour, 2 pixels away from the button.
| Keys | Action |
|---|---|
| Tab | Focus the button. A loading button stays focusable; a disabled one is skipped. |
| Enter / Space | Press the button. Nothing happens while it is loading. |
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { BUTTON_SURFACE, Button, type ButtonVariant } from "@/components/ui/button";
export type IconButtonVariant = Exclude<ButtonVariant, "link">;
export type IconButtonSize = "sm" | "md" | "lg";
export type IconButtonProps = Omit<ComponentProps<"button">, "aria-label"> & {
/** What the button does, in words. It has no text, so this is its name. Example: "Refresh data". */
"aria-label": string;
/** The button variant. `ghost` suits a toolbar, `outline` a button that stands alone. */
variant?: IconButtonVariant;
/** `sm` is a 28 pixel square, `md` 32, `lg` 40. The icon is 16 pixels, 20 in `lg`. */
size?: IconButtonSize;
/** A circle instead of a rounded square. It follows the theme's radius: square corners stay square. */
round?: boolean;
/** Shows a spinner in place of the icon and ignores presses. The button keeps its name and the focus. */
loading?: boolean;
};
const SIZE: Record<IconButtonSize, string> = {
sm: "size-7 [&_svg]:size-4",
md: "size-8 [&_svg]:size-4",
lg: "size-10 [&_svg]:size-5",
};
/**
* A square button that holds one icon. It is the button with fixed sides and a required `aria-label`,
* so it cannot ship without a name. Pass the icon as its child: an inline SVG that draws with `currentColor`.
*/
export function IconButton({ variant = "ghost", size = "md", round = false, className, ...props }: IconButtonProps) {
return (
<Button
data-slot="icon-button"
data-surface={BUTTON_SURFACE[variant]}
variant={variant}
size="icon"
className={cn(SIZE[size], round && "rounded-[calc(var(--radius)*9999)]", className)}
{...props}
/>
);
}Files and dependencies
- Adds
components/ui/icon-button.tsx- Packages
- None
- Brings along
- Button
Changelog
- 1.0.0 · 2026-12-03First release.