aiellie

Search

Find a component, template or page

GitHub
Design

Tooltip Icon Button

An icon button that carries its own tooltip and screen-reader label.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/tooltip-icon-button"

shadcn needs the @aiellie registry in components.json first. npx aiellie add tooltip-icon-button adds it on its first run, then installs the same way.

Tooltip icon button is a small ghost button that carries only an icon and names itself in a tooltip. The name goes into the button as screen-reader text too, so a row of icons stays labelled without any visible words. It is the button behind the menus and message actions in this registry.

Getting started

  1. Add a button

    Give it the icon as its child and the label as tooltip. It takes every prop Button does, so onClick, disabled and the rest work as usual.

    components/copy-button.tsx
    "use client"
    
    import { Copy01Icon } from "@hugeicons/core-free-icons"
    import { HugeiconsIcon } from "@hugeicons/react"
    
    import { TooltipIconButton } from "@/components/ui/tooltip-icon-button"
    
    export function CopyButton({ text }: { text: string }) {
      return (
        <TooltipIconButton
          tooltip="Copy"
          onClick={() => navigator.clipboard.writeText(text)}
        >
          <HugeiconsIcon aria-hidden icon={Copy01Icon} />
        </TooltipIconButton>
      )
    }
  2. Use it as a trigger

    It forwards its ref, so it can be the render of a Base UI trigger. The menu opens from it and the tooltip still shows on hover.

    <MenuTrigger render={<TooltipIconButton tooltip="More" />}>
      <HugeiconsIcon aria-hidden icon={MoreHorizontalIcon} />
    </MenuTrigger>

Anatomy

<button data-slot="button"> {/* the tooltip's trigger */}
  {children} {/* the icon */}
  <span class="sr-only">{tooltip}</span>
</button>

<div data-slot="tooltip-content"> {/* on hover and focus */}
  {tooltip}
  <kbd data-slot="kbd">{shortcut}</kbd> {/* only with `shortcut` */}
</div>

The button is 24px square with a 4px pad, so a 16px icon sits centred. The icon rests in muted-foreground and takes the foreground color on hover, and the button shrinks a little while pressed. Each button brings its own TooltipProvider, so it works without one higher up.

Examples

Keyboard shortcut

shortcut adds the keys that do the same thing after the label, set as a kbd.

Tooltip side

The tooltip opens below by default. Set side where the space is, like top for a button at the bottom of the screen.

Size

className is merged last, so a larger button is a size class away. Keep the padding in step so the icon stays centred.

<TooltipIconButton tooltip="Send" className="size-8 p-2">
  <HugeiconsIcon aria-hidden icon={ArrowUp02Icon} />
</TooltipIconButton>

API reference

TooltipIconButton

PropTypeDefaultDescription
tooltipstringrequiredThe label, shown in the tooltip and read out as the button's name.
shortcutstringKeys that do the same, shown after the label, like ⌘K.
side"top" | "bottom" | "left" | "right""bottom"Which side of the button the tooltip opens on.
variant / sizeButton variants"ghost" / "icon"Passed to the underlying Button.
classNamestringMerged onto the button, after its own classes.

All other Button props, and the ref, are forwarded to the button.