# Tooltip

A short label shown on hover and keyboard focus.

Docs: https://corsairui.vercel.app/docs/tooltip

## Install

```bash
pnpm dlx shadcn@latest add @corsair-ui/tooltip
```

Also adds: `utils`.
npm: `@radix-ui/react-tooltip`, `clsx`, `tailwind-merge`.

## API

Built on Radix Tooltip (https://www.radix-ui.com/primitives/docs/components/tooltip); its props pass through.

## Styling

Restyle it in the project that installs it: `className` on each part, the theme variables, and these attributes with arbitrary variants (`[&_[data-slot=…]]:…`, `data-[state=…]:…`).

data-slot: `tooltip`, `tooltip-content`, `tooltip-provider`, `tooltip-trigger`

## Accessibility

- Opens on hover and on keyboard focus; Escape closes it.
- It adds to the trigger's name: icon-only buttons still need an aria-label.

## Examples

On the docs page: Default, Sides. The usage examples in the source comments below are the reference.

## Source

What `shadcn add` writes into the project:

### tooltip.tsx

```tsx
"use client";

import * as TooltipPrimitive from "@radix-ui/react-tooltip";
import type { ComponentProps } from "react";

import { cn } from "@/registry/default/lib/utils";

/** Shares open and close delays between tooltips. Tooltip already includes one. */
function TooltipProvider({
  delayDuration = 200,
  ...props
}: ComponentProps<typeof TooltipPrimitive.Provider>) {
  return (
    <TooltipPrimitive.Provider
      data-slot="tooltip-provider"
      delayDuration={delayDuration}
      {...props}
    />
  );
}

/**
 * A short label shown on hover and keyboard focus. It supplements the
 * trigger's own name: an icon-only button still needs an `aria-label`.
 */
function Tooltip(props: ComponentProps<typeof TooltipPrimitive.Root>) {
  return (
    <TooltipProvider>
      <TooltipPrimitive.Root data-slot="tooltip" {...props} />
    </TooltipProvider>
  );
}

function TooltipTrigger(props: ComponentProps<typeof TooltipPrimitive.Trigger>) {
  return <TooltipPrimitive.Trigger data-slot="tooltip-trigger" {...props} />;
}

function TooltipContent({
  className,
  sideOffset = 4,
  children,
  ...props
}: ComponentProps<typeof TooltipPrimitive.Content>) {
  return (
    <TooltipPrimitive.Portal>
      <TooltipPrimitive.Content
        data-slot="tooltip-content"
        sideOffset={sideOffset}
        className={cn(
          "bg-foreground text-background z-50 w-fit origin-[var(--radix-tooltip-content-transform-origin)] rounded-md px-3 py-1.5 text-xs text-balance",
          // animate-in/out sit behind motion-safe: so reduced motion really turns them off.
          "fade-in-0 zoom-in-95 motion-safe:animate-in",
          "motion-safe:data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95",
          "data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
          className
        )}
        {...props}
      >
        {children}
        <TooltipPrimitive.Arrow className="bg-foreground fill-foreground z-50 size-2.5 translate-y-[calc(-50%_-_2px)] rotate-45 rounded-[2px]" />
      </TooltipPrimitive.Content>
    </TooltipPrimitive.Portal>
  );
}

export { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger };
```
