# Tooltip

> A visual hint that labels a control on hover or keyboard focus.

```tsx title="hero.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Separator } from '@langgenius/dify-ui/separator'
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@langgenius/dify-ui/tooltip'

const actions = [
  { icon: 'i-ri-edit-line', label: 'Edit app info' },
  { icon: 'i-ri-file-copy-2-line', label: 'Duplicate' },
  { icon: 'i-ri-download-2-line', label: 'Export DSL' },
] as const

export function TooltipHeroDemo() {
  return (
    <TooltipProvider>
      <div className="flex items-center gap-1.5">
        {actions.map(({ icon, label }) => (
          <Tooltip key={label}>
            <TooltipTrigger
              render={
                <IconButton aria-label={label} size="lg" variant="secondary">
                  <span aria-hidden="true" className={`${icon} size-4`} />
                </IconButton>
              }
            />
            <TooltipContent>{label}</TooltipContent>
          </Tooltip>
        ))}
        <Separator orientation="vertical" className="mx-0.5 h-4" />
        <Tooltip>
          <TooltipTrigger
            render={
              <IconButton aria-label="Delete" size="lg" variant="secondary" tone="destructive">
                <span aria-hidden="true" className="i-ri-delete-bin-line size-4" />
              </IconButton>
            }
          />
          <TooltipContent>Delete</TooltipContent>
        </Tooltip>
      </div>
    </TooltipProvider>
  )
}
```

## Usage guidelines

- Use `Tooltip` for a supplementary visual label on a control that already has a primary action and an accessible name. When the trigger's only job is to open an explanation, use `Infotip`.
- Tooltip content is not the trigger's accessible name. An icon-only trigger such as `IconButton` keeps its own `aria-label`, and a trigger with visible text keeps that text as its name.
- Do not put anything users must reach in a tooltip. Touch devices do not open it and assistive technology does not read it, so essential, structured or interactive content goes in a `Popover` or an `Infotip`.
- Text length alone does not decide between `Tooltip` and `Infotip`. Decide by what the trigger does and whether the content must be reachable.
- `TooltipProvider` shares open and close delays, so moving between neighboring triggers opens the next tooltip without waiting again. Wrap the application in one provider at its root; feature code composes `Tooltip` without adding its own.
- Compose the trigger through `render` so the control keeps its own component, such as `IconButton` or `Button`.

## Anatomy

```tsx
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@langgenius/dify-ui/tooltip'
```

```tsx
<TooltipProvider>
  <Tooltip>
    <TooltipTrigger render={<IconButton aria-label="Label" />} />
    <TooltipContent />
  </Tooltip>
</TooltipProvider>
```

`TooltipContent` bundles the portal, positioner and styled popup. `side`, `align`, `sideOffset` and `alignOffset` place it against the trigger.

## Examples

### Keyboard shortcut

A tooltip can show the control's shortcut next to its label with `Kbd`. The label in the tooltip repeats the trigger's `aria-label`. Assistive technology does not read the tooltip, so a trigger whose shortcut the feature handles also sets `aria-keyshortcuts`, such as `Meta+B`. This example only displays the keys and binds no shortcut, so it leaves `aria-keyshortcuts` off.

```tsx title="shortcut.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Kbd, KbdGroup } from '@langgenius/dify-ui/kbd'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'

export function TooltipShortcutDemo() {
  return (
    <Tooltip>
      <TooltipTrigger
        render={
          <IconButton aria-label="Collapse sidebar" size="lg" variant="secondary">
            <span aria-hidden="true" className="i-ri-layout-left-2-line size-4" />
          </IconButton>
        }
      />
      <TooltipContent className="flex items-center gap-1">
        <span className="px-0.5">Collapse sidebar</span>
        <KbdGroup>
          <Kbd>⌘</Kbd>
          <Kbd>B</Kbd>
        </KbdGroup>
      </TooltipContent>
    </Tooltip>
  )
}
```

### Text trigger

A button with visible text keeps that text as its name, and the tooltip adds a detail the user can afford to miss.

```tsx title="text-trigger.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'

export function TooltipTextTriggerDemo() {
  return (
    <Tooltip>
      <TooltipTrigger render={<Button variant="primary" />}>Publish</TooltipTrigger>
      <TooltipContent side="bottom">Last published 2 hours ago</TooltipContent>
    </Tooltip>
  )
}
```

### Disabled trigger

A natively disabled button cannot receive keyboard focus. Pair `disabled` with `focusableWhenDisabled` on `Button` so both pointer and keyboard users can inspect why the action is unavailable.

```tsx title="disabled-trigger.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'

export function TooltipDisabledTriggerDemo() {
  return (
    <Tooltip>
      <TooltipTrigger render={<Button variant="primary" disabled focusableWhenDisabled />}>
        Publish
      </TooltipTrigger>
      <TooltipContent>Add at least one block before publishing</TooltipContent>
    </Tooltip>
  )
}
```

## Keyboard

| Key      | Action                                               |
| -------- | ---------------------------------------------------- |
| `Tab`    | Moves focus to the trigger and opens its tooltip.    |
| `Escape` | Closes the tooltip while focus stays on the trigger. |

## API reference

### Tooltip

Groups a trigger and floating tooltip popup. Doesn't render its own HTML element.

Wrap the application in `TooltipProvider` to share open and close delays across tooltips.

Inherits `defaultOpen`, `open`, `onOpenChange`, `onOpenChangeComplete`, `disableHoverablePopup`, `trackCursorAxis`, `actionsRef`, `disabled`, `children`, `triggerId`, `defaultTriggerId` from [Base UI Tooltip.Root](https://base-ui.com/react/components/tooltip#root).

### TooltipContent

A styled tooltip popup bundled with a portal and positioner. Renders a `<div>` element.

**Props**

| Prop          | Type                       | Default | Description                                                                                             |
| ------------- | -------------------------- | ------- | ------------------------------------------------------------------------------------------------------- |
| `sideOffset?` | `number \| OffsetFunction` | `8`     | Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance. |
| `children`    | `React.ReactNode`          | —       | The tooltip content.                                                                                    |

Inherits `className`, `render`, `style`, `align`, `alignOffset`, `side` from [Base UI Tooltip.Popup](https://base-ui.com/react/components/tooltip#popup).

**Data attributes**

| Attribute             | Description                                                           |
| --------------------- | --------------------------------------------------------------------- |
| `data-open`           | Present when the tooltip is open.                                     |
| `data-closed`         | Present when the tooltip is closed.                                   |
| `data-starting-style` | Present when the tooltip begins animating in.                         |
| `data-ending-style`   | Present when the tooltip is animating out.                            |
| `data-side`           | Indicates which side the popup is positioned relative to the trigger. |
| `data-align`          | Indicates how the popup is aligned relative to specified side.        |
| `data-instant`        | Present if animations should be instant.                              |

**CSS variables**

| Variable              | Type     | Description                                                                                                                   |
| --------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `--available-width`   | `number` | The available width between the trigger and the edge of the viewport.                                                         |
| `--available-height`  | `number` | The available height between the trigger and the edge of the viewport.                                                        |
| `--anchor-width`      | `number` | The anchor's width.                                                                                                           |
| `--anchor-height`     | `number` | The anchor's height.                                                                                                          |
| `--transform-origin`  | `string` | The coordinates that this element is anchored to. Used for animations and transitions.                                        |
| `--positioner-width`  | `number` | The width of the tooltip's positioner. It is important to set `width` to this value when using CSS to animate size changes.   |
| `--positioner-height` | `number` | The height of the tooltip's positioner. It is important to set `height` to this value when using CSS to animate size changes. |

### TooltipProvider

Provides a shared delay for multiple tooltips. The grouping logic ensures that once a tooltip becomes visible, the adjacent tooltips will be shown instantly.

Inherits `children`, `delay`, `closeDelay`, `timeout` from [Base UI Tooltip.Provider](https://base-ui.com/react/components/tooltip#provider).

### TooltipTrigger

An element to attach the tooltip to. Renders a `<button>` element.

Inherits `className`, `payload`, `delay`, `closeOnClick`, `closeDelay`, `disabled`, `render`, `style` from [Base UI Tooltip.Trigger](https://base-ui.com/react/components/tooltip#trigger).

**Data attributes**

| Attribute               | Description                                                                                                    |
| ----------------------- | -------------------------------------------------------------------------------------------------------------- |
| `data-popup-open`       | Present when the corresponding tooltip is open.                                                                |
| `data-trigger-disabled` | Present when the trigger is disabled, either by the `disabled` prop or by a parent `<Tooltip.Root>` component. |
