Tooltip
<script setup lang="ts">
import { TooltipArrow, TooltipContent, TooltipPortal, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
import { Icon } from '@iconify/vue'
</script>
<template>
<TooltipProvider>
<TooltipRoot>
<TooltipTrigger
class="text-grass11 shadow-blackA7 hover:bg-green3 inline-flex h-[35px] w-[35px] items-center justify-center rounded-full bg-white shadow-[0_2px_10px] outline-none focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon icon="radix-icons:plus" />
</TooltipTrigger>
<TooltipPortal>
<TooltipContent
class="data-[state=delayed-open]:data-[side=top]:animate-slideDownAndFade data-[state=delayed-open]:data-[side=right]:animate-slideLeftAndFade data-[state=delayed-open]:data-[side=left]:animate-slideRightAndFade data-[state=delayed-open]:data-[side=bottom]:animate-slideUpAndFade text-grass11 select-none rounded-[4px] bg-white px-[15px] py-[10px] text-[15px] leading-none shadow-[hsl(206_22%_7%_/_35%)_0px_10px_38px_-10px,_hsl(206_22%_7%_/_20%)_0px_10px_20px_-15px] will-change-[transform,opacity]"
:side-offset="5"
>
Add to library
<TooltipArrow
class="fill-white"
:width="8"
/>
</TooltipContent>
</TooltipPortal>
</TooltipRoot>
</TooltipProvider>
</template>Features
- Provider to control display delay globally.
- Opens when the trigger is focused or hovered.
- Closes when the trigger is activated or when pressing escape.
- Supports custom timings.
Anatomy
Import all parts and piece them together.
<script setup lang="ts">
import { TooltipArrow, TooltipContent, TooltipPortal, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
</script>
<template>
<TooltipProvider>
<TooltipRoot>
<TooltipTrigger />
<TooltipPortal>
<TooltipContent>
<TooltipArrow />
</TooltipContent>
</TooltipPortal>
</TooltipRoot>
</TooltipProvider>
</template>API Reference
Provider
Wraps your app to provide global functionality to your tooltips.
| Prop | Default | Type |
|---|---|---|
delayDuration | 700 | numberThe duration from when the pointer enters the trigger until the tooltip gets opened. |
disableClosingTrigger | booleanWhen | |
disabled | booleanWhen | |
disableHoverableContent | false | booleanWhen |
ignoreNonKeyboardFocus | false | booleanPrevent the tooltip from opening if the focus did not come from
the keyboard by matching against the |
skipDelayDuration | 300 | numberHow much time a user has to enter another trigger without incurring a delay again. |
Root
Contains all the parts of a tooltip.
| Prop | Default | Type |
|---|---|---|
defaultOpen | false | booleanThe open state of the tooltip when it is initially rendered. Use when you do not need to control its open state. |
delayDuration | numberOverride the duration given to the | |
disableClosingTrigger | booleanWhen | |
disabled | booleanWhen | |
disableHoverableContent | booleanPrevents Tooltip.Content from remaining open when hovering. Disabling this has accessibility consequences. Inherits from Tooltip.Provider. | |
ignoreNonKeyboardFocus | booleanPrevent the tooltip from opening if the focus did not come from
the keyboard by matching against the | |
open | booleanThe controlled open state of the tooltip. |
| Emit | Payload |
|---|---|
update:open | [value: boolean]Event handler called when the open state of the tooltip changes. |
| Slots (default) | Payload |
|---|---|
open | booleanCurrent open state |
Trigger
The button that toggles the tooltip. By default, the TooltipContent will position itself against the trigger.
| Prop | Default | Type |
|---|---|---|
as | 'button' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Data Attribute | Value |
|---|---|
[data-state] | "closed" | "delayed-open" | "instant-open" |
Portal
When used, portals the content part into the body.
| Prop | Default | Type |
|---|---|---|
disabled | booleanDisable teleport and render the component inline | |
forceMount | booleanUsed to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | |
to | string | HTMLElementVue native teleport component prop |
Content
The component that pops out when the tooltip is open.
| Prop | Default | Type |
|---|---|---|
align | 'start' | 'center' | 'end'The preferred alignment against the trigger. May change when collisions occur. | |
alignOffset | numberAn offset in pixels from the | |
ariaLabel | stringBy default, screenreaders will announce the content inside the component. If this is not descriptive enough, or you have content that cannot be announced, use aria-label as a more descriptive label. | |
arrowPadding | numberThe padding between the arrow and the edges of the content. If your content has border-radius, this will prevent it from overflowing the corners. | |
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
avoidCollisions | booleanWhen | |
collisionBoundary | Element | (Element | null)[] | nullThe element used as the collision boundary. By default this is the viewport, though you can provide additional element(s) to be included in this check. | |
collisionPadding | number | Partial<Record<'top' | 'right' | 'bottom' | 'left', number>>The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }. | |
forceMount | booleanUsed to force mounting when more control is needed. Useful when controlling animation with Vue animation libraries. | |
hideWhenDetached | booleanWhether to hide the content when the trigger becomes fully occluded. | |
side | 'top' | 'top' | 'right' | 'bottom' | 'left'The preferred side of the trigger to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled. |
sideOffset | numberThe distance in pixels from the trigger. | |
sticky | 'partial' | 'always'The sticky behavior on the align axis. |
| Emit | Payload |
|---|---|
escapeKeyDown | [event: KeyboardEvent]Event handler called when focus moves to the destructive action after opening. It can be prevented by calling |
pointerDownOutside | [event: Event]Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling |
| Data Attribute | Value |
|---|---|
[data-state] | "closed" | "delayed-open" | "instant-open" |
[data-side] | "left" | "right" | "bottom" | "top" |
[data-align] | "start" | "end" | "center" |
| CSS Variable | Description |
|---|---|
--radix-tooltip-content-transform-origin | The transform-origin computed from the content and arrow positions/offsets |
--radix-tooltip-content-available-width | The remaining width between the trigger and the boundary edge |
--radix-tooltip-content-available-height | The remaining height between the trigger and the boundary edge |
--radix-tooltip-trigger-width | The width of the trigger |
--radix-tooltip-trigger-height | The height of the trigger |
Arrow
An optional arrow element to render alongside the tooltip. This can be used to help visually link the trigger with the TooltipContent. Must be rendered inside TooltipContent.
| Prop | Default | Type |
|---|---|---|
as | 'svg' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
height | 5 | numberThe height of the arrow in pixels. |
width | 10 | numberThe width of the arrow in pixels. |
Examples
Configure globally
Use the Provider to control delayDuration and skipDelayDuration globally.
<script setup>
import { TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
</script>
<template>
<TooltipProvider
:delay-duration="800"
:skip-delay-duration="500"
>
<TooltipRoot>
<TooltipTrigger>…</TooltipTrigger>
<TooltipContent>…</TooltipContent>
</TooltipRoot>
<TooltipRoot>
<TooltipTrigger>…</TooltipTrigger>
<TooltipContent>…</TooltipContent>
</TooltipRoot>
</TooltipProvider>
</template>Show instantly
Use the delayDuration prop to control the time it takes for the tooltip to open.
<script setup>
import { TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
</script>
<template>
<TooltipRoot :delay-duration="0">
<TooltipTrigger>…</TooltipTrigger>
<TooltipContent>…</TooltipContent>
</TooltipRoot>
</template>Displaying a tooltip from a disabled button
Since disabled buttons don't fire events, you need to:
- Render the
Triggerasspan. - Ensure the
buttonhas nopointerEvents.
<script setup>
import { TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
</script>
<template>
<TooltipRoot>
<TooltipTrigger as-child>
<span tabindex="0">
<button
disabled
style="{ pointerEvents: 'none' }"
>…</button>
</span>
</TooltipTrigger>
<TooltipContent>…</TooltipContent>
</TooltipRoot>
</template>Constrain the content size
You may want to constrain the width of the content so that it matches the trigger width. You may also want to constrain its height to not exceed the viewport.
We expose several CSS custom properties such as --radix-tooltip-trigger-width and --radix-tooltip-content-available-height to support this. Use them to constrain the content dimensions.
<!-- index.vue -->
<script setup>
import { TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
</script>
<template>
<TooltipRoot>
<TooltipTrigger>…</TooltipTrigger>
<TooltipPortal>
<TooltipContent
class="TooltipContent"
:side-offset="5"
>
…
</TooltipContent>
</TooltipPortal>
</TooltipRoot>
</template>/* styles.css */
.TooltipContent {
width: var(--radix-tooltip-trigger-width);
max-height: var(--radix-tooltip-content-available-height);
}Origin-aware animations
We expose a CSS custom property --radix-tooltip-content-transform-origin. Use it to animate the content from its computed origin based on side, sideOffset, align, alignOffset and any collisions.
<!-- index.vue -->
<script setup>
import { TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
</script>
<template>
<TooltipRoot>
<TooltipTrigger>…</TooltipTrigger>
<TooltipContent class="TooltipContent">
…
</TooltipContent>
</TooltipRoot>
</template>/* styles.css */
.TooltipContent {
transform-origin: var(--radix-tooltip-content-transform-origin);
animation: scaleIn 0.5s ease-out;
}
@keyframes scaleIn {
from {
opacity: 0;
transform: scale(0);
}
to {
opacity: 1;
transform: scale(1);
}
}Collision-aware animations
We expose data-side and data-align attributes. Their values will change at runtime to reflect collisions. Use them to create collision and direction-aware animations.
<!-- index.vue -->
<script setup>
import { TooltipContent, TooltipProvider, TooltipRoot, TooltipTrigger } from 'radix-vue'
</script>
<template>
<TooltipRoot>
<TooltipTrigger>…</TooltipTrigger>
<TooltipContent class="TooltipContent">
…
</TooltipContent>
</TooltipRoot>
</template>/* styles.css */
.TooltipContent {
animation-duration: 0.6s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.TooltipContent[data-side="top"] {
animation-name: slideUp;
}
.TooltipContent[data-side="bottom"] {
animation-name: slideDown;
}
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}Accessibility
Keyboard Interactions
| 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. |
Custom APIs
Create your own API by abstracting the primitive parts into your own component.
Abstract parts and introduce a content prop
This example abstracts all of the Tooltip parts and introduces a new content prop.
Usage
<script setup lang="ts">
import { Tooltip } from './your-tooltip'
</script>
<template>
<Tooltip content="Tooltip content">
<button>Tooltip trigger</button>
</Tooltip>
</template>Implementation
Use the asChild prop to convert the trigger part into a slottable area. It will replace the trigger with the child that gets passed to it.
<!-- your-tooltip.vue -->
<script setup lang="ts">
import { TooltipArrow, TooltipContent, TooltipRoot, type TooltipRootEmits, type TooltipRootProps, TooltipTrigger, useForwardPropsEmits } from 'radix-vue'
const props = defineProps<TooltipRootProps & { content?: string }>()
const emits = defineEmits<TooltipRootEmits>()
const forward = useForwardPropsEmits(props, emits)
</script>
<template>
<TooltipRoot v-bind="forward">
<TooltipTrigger as-child>
<slot />
</TooltipTrigger>
<TooltipContent
side="top"
align="center"
>
{{ content }}
<TooltipArrow
:width="11"
:height="5"
/>
</TooltipContent>
</TooltipRoot>
</template>