Docs
Tooltip
Tooltip
A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it.
Installation
Install the following dependencies:
npm install @radix-ui/react-tooltip
Copy and paste the following code into your project.
"use client"
import * as React from "react"
import * as TooltipPrimitive from "@radix-ui/react-tooltip"
import { cn } from "@/lib/utils"
const Tooltip = React.forwardRef<
React.ElementRef<typeof TooltipPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content> & {
text: string | React.ReactNode
}
>(({ className, sideOffset = 5, text, children, ...props }, ref) => (
<TooltipPrimitive.Provider>
<TooltipPrimitive.Root>
<TooltipPrimitive.Trigger asChild>{children}</TooltipPrimitive.Trigger>
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
ref={ref}
sideOffset={sideOffset}
className={cn(
"z-50 overflow-hidden rounded-md border border-contrast bg-contrast px-3 py-2 text-[13px] text-gray-100 shadow-md animate-in fade-in-0 zoom-in-95 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}
>
<p>{text}</p>
<TooltipPrimitive.Arrow className="fill-contrast" />
</TooltipPrimitive.Content>
</TooltipPrimitive.Portal>
</TooltipPrimitive.Root>
</TooltipPrimitive.Provider>
))
Tooltip.displayName = TooltipPrimitive.Content.displayName
export { Tooltip }
Update the import paths to match your project setup.
Usage
import { Tooltip } from "@/components/ui/tooltip"
<Tooltip text="Add to library">Hover</Tooltip>