Tooltip

Web/Mobile

A popup that displays information related to an element when it receives keyboard focus or hover.

Installation

pnpm dlx cubix-ui@latest add tooltip

Add the Provider

Place TooltipProvider near the root of your app so tooltips share one open delay. It is optional: without it every tooltip opens immediately.

app/layout.tsx
import type { ReactNode } from "react"

import { TooltipProvider } from "@/components/cubix/tooltip"

export default function RootLayout({ children }: { children: ReactNode }) {
  return (
    <html lang="fa" dir="rtl">
      <body>
        <TooltipProvider>{children}</TooltipProvider>
      </body>
    </html>
  )
}

Usage

Import
import { Button } from "@/components/cubix/button"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/cubix/tooltip"
Example
<Tooltip>
  <TooltipTrigger render={<Button variant="outline" size="sm" />}>نگه دارید</TooltipTrigger>
  <TooltipContent>افزودن به کتابخانه</TooltipContent>
</Tooltip>

Composition

The tooltip opens on hover and on keyboard focus, and closes on Escape. It starts right-to-left and follows the closest dir on the page; pass dir to Tooltip to override it.

Structure
TooltipProvider (optional)
└── Tooltip
    ├── TooltipTrigger
    └── TooltipContent

Examples

Side

Use side to choose where the tooltip appears. Prefer the logical values inline-start and inline-end: in RTL, inline-start is the right side of the trigger.

Icon button

Give an icon-only trigger an aria-label so it has an accessible name for screen readers.

With keyboard shortcut

Put a Cubix Kbd inside the content to show the shortcut for an action.

Disabled button

A disabled button can't be hovered or focused, so render the trigger as a span with tabIndex={0}. The tooltip then opens on hover and on keyboard focus.

Delay

Pass delay to a single Tooltip, or to TooltipProvider to set a shared open delay for the tree.

API Reference

TooltipProvider

Optional. Shares one open delay between every tooltip in the tree.

PropTypeDefaultDescription
delaynumber0Default delay in milliseconds before any tooltip in the tree opens. Without a provider, tooltips open immediately.
childrenReact.ReactNode-Tooltip trees that share this delay setting.

Tooltip

The container that wraps the trigger and content and manages open state.

PropTypeDefaultDescription
dir"ltr" | "rtl"closest page dir, else rtlText direction of the trigger and tooltip. Defaults to the closest dir on the page, and to right-to-left when there is none.
openboolean-Controlled open state. Omit to use uncontrolled mode.
defaultOpenbooleanfalseInitial open state when uncontrolled.
onOpenChange(open: boolean) => void-Called when the open state changes.
delaynumberTooltipProvider delay, else 0Delay in milliseconds before this tooltip opens. Overrides TooltipProvider.
childrenReact.ReactNode-The trigger and content elements.

TooltipTrigger

The element that opens the tooltip on hover and focus. Compose it with Cubix Button via render.

PropTypeDefaultDescription
renderReact.ReactElement-Render the trigger as another element (for example a Button).
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).

TooltipContent

The popup rendered in a portal, positioned relative to the trigger.

PropTypeDefaultDescription
side"top" | "right" | "bottom" | "left" | "inline-start" | "inline-end""top"Preferred side of the trigger to render the tooltip on.
sideOffsetnumber4Distance in pixels between the trigger and the tooltip.
align"start" | "center" | "end""center"Alignment of the tooltip relative to the trigger.
alignOffsetnumber0Offset in pixels along the alignment axis.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).