Tooltip
A floating, intelligent label that provides additional context or information. It can be triggered by hovering or clicking, includes a visual arrow, and avoids edge collisions.
When To Use
- The Tooltip doesn't support complex text or operations. The tip is shown on mouse enter and is hidden on mouse leave.
- To provide an explanation for a button/text/operation. It's often used instead of the HTML title attribute to explain button/text/operation.
Wrapper Component
You can wrap elements with the Tooltip component and configure its content and position props.
---
import Tooltip from '@/components/data-display/Tooltip.astro';
import Button from '@/components/general/Button.astro';
---
<Tooltip content="I appear on top" position="top">
<Button variant="outline">Top</Button>
</Tooltip> Data Attributes (No Wrapper)
Because Tooltip creates a global listener, you do not need to import or wrap elements. Just add the data-tooltip attribute to any HTML element! This is perfect for inline text or dynamic content.
This is a paragraph with some inline tooltipable text that works without any wrappers.
<p>
This is a paragraph with some <span data-tooltip="This is an inline explanation!" data-tooltip-pos="top">inline tooltipable text</span>.
</p> Rich Content & Clicks
Tooltips can also be triggered on click instead of hover using trigger="click". You can also pass complex HTML content and titles using the slots (or direct props).
This is a multi-line paragraph describing the complex content that can be passed to the Tooltip component using slots.
---
import Tooltip from '@/components/data-display/Tooltip.astro';
import Button from '@/components/general/Button.astro';
import Icon from '@/components/general/Icon.astro';
---
<Tooltip trigger="click" position="bottom">
<Button variant="primary">Click Me</Button>
<div slot="title" class="flex items-center gap-2">
<Icon name="Info" class="h-4 w-4 text-blue-400" />
<span>Rich Tooltip</span>
</div>
<div slot="content" class="flex flex-col gap-2 p-1">
<p class="text-xs max-w-xs">
This is a multi-line paragraph describing the complex content that can be passed to the Tooltip component using slots.
</p>
</div>
</Tooltip> Properties Approach
Instead of using slots for complex tooltips, you can directly pass the title, description, and icon via component props for a cleaner syntax.
---
import Tooltip from '@/components/data-display/Tooltip.astro';
import Button from '@/components/general/Button.astro';
---
<Tooltip
title="Quick Tip"
description="You can define the entire tooltip layout directly using props rather than slots."
icon="Lightbulb"
position="right"
>
<Button variant="outline" >Hover for Tips</Button>
</Tooltip> API Reference
| Property | Description | Type | Default |
|---|---|---|---|
| content | Main text content to display | string | '-' |
| title | Tooltip | string | '-' |
| description | Optional muted description text below the content | string | '-' |
| icon | Optional IconName to render alongside the text | IconName | '-' |
| position | Preferred placement around the trigger | 'top' | 'bottom' | 'left' | 'right' | 'top' |
| trigger | Event that triggers the tooltip to appear | 'hover' | 'click' | 'hover' |