Icon button
FreeAn 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.
npx shadcn@latest add @pixilui/icon-buttonPreview
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>
);
}When to use
Use it when
- 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.
Use something else when
- 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.
Installation
npx shadcn@latest add @pixilui/icon-buttonAdds icon-button.tsx with button.tsx and tooltip.tsx, and installs clsx, tailwind-merge and @base-ui/react. First PixilUI item in this app? Add the registry once, as in Installation.
"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>
);
});
It uses Button and Tooltip: add them the same way. Then npm i clsx tailwind-merge @base-ui/react, and add the foundation once per app, as in Installation.
Usage
import { IconButton } from "@/components/pixilui/icon-button";
<IconButton
icon={icon}
aria-label={aria-label}
onPressedChange={onPressedChange}
onClick={onClick}
/>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.
API reference
iconRequiredReactNode–The icon (Lucide, or your own). Pass it as a node; the button sets its size and stroke.aria-labelRequiredstring–The accessible name, and the tooltip's text unless tooltip says otherwise ("Attach files").pressedLook"set" | "glyph"–How a pressed toggle looks: "set" (set into the surface, the default) or "glyph" (the icon fills, for feedback).variantButtonVariant–Visual weight, as on Button: primary, secondary (the glass, default), secondary-2 (no fill until hover), danger.sizeButtonSize–Height and width, as on Button: xs 24, sm 28, md 32 (default), lg 36, xl 44.tooltipboolean | string–Show a tooltip on hover and focus: true uses aria-label, a string says something else.shortcutstring–A key hint in the tooltip ("⌘U"). Needs tooltip.pressedboolean–Makes it a toggle, controlled. Pair with onPressedChange.defaultPressedboolean–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
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.