# Tooltip
A short hover or focus label for an already-named control.
Source page: https://dinachi.dev/docs/components/tooltip

## Installation
```bash
npx @dinachi/cli@latest add tooltip
```
Adds to your project:
- `components/ui/tooltip/tooltip.tsx`
- `components/ui/tooltip/index.ts`
Installs:
- `@base-ui/react` ^1.9.0

## Usage
```tsx
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
import { Button } from "@/components/ui/button"
import { Plus } from "lucide-react"

export function AddButton() {
  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger render={<Button variant="outline" size="icon" aria-label="Add item" />}>
          <Plus className="h-4 w-4" />
        </TooltipTrigger>
        <TooltipContent>Add item</TooltipContent>
      </Tooltip>
    </TooltipProvider>
  )
}
```

## Anatomy
```tsx
<TooltipProvider>
  <Tooltip>
    <TooltipTrigger />
    <TooltipContent />
  </Tooltip>
</TooltipProvider>
```
- `Tooltip`: Open state for one tooltip. No element.
- `TooltipTrigger`: The described element. Unstyled; style via `render`.
- `TooltipContent`: Portal, positioner, popup and arrow in one.
- `TooltipProvider`: Shares delays across tooltips. Optional.
- `TooltipPortal`: Portals into the body, for custom content.
- `TooltipPositioner`: Positions the popup, for custom content.
- `TooltipPopup`: The visible box.
- `TooltipArrow`: The pointer from the popup to the trigger.
- `TooltipArrowSvg`: Default arrow shape, for custom arrows.

## Examples

### Default
A tooltip on an icon button.
```tsx
import {
  TooltipProvider,
  Tooltip,
  TooltipTrigger,
  TooltipContent,
} from '@/components/ui/tooltip';
import { Plus } from 'lucide-react';

export function Example() {
  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger>
          <Plus className="h-4 w-4" />
        </TooltipTrigger>
        <TooltipContent>
          Add new item
        </TooltipContent>
      </Tooltip>
    </TooltipProvider>
  );
}
```

### Positions
Top, bottom, left and right.
```tsx
import {
  TooltipProvider,
  Tooltip,
  TooltipTrigger,
  TooltipContent,
} from '@/components/ui/tooltip';
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <TooltipProvider>
      <div className="flex gap-4 flex-wrap">
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" />}>Top</TooltipTrigger>
          <TooltipContent side="top">Tooltip on top</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" />}>Bottom</TooltipTrigger>
          <TooltipContent side="bottom">Tooltip on bottom</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" />}>Left</TooltipTrigger>
          <TooltipContent side="left">Tooltip on left</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" />}>Right</TooltipTrigger>
          <TooltipContent side="right">Tooltip on right</TooltipContent>
        </Tooltip>
      </div>
    </TooltipProvider>
  );
}
```

### Variants
Default and inverse.
```tsx
import {
  TooltipProvider,
  Tooltip,
  TooltipTrigger,
  TooltipContent,
} from '@/components/ui/tooltip';
import { Button } from '@/components/ui/button';
import { Settings, Trash2 } from 'lucide-react';

export function Example() {
  return (
    <TooltipProvider>
      <div className="flex gap-4">
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" size="icon" />}>
            <Settings className="h-4 w-4" />
          </TooltipTrigger>
          <TooltipContent variant="default">Default tooltip</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" size="icon" />}>
            <Trash2 className="h-4 w-4" />
          </TooltipTrigger>
          <TooltipContent variant="inverse">Inverse tooltip</TooltipContent>
        </Tooltip>
      </div>
    </TooltipProvider>
  );
}
```

### Close on click
Whether clicking the trigger dismisses the tooltip.
```tsx
import {
  TooltipProvider,
  Tooltip,
  TooltipTrigger,
  TooltipContent,
} from '@/components/ui/tooltip';
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <TooltipProvider>
      <div className="flex gap-4">
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" />} closeOnClick={true}>
            Dismisses on click
          </TooltipTrigger>
          <TooltipContent>This tooltip closes when you click the trigger (default)</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger render={<Button variant="outline" />} closeOnClick={false}>
            Persists on click
          </TooltipTrigger>
          <TooltipContent>This tooltip stays open when you click the trigger</TooltipContent>
        </Tooltip>
      </div>
    </TooltipProvider>
  );
}
```

## Notes

### Custom content
`TooltipContent` passes `side`, `align` and `sideOffset` to the positioner and the rest to the popup. For other positioner options, such as collision handling, compose `TooltipPortal`, `TooltipPositioner`, `TooltipPopup` and `TooltipArrow`.

### Supplementary content only
It shows only on hover or focus. Put nothing in it needed to finish the task, and nothing interactive.

## API reference

### Tooltip
Open state for one tooltip. No element.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | - | Controlled open state. |
| `defaultOpen` | `boolean` | `false` | Starts open when uncontrolled. |
| `onOpenChange` | `(open, details) => void` | - | Runs when the tooltip opens or closes. |
| `disabled` | `boolean` | `false` | Stops the tooltip from opening. |
| `disableHoverablePopup` | `boolean` | `false` | Closes when the pointer leaves the trigger, even onto the popup. |
| `trackCursorAxis` | `"none" \| "x" \| "y" \| "both"` | `"none"` | Follows the cursor along an axis. |

### TooltipTrigger
The described element. Unstyled; style via `render`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `ReactElement` | - | Your own element, such as a Button, with trigger behaviour. |
| `delay` | `number` | `600` | Milliseconds of hover before it opens. |
| `closeDelay` | `number` | `0` | Milliseconds before it closes once the pointer leaves. |
| `closeOnClick` | `boolean` | `true` | Closes the tooltip when the trigger is clicked. |
| `disabled` | `boolean` | `false` | Stops this trigger opening the tooltip. The element stays enabled. |

### TooltipContent
Portal, positioner, popup and arrow in one.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `side` | `"top" \| "right" \| "bottom" \| "left"` | `"top"` | Preferred side of the trigger. |
| `align` | `"start" \| "center" \| "end"` | `"center"` | Alignment along that side. |
| `sideOffset` | `number` | `4` | Gap to the trigger, in pixels. |
| `showArrow` | `boolean` | `true` | Draws the arrow pointing at the trigger. |
| `variant` | `"default" \| "inverse"` | `"default"` | Inverse uses the primary colour for more contrast. |

### TooltipProvider
Shares delays across tooltips. Optional.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `delay` | `number` | - | Open delay for every trigger inside. |
| `closeDelay` | `number` | - | Close delay for every tooltip inside. |
| `timeout` | `number` | `400` | After one tooltip closes, another opens instantly within this many milliseconds. |

### TooltipPortal
Portals into the body, for custom content.

Takes the props of the element it renders.

### TooltipPositioner
Positions the popup, for custom content.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `sideOffset` | `number` | `4` | Gap to the trigger, in pixels. |
| `side` | `"top" \| "right" \| "bottom" \| "left"` | `"top"` | Preferred side of the trigger. |

### TooltipPopup
The visible box.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"default" \| "inverse"` | `"default"` | Colour scheme. |

### TooltipArrow
The pointer from the popup to the trigger.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"default" \| "inverse"` | `"default"` | Match the popup's variant. |
| `children` | `ReactNode` | `<TooltipArrowSvg />` | Replaces the arrow shape. |

### TooltipArrowSvg
Default arrow shape, for custom arrows.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"default" \| "inverse"` | `"default"` | Fill colours. |

## Accessibility
- No aria-describedby link, so screen readers skip the popup. Name the trigger.
- An icon-only trigger needs an aria-label; it can repeat the tooltip text.
- Opening on keyboard focus skips the animation.
- Reduced motion drops the scale; only the fade remains.
### Keyboard
| Keys | Does |
| --- | --- |
| `Tab` | Focusing the trigger opens the tooltip |
| `Esc` | Closes the tooltip |

## Source
`components/ui/tooltip/tooltip.tsx`
```tsx
"use client";

import * as React from "react";
import { Tooltip as BaseTooltip } from "@base-ui/react/tooltip";
import { cn } from "@/lib/utils";

// Provider Component
const TooltipProvider: typeof BaseTooltip.Provider = BaseTooltip.Provider;

// Root Component
const Tooltip: typeof BaseTooltip.Root = BaseTooltip.Root;

// Trigger Component — a simple pass-through. Style via `render` prop.
const TooltipTrigger = React.forwardRef<
  HTMLButtonElement,
  React.ComponentProps<typeof BaseTooltip.Trigger>
>(({ className, ...props }, ref) => (
  <BaseTooltip.Trigger ref={ref} className={className} data-slot="tooltip-trigger" {...props} />
));
TooltipTrigger.displayName = "TooltipTrigger";

// Portal Component
function TooltipPortal(props: React.ComponentProps<typeof BaseTooltip.Portal>): React.JSX.Element {
  return <BaseTooltip.Portal data-slot="tooltip-portal" {...props} />;
}

// Positioner Component
const TooltipPositioner: React.ForwardRefExoticComponent<
  React.ComponentProps<typeof BaseTooltip.Positioner> &
    React.RefAttributes<React.ComponentRef<typeof BaseTooltip.Positioner>>
> = React.forwardRef<
  React.ComponentRef<typeof BaseTooltip.Positioner>,
  React.ComponentProps<typeof BaseTooltip.Positioner>
>(({ className, sideOffset = 4, ...props }, ref) => (
  <BaseTooltip.Positioner
    ref={ref}
    sideOffset={sideOffset}
    className={cn("z-50", className)}
    data-slot="tooltip-positioner"
    {...props}
  />
));
TooltipPositioner.displayName = "TooltipPositioner";

// Popup Component with variant support
interface TooltipPopupProps
  extends React.ComponentProps<typeof BaseTooltip.Popup> {
  variant?: "default" | "inverse";
}

const TooltipPopup: React.ForwardRefExoticComponent<
  TooltipPopupProps & React.RefAttributes<React.ComponentRef<typeof BaseTooltip.Popup>>
> = React.forwardRef<
  React.ComponentRef<typeof BaseTooltip.Popup>,
  TooltipPopupProps
>(({ className, variant = "default", ...props }, ref) => {
  const variantStyles = {
    default: "bg-popover text-popover-foreground border border-border",
    inverse: "bg-primary text-primary-foreground",
  };

  return (
    <BaseTooltip.Popup
      ref={ref}
      className={cn(
        "origin-[var(--transform-origin)] rounded-md px-3 py-1.5 text-sm shadow-md",
        "transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
        "data-[ending-style]:scale-95 data-[ending-style]:opacity-0",
        "data-[starting-style]:scale-95 data-[starting-style]:opacity-0",
        "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
        // Base UI sets data-instant when the open came from an adjacent trigger while a
        // tooltip was already up, from keyboard focus, or from Escape. All three are
        // cases where the animation is the only thing standing between the user and the
        // content they asked for.
        "data-[instant]:duration-0",
        variantStyles[variant],
        className
      )}
      data-slot="tooltip-popup"
      {...props}
    />
  );
});
TooltipPopup.displayName = "TooltipPopup";

// Arrow Component with custom SVG
type TooltipArrowProps = React.ComponentProps<typeof BaseTooltip.Arrow> & {
  children?: React.ReactNode;
  variant?: "default" | "inverse";
};

const TooltipArrow: React.ForwardRefExoticComponent<
  TooltipArrowProps & React.RefAttributes<React.ComponentRef<typeof BaseTooltip.Arrow>>
> = React.forwardRef<
  React.ComponentRef<typeof BaseTooltip.Arrow>,
  TooltipArrowProps
>(({ className, children, variant = "default", ...props }, ref) => (
  <BaseTooltip.Arrow
    ref={ref}
    className={cn(
      "data-[side=bottom]:top-[-8px] data-[side=left]:right-[-13px] data-[side=left]:rotate-90",
      "data-[side=right]:left-[-13px] data-[side=right]:-rotate-90",
      "data-[side=top]:bottom-[-8px] data-[side=top]:rotate-180",
      className
    )}
    data-slot="tooltip-arrow"
    {...props}
  >
    {children || <TooltipArrowSvg variant={variant} />}
  </BaseTooltip.Arrow>
));
TooltipArrow.displayName = "TooltipArrow";

// Combined Content Component for easier usage
interface TooltipContentProps extends TooltipPopupProps {
  sideOffset?: number;
  side?: "top" | "right" | "bottom" | "left";
  align?: "start" | "center" | "end";
  showArrow?: boolean;
}

const TooltipContent: React.ForwardRefExoticComponent<
  TooltipContentProps & React.RefAttributes<React.ComponentRef<typeof BaseTooltip.Popup>>
> = React.forwardRef<
  React.ComponentRef<typeof BaseTooltip.Popup>,
  TooltipContentProps
>(
  (
    {
      className,
      children,
      sideOffset = 4,
      side = "top",
      align = "center",
      showArrow = true,
      variant = "default",
      ...props
    },
    ref
  ) => (
    <TooltipPortal>
      <TooltipPositioner sideOffset={sideOffset} side={side} align={align}>
        <TooltipPopup
          ref={ref}
          className={className}
          variant={variant}
          data-slot="tooltip-content"
          {...props}
        >
          {showArrow && <TooltipArrow variant={variant} />}
          {children}
        </TooltipPopup>
      </TooltipPositioner>
    </TooltipPortal>
  )
);
TooltipContent.displayName = "TooltipContent";

// Optimized Arrow SVG Component
type TooltipArrowSvgProps = React.ComponentProps<"svg"> & {
  variant?: "default" | "inverse";
};

const TooltipArrowSvg: React.MemoExoticComponent<
  React.FC<TooltipArrowSvgProps>
> = React.memo(({ variant = "default", ...props }: TooltipArrowSvgProps) => {
    const fillClasses = variant === "inverse" ? "fill-primary" : "fill-popover";

    const borderClasses =
      variant === "inverse" ? "fill-primary" : "fill-border";

    return (
      <svg width="20" height="10" viewBox="0 0 20 10" fill="none" data-slot="tooltip-arrow-svg" {...props}>
        <path
          d="M9.66437 2.60207L4.80758 6.97318C4.07308 7.63423 3.11989 8 2.13172 8H0V10H20V8H18.5349C17.5468 8 16.5936 7.63423 15.8591 6.97318L11.0023 2.60207C10.622 2.2598 10.0447 2.25979 9.66437 2.60207Z"
          className={fillClasses}
        />
        <path
          d="M10.3333 3.34539L5.47654 7.71648C4.55842 8.54279 3.36693 9 2.13172 9H0V8H2.13172C3.11989 8 4.07308 7.63423 4.80758 6.97318L9.66437 2.60207C10.0447 2.25979 10.622 2.2598 11.0023 2.60207L15.8591 6.97318C16.5936 7.63423 17.5468 8 18.5349 8H20V9H18.5349C17.2998 9 16.1083 8.54278 15.1901 7.71648L10.3333 3.34539Z"
          className={borderClasses}
        />
      </svg>
    );
  }
);
TooltipArrowSvg.displayName = "TooltipArrowSvg";

export {
  TooltipProvider,
  Tooltip,
  TooltipTrigger,
  TooltipPortal,
  TooltipPositioner,
  TooltipPopup,
  TooltipArrow,
  TooltipContent,
  TooltipArrowSvg,
};
```
