# Tooltip

A short label that names a control on hover or focus.

A dark chip that names a control, with an optional keyboard-shortcut hint. Opens after a short delay on hover and at once on keyboard focus; once one tooltip is open its neighbours open with no delay and no animation. Never holds interactive content. Behaviour comes from the Zag tooltip machine, through Ark UI in React, Vue and Svelte.

Kind: component · Category: Overlays · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `tooltip` machine, through Ark UI

## Add

```bash
npx fndn add tooltip
```

It writes the folder `tooltip/` into the project's ui folder (`index.ts`, `tooltip.tsx`, `Tooltip.vue`, `Tooltip.svelte`, `tooltip.recipe.ts`), and what it is built from: [Button](/components/button.md), [Kbd](/components/kbd.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Tooltip } from "@/components/ui/tooltip";
```

```vue
import { Tooltip } from "@/components/ui/tooltip";
```

```svelte
import { Tooltip } from "$lib/components/ui/tooltip";
```

## Parts

In the DOM the root carries `data-slot="tooltip"` and each part `data-slot="tooltip-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `tooltip` |  | The chip itself (data-slot="tooltip"): role=tooltip, data-state=open\|closed, data-instant for a neighbour, kbd-instant. Rendered in a portal on first open. |
| `positioner` | `div` | `tooltip-positioner` |  | Placed by the engine; exposes --transform-origin |
| `label` | `span` | `tooltip-label` |  |  |
| `shortcut` | `kbd` | `tooltip-shortcut` | optional | Keyboard shortcut hint: the real Kbd (plain, one key holding the hint), marked data-tooltip="shortcut", in the muted tooltip colour |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `content` | `string` |  | The label. |
| `shortcut` | `string` |  | Shortcut hint, e.g. ⌘C. |
| `placement` | `"top" \| "top-start" \| "top-end" \| "bottom" \| "bottom-start" \| "bottom-end" \| "left" \| "right"` | `"top"` | Preferred side; flips when there is no room. |
| `openDelay` | `number` | `400` | Milliseconds before the first tooltip opens on hover. |
| `disabled` | `boolean` | `false` |  |
| `triggerLabel` | `string` |  | Renders a default Button trigger with this label. Otherwise the trigger is your own element (child / default slot / trigger snippet). |
| `id` | `string` |  | Machine id; generated when omitted. Set it when several React roots or Vue apps on one page each render tooltips, so their generated ids cannot collide. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Slots

| Slot | Content |
| --- | --- |
| `default` | The trigger element (React child, Vue default slot). Svelte: a `trigger` snippet that spreads the props it receives. React and Vue pass any other props and listeners (with the ref) on to the trigger, so a Tooltip can be the as-child trigger of a Menu or a Dialog close button; in Svelte merge both props functions on your element. |
| `content` | Vue: rich label instead of the content prop |

## States

`open`, `instant`, `disabled`

## Keyboard and accessibility

Role: tooltip.

Keyboard:

- Focus on the trigger (keyboard) shows the tooltip at once, with no animation
- Escape hides it
- Blur hides it

Notes:

- The trigger is described by the tooltip (aria-describedby) while it is open; an icon-only trigger still needs its own aria-label.
- Not shown on touch: put essential information elsewhere.
- Hovering from one trigger to the next skips both the delay and the animation.
- A press on the trigger closes the tooltip, and cancels one still waiting out its delay (also when the trigger's own engine prevents the press's default, as a date picker's or a menu's does); none opens again until the pointer leaves.

## Motion

- chip: opacity + scale from motion.popup-scale out of the trigger (--transform-origin), duration.tooltip, easing.out-strong; closes at duration.fast
- neighbour of an open tooltip: no delay, no transition (data-instant)
- keyboard focus: no transition (kbd-instant)

## Tokens

`color.tooltip-bg`, `color.tooltip-fg`, `color.tooltip-muted`, `color.tooltip-border`, `radius.chip`, `text.xs`, `duration.tooltip`, `duration.fast`, `easing.out-strong`, `motion.popup-scale`

## Dependencies

- [Button](/components/button.md)
- [Kbd](/components/kbd.md)

## Examples

### Formatting toolbar of a comment

React · `comment-toolbar.tsx`

```tsx
import { Code, Link, Paperclip, TextB, TextItalic } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Tooltip } from "@/components/ui/tooltip";

const tools = [
  { label: "Bold", shortcut: "⌘B", icon: TextB },
  { label: "Italic", shortcut: "⌘I", icon: TextItalic },
  { label: "Code", shortcut: "⌘E", icon: Code },
  { label: "Link", shortcut: "⌘K", icon: Link },
  { label: "Attach a file", icon: Paperclip },
];

/* once one tooltip is open, its neighbours open at once, with no animation */
export default function CommentToolbar() {
  return (
    <div className="flex w-80 flex-col gap-2">
      <div className="flex gap-0.5" role="toolbar" aria-label="Formatting">
        {tools.map(({ label, shortcut, icon: Icon }) => (
          <Tooltip key={label} content={label} shortcut={shortcut}>
            <Button variant="ghost" size="sm" square aria-label={label} icon={<Icon data-slot="icon" weight="bold" />} />
          </Tooltip>
        ))}
      </div>
      <Input multiline rows={3} aria-label="Comment on #482" placeholder="Leave a review comment" />
    </div>
  );
}
```

Vue · `comment-toolbar.vue`

```vue
<script setup lang="ts">
import { PhCode, PhLink, PhPaperclip, PhTextB, PhTextItalic } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Tooltip } from "@/components/ui/tooltip";

const tools = [
  { label: "Bold", shortcut: "⌘B", icon: PhTextB },
  { label: "Italic", shortcut: "⌘I", icon: PhTextItalic },
  { label: "Code", shortcut: "⌘E", icon: PhCode },
  { label: "Link", shortcut: "⌘K", icon: PhLink },
  { label: "Attach a file", icon: PhPaperclip },
];
</script>

<!-- once one tooltip is open, its neighbours open at once, with no animation -->
<template>
  <div class="flex w-80 flex-col gap-2">
    <div class="flex gap-0.5" role="toolbar" aria-label="Formatting">
      <Tooltip v-for="tool in tools" :key="tool.label" :content="tool.label" :shortcut="tool.shortcut">
        <Button variant="ghost" size="sm" square :aria-label="tool.label">
          <template #icon><component :is="tool.icon" data-slot="icon" weight="bold" /></template>
        </Button>
      </Tooltip>
    </div>
    <Input multiline :rows="3" aria-label="Comment on #482" placeholder="Leave a review comment" />
  </div>
</template>
```

Svelte · `comment-toolbar.svelte`

```svelte
<script lang="ts">
  import Code from "phosphor-svelte/lib/CodeIcon";
  import Link from "phosphor-svelte/lib/LinkIcon";
  import Paperclip from "phosphor-svelte/lib/PaperclipIcon";
  import TextB from "phosphor-svelte/lib/TextBIcon";
  import TextItalic from "phosphor-svelte/lib/TextItalicIcon";
  import { Button } from "$lib/components/ui/button";
  import { Input } from "$lib/components/ui/input";
  import { Tooltip } from "$lib/components/ui/tooltip";

  const tools = [
    { label: "Bold", shortcut: "⌘B", icon: TextB },
    { label: "Italic", shortcut: "⌘I", icon: TextItalic },
    { label: "Code", shortcut: "⌘E", icon: Code },
    { label: "Link", shortcut: "⌘K", icon: Link },
    { label: "Attach a file", icon: Paperclip },
  ];
</script>

<!-- once one tooltip is open, its neighbours open at once, with no animation -->
<div class="flex w-80 flex-col gap-2">
  <div class="flex gap-0.5" role="toolbar" aria-label="Formatting">
    {#each tools as tool (tool.label)}
      <Tooltip content={tool.label} shortcut={tool.shortcut}>
        {#snippet trigger(props)}
          <Button {...props()} variant="ghost" size="sm" square aria-label={tool.label}>
            {#snippet icon()}<tool.icon data-slot="icon" weight="bold" />{/snippet}
          </Button>
        {/snippet}
      </Tooltip>
    {/each}
  </div>
  <Input multiline rows={3} aria-label="Comment on #482" placeholder="Leave a review comment" />
</div>
```

### Icon button with a shortcut

React · `sidebar-toggle.tsx`

```tsx
import { SidebarSimple } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";

export default function SidebarToggle() {
  return (
    <div className="flex items-center gap-2">
      <Tooltip content="Toggle sidebar" shortcut="⌘." placement="bottom-start">
        <Button variant="ghost" size="sm" square aria-label="Toggle sidebar" icon={<SidebarSimple data-slot="icon" weight="bold" />} />
      </Tooltip>
      <span className="text-sm font-medium text-ink">Deploys</span>
    </div>
  );
}
```

Vue · `sidebar-toggle.vue`

```vue
<script setup lang="ts">
import { PhSidebarSimple } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";
</script>

<template>
  <div class="flex items-center gap-2">
    <Tooltip content="Toggle sidebar" shortcut="⌘." placement="bottom-start">
      <Button variant="ghost" size="sm" square aria-label="Toggle sidebar">
        <template #icon><PhSidebarSimple data-slot="icon" weight="bold" /></template>
      </Button>
    </Tooltip>
    <span class="text-sm font-medium text-ink">Deploys</span>
  </div>
</template>
```

Svelte · `sidebar-toggle.svelte`

```svelte
<script lang="ts">
  import SidebarSimple from "phosphor-svelte/lib/SidebarSimpleIcon";
  import { Button } from "$lib/components/ui/button";
  import { Tooltip } from "$lib/components/ui/tooltip";
</script>

<div class="flex items-center gap-2">
  <Tooltip content="Toggle sidebar" shortcut="⌘." placement="bottom-start">
    {#snippet trigger(props)}
      <Button {...props()} variant="ghost" size="sm" square aria-label="Toggle sidebar">
        {#snippet icon()}<SidebarSimple data-slot="icon" weight="bold" />{/snippet}
      </Button>
    {/snippet}
  </Tooltip>
  <span class="text-sm font-medium text-ink">Deploys</span>
</div>
```

### Labels for a collapsed rail

React · `navigation-rail.tsx`

```tsx
import { ChartLine, Gear, House, RocketLaunch } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";

const pages = [
  { label: "Overview", icon: House },
  { label: "Deploys", icon: RocketLaunch },
  { label: "Usage", icon: ChartLine },
  { label: "Settings", icon: Gear },
];

/* a collapsed rail has no visible labels: the tooltip names each page, to the side */
export default function NavigationRail() {
  return (
    <nav aria-label="Project" className="flex flex-col gap-0.5">
      {pages.map(({ label, icon: Icon }) => (
        <Tooltip key={label} content={label} placement="right">
          <Button variant="ghost" square aria-label={label} icon={<Icon data-slot="icon" weight="bold" />} />
        </Tooltip>
      ))}
    </nav>
  );
}
```

Vue · `navigation-rail.vue`

```vue
<script setup lang="ts">
import { PhChartLine, PhGear, PhHouse, PhRocketLaunch } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";

const pages = [
  { label: "Overview", icon: PhHouse },
  { label: "Deploys", icon: PhRocketLaunch },
  { label: "Usage", icon: PhChartLine },
  { label: "Settings", icon: PhGear },
];
</script>

<!-- a collapsed rail has no visible labels: the tooltip names each page, to the side -->
<template>
  <nav aria-label="Project" class="flex flex-col gap-0.5">
    <Tooltip v-for="page in pages" :key="page.label" :content="page.label" placement="right">
      <Button variant="ghost" square :aria-label="page.label">
        <template #icon><component :is="page.icon" data-slot="icon" weight="bold" /></template>
      </Button>
    </Tooltip>
  </nav>
</template>
```

Svelte · `navigation-rail.svelte`

```svelte
<script lang="ts">
  import ChartLine from "phosphor-svelte/lib/ChartLineIcon";
  import Gear from "phosphor-svelte/lib/GearIcon";
  import House from "phosphor-svelte/lib/HouseIcon";
  import RocketLaunch from "phosphor-svelte/lib/RocketLaunchIcon";
  import { Button } from "$lib/components/ui/button";
  import { Tooltip } from "$lib/components/ui/tooltip";

  const pages = [
    { label: "Overview", icon: House },
    { label: "Deploys", icon: RocketLaunch },
    { label: "Usage", icon: ChartLine },
    { label: "Settings", icon: Gear },
  ];
</script>

<!-- a collapsed rail has no visible labels: the tooltip names each page, to the side -->
<nav aria-label="Project" class="flex flex-col gap-0.5">
  {#each pages as page (page.label)}
    <Tooltip content={page.label} placement="right">
      {#snippet trigger(props)}
        <Button {...props()} variant="ghost" square aria-label={page.label}>
          {#snippet icon()}<page.icon data-slot="icon" weight="bold" />{/snippet}
        </Button>
      {/snippet}
    </Tooltip>
  {/each}
</nav>
```

## Recipe

`tooltip.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* tooltip — dark chip that names a control. Never holds interactive content.
 * Behaviour comes from the Zag tooltip machine: opens after a delay on hover or keyboard focus,
 * and once one tooltip is open its neighbours open at once (data-instant).
 * The chip lives in a portal; its positioner stacks above popups and dialogs (base.css, --fndn-z-tooltip).
 * The shortcut hint is a plain `kbd` in the muted tooltip colour.
 *
 * Tooltips are not popups (no popup recipe): the chip has its own entrance. It grows from the trigger
 * at duration.tooltip and leaves at duration.fast; a neighbour of a tooltip that was just open
 * (data-instant) neither waits (engine) nor animates (here). Under the keyboard it is instant
 * (kbd-instant). The shortcut is the real Kbd, marked
 * data-tooltip="shortcut". */

export const tooltip = tv({
  slots: {
    /* the chip never takes pointer events, and neither does the box that places it */
    positioner: "pointer-events-none",
    base: [
      "kbd-instant",
      "relative inline-flex items-baseline gap-2 max-w-[min(280px,calc(100vw-16px))] px-2 py-1",
      "rounded-chip bg-tooltip-bg text-tooltip-fg shadow-[0_0_0_1px_var(--fndn-tooltip-border)]",
      "text-xs font-medium leading-[1.4] pointer-events-none select-none origin-(--transform-origin)",
      "motion-opacity-tooltip motion-transform-tooltip motion-also-[display_var(--fndn-duration-tooltip)_allow-discrete]",
      "starting:data-[state=open]:opacity-0 starting:data-[state=open]:scale-popup",
      "data-[state=closed]:opacity-0 data-[state=closed]:scale-popup data-[state=closed]:not-data-instant:duration-fast",
      "data-instant:duration-0",
    ],
    label: "min-w-0 wrap-anywhere",
    /* the plain kbd in the muted tooltip colour (passed to Kbd as className, so it wins the merge) */
    shortcut: "text-tooltip-muted",
  },
});

export type TooltipRecipeProps = VariantProps<typeof tooltip>;
```
