Skip to main content
Decoderen
Browse documentation

IconButton

Run one compact action shown only as an icon: Material 3's icon button.

Variants

Show code
Example.astroAstro
---
import IconButton from '@dhekode/ui/primitives/IconButton.tsx';
import PhosphorIcon from '@dhekode/icons/PhosphorIcon.tsx';
import { ShareNetworkIcon } from '@dhekode/icons/phosphor';
---

<IconButton variant="standard" label="Share">
  <PhosphorIcon icon={ShareNetworkIcon} size={20} />
</IconButton>
<IconButton variant="filled" label="Share">
  <PhosphorIcon icon={ShareNetworkIcon} size={20} />
</IconButton>
<IconButton variant="tonal" label="Share">
  <PhosphorIcon icon={ShareNetworkIcon} size={20} />
</IconButton>
<IconButton variant="outlined" label="Share">
  <PhosphorIcon icon={ShareNetworkIcon} size={20} />
</IconButton>

Small

Show code
Example.astroAstro
---
import IconButton from '@dhekode/ui/primitives/IconButton.tsx';
import PhosphorIcon from '@dhekode/icons/PhosphorIcon.tsx';
import { ShareNetworkIcon } from '@dhekode/icons/phosphor';
---

<IconButton size="sm" variant="outlined" label="Share">
  <PhosphorIcon icon={ShareNetworkIcon} size={16} />
</IconButton>

Disabled

Show code
Example.astroAstro
---
import IconButton from '@dhekode/ui/primitives/IconButton.tsx';
import PhosphorIcon from '@dhekode/icons/PhosphorIcon.tsx';
import { ShareNetworkIcon } from '@dhekode/icons/phosphor';
---

<IconButton variant="filled" label="Share" disabled>
  <PhosphorIcon icon={ShareNetworkIcon} size={20} />
</IconButton>
API reference
label

Type: string

Default: required

The accessible name.

variant

Type: 'standard' | 'filled' | 'tonal' | 'outlined'

Default: 'standard'

Emphasis, from a bare icon to a filled circle.

size

Type: 'sm' | 'md'

Default: 'md'

md is the 40px button; sm is the chip height, for toolbars beside chips.

href

Type: string

Default: unset

Renders a link instead of a button.

disabled

Type: boolean

Default: false

Disables the button; a disabled link loses its href.

className

Type: string | undefined

Default: unset

Adds classes to the React component root, after its own utilities. React components take className, not class.

Slots: children: the decorative icon, such as PhosphorIcon.

Use only for actions whose icon is widely understood, such as close or share. The 40px circle keeps a 44px touch target.

Original component source
packages/ui/src/primitives/IconButton.tsxAstro
import type { ComponentProps, ReactNode } from 'react';
import { classNames } from '@dhekode/utils/class-names';
import { inertLink, ownStates, stateLayer } from '@dhekode/utils/state-layer';

type Variant = 'standard' | 'filled' | 'tonal' | 'outlined';
type Size = 'sm' | 'md';

interface IconButtonOptions {
  /** The accessible name. Required: the button has no visible text. */
  label: string;
  /** The decorative icon, such as <PhosphorIcon name="..." />. */
  children: ReactNode;
  variant?: Variant | undefined;
  /** md is the 40px button; sm is the chip height, for toolbars beside chips and small buttons. */
  size?: Size | undefined;
  disabled?: boolean | undefined;
  className?: string | undefined;
}

/** `style` is left out: the hash-based CSP blocks inline styles (AGENTS.md section 5). */
type Native<Element extends 'a' | 'button'> = Omit<
  ComponentProps<Element>,
  keyof IconButtonOptions | 'style'
>;

export type IconButtonProps = IconButtonOptions &
  ((Native<'button'> & { href?: undefined }) | (Omit<Native<'a'>, 'href'> & { href: string }));

/** A circle at the size's height (40px, or 32px small) with the state layer, and a centred touch target of at least 44px. */
const base =
  'dk-icon-button relative isolate inline-grid shrink-0 cursor-pointer place-items-center rounded-full border-(length:--dk-border-width-hairline) border-solid no-underline outline-(color:--dk-surface-focus) transition-colors duration-(--dk-motion-duration-quick) ease-out before:rounded-full after:absolute after:top-1/2 after:left-1/2 after:size-(--dk-layout-touch-target) after:-translate-1/2 disabled:cursor-not-allowed disabled:text-disabled-text aria-disabled:cursor-not-allowed aria-disabled:text-disabled-text';

/** Component tokens, Material 3's component tier: each variant's container, icon and outline. */
const variants: Record<Variant, string> = {
  standard: 'border-transparent bg-transparent text-surface-text',
  filled:
    'border-surface-action bg-surface-action text-surface-action-text disabled:border-disabled-border disabled:bg-disabled-background aria-disabled:border-disabled-border aria-disabled:bg-disabled-background',
  tonal:
    'border-transparent bg-surface-tint text-surface-accent disabled:bg-disabled-background aria-disabled:bg-disabled-background',
  outlined:
    'border-surface-muted bg-transparent text-surface-text disabled:border-disabled-border aria-disabled:border-disabled-border',
};

const sizes: Record<Size, string> = {
  md: 'size-(--dk-layout-button-height)',
  sm: 'size-(--dk-layout-control-height-sm)',
};

/** Material 3's icon button: one compact action, named by `label`. */
export default function IconButton({
  label,
  children,
  variant = 'standard',
  size = 'md',
  disabled = false,
  className,
  ...native
}: IconButtonProps) {
  const shared = {
    className: classNames(base, stateLayer, ownStates, variants[variant], sizes[size], className),
    'aria-label': label,
    'data-variant': variant,
    'data-size': size,
  };

  if (native.href !== undefined) {
    const { href, ...rest } = native;
    if (disabled) {
      return (
        <a {...rest} {...shared} {...inertLink}>
          {children}
        </a>
      );
    }
    return (
      <a {...rest} {...shared} href={href}>
        {children}
      </a>
    );
  }

  const { type = 'button', ...rest } = native;
  return (
    <button {...rest} {...shared} type={type} disabled={disabled}>
      {children}
    </button>
  );
}

Best practices

  • Pair every icon button with a clear label.
  • Prefer a text Button when space allows.