A popup that displays related information when a button receives keyboard focus or the mouse hovers over it.
Key | Description | |
---|---|---|
Tab | Opens/closes the tooltip without delay. | |
Space | If open, closes the tooltip without delay. | |
Enter | If open, closes the tooltip without delay. | |
Escape | If open, closes the tooltip without delay. |
<Text className="mt-4"> Here's the{' '} <Tooltip content="This is a tooltip"> <button className="bg-green-500">tooltip component</button> </Tooltip>{' '} in action.</Text>
Here's the in action.
Prop | Type | Default |
---|---|---|
content | string | '' |
You can follow the global installation guide that will install everything you need to use all the components or you can install the tooltip component by itself by installing its dependencies and wrap your app with the tooltip provider.
Either way, you'll need the to add 'tooltip'
to the spacing
key on the extendTailwindMerge()
function, so twMerge()
knows that the tooltip's custom class text-size-tooltip
belongs to the sizing category, so it won't conflict with color classes like text-tooltip-foreground
, bg-tooltip-background
, or text-green-500
for example.
import { type ClassValue, clsx } from 'clsx'import { extendTailwindMerge } from 'tailwind-merge'
const twMerge = extendTailwindMerge({ classGroups: { spacing: [{ 'text-size': ['tooltip'] }], },})
export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs))}
Additionally, the tooltip component's display delay is controlled globally by a tooltip provider, so you'll need to add the provider to your app's root component. A slight delay (300-500 milliseconds) is recommended to prevent accidental hover-triggered tooltips from appearing too quickly and becoming distracting.
import * as TooltipPrimitive from '@radix-ui/react-tooltip'...
function App() { return ( <html> <head> ... </head> <body> <TooltipPrimitive.Provider delayDuration={300}> <Header /> <Outlet /> <Footer /> ... </TooltipPrimitive.Provider> </body> </html> )}
~npm i @radix-ui/react-tooltip
import * as TooltipPrimitive from '@radix-ui/react-tooltip'import { type ElementRef, forwardRef, type ComponentPropsWithoutRef, useState } from 'react'import { cn } from '~/utils/tailwind-merge.ts'
type TooltipProps = ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
const Tooltip = forwardRef<ElementRef<typeof TooltipPrimitive.Content>, TooltipProps>(({ content, className, children }, ref) => { const [isOpen, setIsOpen] = useState(false)
return ( <TooltipPrimitive.Root open={isOpen} onOpenChange={setIsOpen}> <TooltipPrimitive.Trigger asChild>{children}</TooltipPrimitive.Trigger> <TooltipPrimitive.Portal /* container={container} forceMount={forceMount} */> <TooltipPrimitive.Content sideOffset={4} collisionPadding={10} ref={ref} className={cn('rounded-md bg-tooltip-background px-2 text-size-tooltip text-tooltip-foreground', className)} > {content} <TooltipPrimitive.Arrow className="fill-tooltip-background" /> </TooltipPrimitive.Content> </TooltipPrimitive.Portal> </TooltipPrimitive.Root> )})export { Tooltip }
The tooltip's styling is controlled through CSS variables.
--tooltip-color-background: var(--color-foreground);--tooltip-color-foreground: var(--color-background);--tooltip-text-size: var(--text-size-sm);