Tooltip
A short hint shown on hover or keyboard focus.
Reka UI Tooltip
<script setup lang="ts">
import * as Tooltip from '@/components/ui/tooltip';
</script>
<template>
<Tooltip.Root>
<Tooltip.Trigger>Hover me</Tooltip.Trigger>
<Tooltip.Content>Add to library</Tooltip.Content>
</Tooltip.Root>
</template>
Installation
npx distill-ui add tooltipIt also copies Button, which Tooltip uses.
It installs reka-ui if you don't have it yet.
- Set up the tokens once, if you haven't: see manual installation.
- Install the packages it needs:
npm install reka-ui@2.11.0 - Copy Button too, which it uses.
- Copy these files into your project:
<script setup lang="ts">
import { TooltipProvider, TooltipRoot } from 'reka-ui';
import { toRef } from 'vue';
import { provideTooltipPlacement } from './context.js';
import type { Placement } from './placement.js';
// Use `v-model:open` to control it from outside.
const { placement = 'top', openDelay = 300 } = defineProps<{
placement?: Placement;
openDelay?: number;
}>();
const open = defineModel<boolean>('open', { default: false });
provideTooltipPlacement(toRef(() => placement));
</script>
<template>
<TooltipProvider :delay-duration="openDelay">
<TooltipRoot v-model:open="open">
<slot />
</TooltipRoot>
</TooltipProvider>
</template>
<script setup lang="ts">
import { TooltipArrow, TooltipContent } from 'reka-ui';
import { computed } from 'vue';
import { injectTooltipPlacement } from './context.js';
import { toSideAlign } from './placement.js';
defineOptions({ inheritAttrs: false });
const position = computed(() => toSideAlign(injectTooltipPlacement().value));
</script>
<template>
<!-- No portal: Reka positions it with position: fixed, so it stays inside your styles. -->
<TooltipContent :side="position.side" :align="position.align" :side-offset="4" as-child>
<div v-bind="$attrs" class="content">
<slot />
<TooltipArrow :width="8" :height="4" as-child>
<span class="arrow"></span>
</TooltipArrow>
</div>
</TooltipContent>
</template>
<!--
Options you can set from a parent or on the tooltip content itself:
--dui-tooltip-max-width, --dui-tooltip-radius, --dui-tooltip-bg, --dui-tooltip-fg
-->
<style scoped>
.content {
width: fit-content;
max-width: var(--dui-tooltip-max-width, 20rem);
padding: var(--dui-space-1-5) var(--dui-space-3);
border-radius: var(--dui-tooltip-radius, var(--dui-radius-md));
background: var(--dui-tooltip-bg, var(--dui-color-primary));
color: var(--dui-tooltip-fg, var(--dui-color-primary-foreground));
font-size: var(--dui-text-xs);
line-height: var(--dui-text-xs-line-height);
text-wrap: balance;
overflow: visible;
/* Reka copies this onto the positioning wrapper it puts around the content. */
z-index: var(--dui-z-popover);
/* Reka waits for animationend before removing it, so this uses the shared keyframes. */
&[data-state='delayed-open'],
&[data-state='instant-open'] {
animation: distill-zoom-in var(--dui-duration-fast) var(--dui-ease-out);
}
&[data-state='closed'] {
animation: distill-zoom-out var(--dui-duration-fast) var(--dui-ease-out);
}
}
/* Reka positions the arrow at the edge; we draw it as a square turned 45° and
pulled halfway under the tooltip, in the tooltip's color. */
.arrow {
display: block;
width: 0.5rem;
height: 0.5rem;
border-radius: 2px;
background: var(--dui-tooltip-bg, var(--dui-color-primary));
translate: 0 -50%;
rotate: 45deg;
}
</style>
<script setup lang="ts">
import { TooltipTrigger } from 'reka-ui';
import { Button, type ButtonSize, type ButtonVariant } from '../button/index.js';
// Renders our Button, so it takes the same variant and size props.
const { variant = 'outline', size } = defineProps<{ variant?: ButtonVariant; size?: ButtonSize }>();
</script>
<template>
<TooltipTrigger as-child>
<Button :variant="variant" :size="size"><slot /></Button>
</TooltipTrigger>
</template>
import { inject, provide } from 'vue';
import type { PlacementRef } from './placement.js';
// The root takes the placement, and the content reads it to position itself.
const key = Symbol('distill-tooltip');
export const provideTooltipPlacement = (placement: PlacementRef) => provide(key, placement);
export const injectTooltipPlacement = () => inject<PlacementRef>(key)!;
export { default as Root } from './Tooltip.vue';
export { default as Trigger } from './TooltipTrigger.vue';
export { default as Content } from './TooltipContent.vue';
export type { Placement } from './placement.js';
import type { Ref } from 'vue';
// Placement is written like Floating UI's ('bottom', 'top-start'…), the same as the
// Svelte version. Reka takes it as a side and an alignment, so this splits it.
export type Placement =
'top' | 'right' | 'bottom' | 'left' | `${'top' | 'right' | 'bottom' | 'left'}-${'start' | 'end'}`;
export function toSideAlign(placement: Placement) {
const [side, align = 'center'] = placement.split('-') as [
'top' | 'right' | 'bottom' | 'left',
('start' | 'end' | 'center')?
];
return { side, align };
}
export type PlacementRef = Readonly<Ref<Placement>>;
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-tooltip-max-width | 20rem |
--dui-tooltip-radius | var(--dui-radius-md) |
--dui-tooltip-bg | var(--dui-color-primary) |
--dui-tooltip-fg | var(--dui-color-primary-foreground) |