# Tooltip

Short description of an element, shown on hover after `appearTimeout` and right away on keyboard focus. It stays open while the pointer moves onto it and closes on Escape, on a click or when the pointer and the focus leave; on touch screens a tap on the activator toggles it. It opens on the browser's top layer (`popover`) next to the `activator`, flips and shifts to stay in the viewport, and adds its id to the activator's `aria-describedby`. `variant="plain"` is a dark one-line label, `variant="rich"` a card with a `title` and wrapping text. By default a tooltip is not interactive; with `interactive` its content can hold links, buttons or a scrolling text: it stays open while the pointer or the focus is inside it, and Tab moves into it when the Tooltip comes right after its activator in the markup. For a list of actions use `Menu`. The current side is exposed as `data-side`, the variant as `data-variant` and `interactive` as `data-interactive` on the tooltip element.

```ts
import { Tooltip } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/common/Tooltip.svelte`.

## Examples

### Labels

The default tooltip is a dark one-line label above the activator. Hover a button, or move to it with Tab: keyboard focus opens the tooltip right away. `children` replaces `text`, here to add a shortcut hint.

```svelte
<script lang="ts">
	import { mdiContentCopy, mdiDeleteOutline, mdiPencilOutline } from '@mdi/js';
	import { Button, Tooltip } from '@likable-hair/svelte';

	let copy = $state<HTMLButtonElement | HTMLAnchorElement>();
	let edit = $state<HTMLButtonElement | HTMLAnchorElement>();
	let remove = $state<HTMLButtonElement | HTMLAnchorElement>();
</script>

<div class="row">
	<Button bind:buttonElement={copy} buttonType="icon" variant="secondary" icon={mdiContentCopy} aria-label="Copy link" />
	<Tooltip activator={copy}>
		Copy link <kbd>⌘C</kbd>
	</Tooltip>

	<Button bind:buttonElement={edit} buttonType="icon" variant="secondary" icon={mdiPencilOutline} aria-label="Edit" />
	<Tooltip activator={edit} text="Edit appointment" />

	<Button bind:buttonElement={remove} buttonType="icon" variant="secondary" icon={mdiDeleteOutline} aria-label="Delete" />
	<Tooltip activator={remove} text="Delete appointment" placement="bottom" />
</div>

<style>
	.row {
		display: flex;
		gap: 8px;
	}

	kbd {
		margin-left: 4px;
		font-family: var(--global-font-family-mono);
		opacity: 0.6;
	}
</style>
```

### Rich

`variant="rich"` is a card with a bold `title` and wrapping text, for explanations longer than a label. `placement` picks the side; the tooltip flips when there is no room.

```svelte
<script lang="ts">
	import { mdiInformationOutline } from '@mdi/js';
	import { Button, Tooltip } from '@likable-hair/svelte';

	let info = $state<HTMLButtonElement | HTMLAnchorElement>();
</script>

<span class="field">
	Fidelity score
	<Button
		bind:buttonElement={info}
		buttonType="icon"
		variant="secondary"
		size="sm"
		icon={mdiInformationOutline}
		aria-label="About the fidelity score"
	/>
	<Tooltip
		activator={info}
		variant="rich"
		placement="right"
		title="Fidelity score"
		text="Based on how often the client visited and how much they spent in the last 12 months."
	/>
</span>

<style>
	.field {
		display: inline-flex;
		align-items: center;
		gap: 8px;
		font-size: 14px;
		font-weight: 500;
	}
</style>
```

### Truncated text

A common use: show the full text of a truncated cell. The tooltip lives on the top layer, so the `overflow: hidden` of the cell does not clip it. `appearTimeout` sets the hover delay.

```svelte
<script lang="ts">
	import { Tooltip } from '@likable-hair/svelte';

	const notes = [
		'Allergic to ammonia-based dyes: use the organic line only.',
		'Prefers afternoon appointments.',
		'Wants to grow the fringe out before the wedding in June, check length at every visit.'
	];
	let cells = $state<HTMLElement[]>([]);
</script>

<ul class="list">
	{#each notes as note, index (index)}
		<li>
			<span class="note" bind:this={cells[index]}>{note}</span>
			<Tooltip activator={cells[index]} text={note} appearTimeout={700} />
		</li>
	{/each}
</ul>

<style>
	.list {
		display: grid;
		grid-template-columns: minmax(0, 1fr);
		gap: 6px;
		width: min(100%, 260px);
		margin: 0;
		padding: 0;
		list-style: none;
		font-size: 14px;
	}

	.note {
		display: block;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		padding: 6px 10px;
		border-radius: var(--global-radius-sm);
		background: var(--global-color-surface-2);
	}
</style>
```

### Interactive

With `interactive` the content can hold links and buttons, like a preview card. The tooltip waits a moment before closing, so the pointer can reach it, and stays open while the keyboard focus is inside; put the Tooltip right after its activator so Tab moves into it. Escape gives the focus back to the activator.

```svelte
<script lang="ts">
	import { Button, Tooltip } from '@likable-hair/svelte';

	let client = $state<HTMLButtonElement | HTMLAnchorElement>();
</script>

<div class="note">
	Order placed by
	<Button bind:buttonElement={client} buttonType="text" size="sm">Mario Rossi</Button>
	<Tooltip activator={client} interactive variant="rich" placement="bottom" title="Mario Rossi">
		<span class="card">
			<span>Client since 2019 · 42 orders · Milan</span>
			<a href="#client">Open the client</a>
		</span>
	</Tooltip>
	on 24 September.
</div>

<style>
	.note {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 4px;
		margin: 0;
		font-size: 14px;
	}

	.card {
		display: grid;
		gap: 8px;
	}

	.card a {
		color: var(--global-color-primary);
		font-weight: 500;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `activator` (bindable) | `HTMLElement` |  | Element the tooltip describes and opens from. |
| `open` (bindable) | `boolean` |  | Whether the tooltip is open. It opens and closes by itself; bind it to open it from code. |
| `text` | `string` |  | Text of the tooltip. |
| `title` | `string` |  | Bold first line, mostly for `variant="rich"`. |
| `variant` | `'plain' \| 'rich'` | `'plain'` | `plain` a dark one-line label, `rich` a card with a title and wrapping text. |
| `placement` | `'top' \| 'top-start' \| 'top-end' \| 'bottom' \| 'bottom-start' \| 'bottom-end' \| 'left' \| 'left-start' \| 'left-end' \| 'right' \| 'right-start' \| 'right-end'` | `'top'` | Side of the activator and alignment. Flips to the opposite side when there is not enough space. |
| `offset` | `number` | `10` | Distance from the activator, in pixels, arrow included. |
| `appearTimeout` | `number` | `400` | Delay before the tooltip opens on hover, in milliseconds. Keyboard focus opens it right away, and so does hovering another tooltip's activator just after one closed. |
| `interactive` | `boolean` | `false` | Lets the content hold links and buttons: the tooltip becomes a non-modal `dialog`, waits longer before closing when the pointer leaves, stays open while the keyboard focus is inside it, and Escape gives the focus back to the activator. |
| `tooltipElement` (bindable) | `HTMLDivElement` |  | The tooltip element. |
| `class` | `string` |  | Extra classes on the tooltip element. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `children` |  | Replaces `text`. Keep it non-interactive unless `interactive` is set. |
| `titleSnippet` | `{ title: string \| undefined }` | Replaces the title. |

## CSS variables

For a single instance, pass them as style props: `<Tooltip --tooltip-background="tomato">`. The `--tooltip-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--tooltip-border-width` | `--tooltip-default-border-width`: `0px`<br>`--tooltip-default-rich-border-width`: `var(--global-border-width)` |
| `--tooltip-border-color` | `--tooltip-default-border-color`: `transparent`<br>`--tooltip-default-rich-border-color`: `var(--global-color-border-strong)` |
| `--tooltip-arrow-size` | `--tooltip-default-arrow-size`: `8px` |
| `--tooltip-duration` | `--tooltip-default-duration`: `var(--global-duration)` |
| `--tooltip-transition-distance` | `--tooltip-default-transition-distance`: `4px` |
| `--tooltip-max-width` | `--tooltip-default-max-width`: `280px`<br>`--tooltip-default-rich-max-width`: `240px` |
| `--tooltip-padding` | `--tooltip-default-padding`: `6px 10px`<br>`--tooltip-default-rich-padding`: `12px` |
| `--tooltip-background` | `--tooltip-default-background`: `var(--global-color-text)`<br>`--tooltip-default-rich-background`: `var(--global-color-surface-pop)` |
| `--tooltip-color` | `--tooltip-default-color`: `var(--global-color-bg)`<br>`--tooltip-default-rich-color`: `var(--global-color-text)` |
| `--tooltip-border-radius` | `--tooltip-default-border-radius`: `var(--global-radius-sm)` |
| `--tooltip-box-shadow` | `--tooltip-default-box-shadow`: `var(--global-shadow-md)` |
| `--tooltip-font-size` | `--tooltip-default-font-size`: `12px` |
| `--tooltip-font-weight` | `--tooltip-default-font-weight`: `500`<br>`--tooltip-default-rich-font-weight`: `400` |
| `--tooltip-line-height` | `--tooltip-default-line-height`: `1.4` |
| `--tooltip-title-font-weight` | `--tooltip-default-title-font-weight`: `600` |
| `--tooltip-text-color` | `--tooltip-default-text-color`: `inherit`<br>`--tooltip-default-rich-text-color`: `var(--global-color-text-3)` |
