IconButton
@codeui/styles v0.3.0
A square icon action in plain HTML, built on the Button classes.
Load @codeui/styles/iconbutton.css, or all.css. The entry includes the Button styles. The Solid component is documented in IconButton.
Demo
<button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-size="sm" data-variant="secondary" aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg></button>Markup
| Part | Element | Class | data-cui |
|---|---|---|---|
| Root | button or a | cui-button cui-iconbutton | icon-button |
| Icon | svg | None | None |
| Loading overlay | div | cui-loading | None |
The root takes all three classes. cui-iconbutton removes the padding and sets the width to the button height, so the button is square.
| Attribute | Values | Default |
|---|---|---|
data-variant | primary, secondary, tertiary, caution, negative | None. Set one. |
data-size | xs, sm, md, lg | sm |
data-ghost | Presence | Off |
data-pill | Presence | Off |
data-block | Presence | Off |
Variants
Colors
<div class="cui-stack" data-direction="row" data-gap="3" data-align="center" data-wrap="true"> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="primary" aria-label="Export PNG"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M12 4v12" /> <path d="m7 11 5 5 5-5" /> <path d="M5 20h14" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="tertiary" aria-label="Share snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="18" cy="5" r="3" /> <circle cx="6" cy="12" r="3" /> <circle cx="18" cy="19" r="3" /> <path d="m8.6 13.5 6.8 4M15.4 6.5l-6.8 4" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="caution" aria-label="Overwrite export"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M20 12a8 8 0 1 1-2.34-5.66" /> <path d="M20 4v5h-5" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="negative" aria-label="Delete snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 7h16" /> <path d="M10 11v6M14 11v6" /> <path d="M6 7l1 13h10l1-13" /> <path d="M9 7V4h6v3" /> </svg> </button></div>primary uses brand fill. secondary is neutral. tertiary uses soft brand fill. caution and negative use warning and danger colors. There is no default data-variant, so set one.
Sizes
<div class="cui-stack" data-direction="row" data-gap="3" data-align="center" data-wrap="true"> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" data-size="xs" aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" data-size="sm" aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" data-size="md" aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" data-size="lg" aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button></div>xs, sm, md, and lg are 28px, 36px, 44px, and 52px square at comfortable density. Omitting data-size renders sm.
Ghost
<div class="cui-stack" data-direction="row" data-gap="3" data-align="center" data-wrap="true"> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" data-ghost aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="primary" data-ghost aria-label="Export PNG"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M12 4v12" /> <path d="m7 11 5 5 5-5" /> <path d="M5 20h14" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="negative" data-ghost aria-label="Delete snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 7h16" /> <path d="M10 11v6M14 11v6" /> <path d="M6 7l1 13h10l1-13" /> <path d="M9 7V4h6v3" /> </svg> </button></div>data-ghost removes the idle fill. The data-variant colors appear on hover.
Pill
<div class="cui-stack" data-direction="row" data-gap="3" data-align="center" data-wrap="true"> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="primary" data-pill aria-label="Export PNG"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M12 4v12" /> <path d="m7 11 5 5 5-5" /> <path d="M5 20h14" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" data-pill aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button></div>data-pill uses fully rounded corners, so the button reads as a circle.
Link
<a href="#codeimage" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" aria-label="Open CodeImage"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M14 4h6v6" /> <path d="M20 4 10 14" /> <path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5" /> </svg></a>The classes work on an anchor. It keeps link semantics: Enter follows it and Space does not. Use Link for ordinary navigation in text.
Block
data-block sets width to 100% and flex to 1. It competes with the square width rule, so avoid full-width icon actions.
States
Disabled
<div class="cui-stack" data-direction="row" data-gap="3" data-align="center" data-wrap="true"> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="primary" disabled data-disabled aria-label="Export PNG"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M12 4v12" /> <path d="m7 11 5 5 5-5" /> <path d="M5 20h14" /> </svg> </button> <button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" disabled data-disabled aria-label="Copy snippet"> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg> </button></div>Set both disabled and data-disabled. The native attribute removes the button from the tab order and blocks clicks. The dimmed fill selects on data-disabled.
Loading
<button type="button" class="cui-button cui-iconbutton" data-cui="iconbutton" data-variant="secondary" data-loading aria-busy="true" aria-disabled="true" aria-label="Copy snippet"> <div class="cui-loading"> <span class="cui-loading-pointer-group" aria-label="Loading"> <i class="cui-loading-pointer"></i> <i class="cui-loading-pointer" style="--cui-loading-pointer-delay: 0.2s"></i> <i class="cui-loading-pointer" style="--cui-loading-pointer-delay: 0.4s"></i> </span> </div> <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="8" y="8" width="12" height="12" rx="2" /> <path d="M16 8V4H4v12h4" /> </svg></button>data-loading blocks pointer events and places three animated dots over the icon. The dots are plain markup, so the animation runs without a script. Set aria-busy="true" and aria-disabled="true" yourself, and ignore activation while the work runs.
What you supply
- An accessible name on every icon button, such as
aria-label="Copy snippet". Name the action, not the icon shape. aria-hidden="true"on the icon.- A tooltip that shows the name on hover and focus. The kit pairs every icon button with a Tooltip.
- Click handlers, plus a guard that ignores Enter, Space, and clicks while
data-loadingis set.
Helper
import "@codeui/styles/iconbutton.css";
import { applyStyleProps } from "@codeui/styles";
import { iconButtonStyleProps } from "@codeui/styles/iconbutton";
const copy = document.querySelector("button")!;
applyStyleProps(copy, iconButtonStyleProps({ variant: "secondary" }));
// class="cui-button cui-iconbutton" data-cui="iconbutton" data-size="sm" data-variant="secondary"