Tooltip

A short hint shown on hover or keyboard focus.

Install

If this is the first component in the project, set up once. This writes ermine.json and copies the theme stylesheet. The installation guide covers Vue, Nuxt and Astro.

npx ermineui init
pnpm dlx ermineui init
yarn dlx ermineui init
bunx ermineui init

Then add the component:

npx ermineui add tooltip
pnpm dlx ermineui add tooltip
yarn dlx ermineui add tooltip
bunx ermineui add tooltip

That copies 5 files into src/components/ui/tooltip/, 3 helpers into src/lib/ermine/. Files you already have are kept.

Use

import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
<script setup lang="ts">
import { Tooltip } from '@/components/ui/tooltip'
</script>

<template>
  <Tooltip />
</template>

In Nuxt, with the auto-import setting, the tags work in templates without the import. In Astro, use it inside a Vue island. The props are documented in the source below, on each one.

What gets copied

FileWhere it goes
Tooltip.vuesrc/components/ui/tooltip/Tooltip.vue
TooltipContent.vuesrc/components/ui/tooltip/TooltipContent.vue
TooltipTrigger.vuesrc/components/ui/tooltip/TooltipTrigger.vue
context.tssrc/components/ui/tooltip/context.ts
index.tssrc/components/ui/tooltip/index.ts
context.ts helpersrc/lib/ermine/context.ts
floating.ts helpersrc/lib/ermine/floating.ts
primitive.ts helpersrc/lib/ermine/primitive.ts

The folders are the suggested ones. ermine.json says where yours are.

Source

This is exactly what the command copies. To install by hand, create each file below and the helpers listed above.

Tooltip.vue 39 lines
<script setup lang="ts">
import { onBeforeUnmount, ref, useId } from 'vue'
import { provideTooltip } from './context'

const props = withDefaults(
  defineProps<{
    /** Hover time before the tooltip appears, in ms. Keyboard focus shows it immediately. */
    delay?: number
    disabled?: boolean
  }>(),
  { delay: 300 },
)

const open = defineModel<boolean>('open', { default: false })
let timer: ReturnType<typeof setTimeout> | undefined

function show(immediate = false) {
  clearTimeout(timer)
  if (props.disabled) return
  if (immediate || open.value) open.value = true
  else timer = setTimeout(() => (open.value = true), props.delay)
}

function hide(immediate = false) {
  clearTimeout(timer)
  if (immediate) open.value = false
  // The grace period lets the pointer travel from the trigger onto the tooltip.
  else timer = setTimeout(() => (open.value = false), 100)
}

onBeforeUnmount(() => clearTimeout(timer))

provideTooltip({ open, show, hide, anchor: ref(null), contentId: useId() })
</script>

<template>
  <slot :open="open" />
</template>
TooltipContent.vue 58 lines
<script setup lang="ts">
import { ref } from 'vue'
import { useFloating, type Align, type Side } from '../../lib/floating'
import { Portal } from '../../lib/primitive'
import { useTooltip } from './context'

defineOptions({ inheritAttrs: false })

const props = withDefaults(
  defineProps<{
    side?: Side
    align?: Align
    /** Gap between trigger and tooltip, in px. */
    sideOffset?: number
  }>(),
  { side: 'top', align: 'center', sideOffset: 6 },
)

const tooltip = useTooltip()
const content = ref<HTMLElement | null>(null)

useFloating(tooltip.anchor, content, tooltip.open, props)
</script>

<template>
  <Portal>
    <Transition name="er-pop">
      <div
        v-if="tooltip.open.value"
        :id="tooltip.contentId"
        ref="content"
        v-bind="$attrs"
        role="tooltip"
        class="er-floating er-tooltip"
        @pointerenter="tooltip.show(true)"
        @pointerleave="tooltip.hide()"
      >
        <slot />
      </div>
    </Transition>
  </Portal>
</template>

<style>
@layer components {
  .er-tooltip {
    max-width: min(20rem, calc(100vw - 1rem));
    padding: 0.375rem 0.75rem;
    border-radius: var(--er-radius-md);
    background: var(--er-primary);
    color: var(--er-primary-foreground);
    font-size: 0.75rem;
    line-height: 1.35;
    text-wrap: balance;
  }
}
</style>
TooltipTrigger.vue 53 lines
<script setup lang="ts">
import type { Component } from 'vue'
import { Primitive, toElement } from '../../lib/primitive'
import { useTooltip } from './context'

withDefaults(
  defineProps<{
    as?: string | Component
    /** Use the single child (e.g. a Button) as the trigger instead of rendering one. */
    asChild?: boolean
  }>(),
  { as: 'button' },
)

const tooltip = useTooltip()

function setAnchor(target: unknown) {
  tooltip.anchor.value = toElement(target)
}

function onPointerEnter(event: PointerEvent) {
  if (event.pointerType !== 'touch') tooltip.show()
}

function onFocus(event: FocusEvent) {
  // Only for keyboard focus — a click also focuses, and should not pop a tooltip.
  if ((event.target as HTMLElement).matches(':focus-visible')) tooltip.show(true)
}

function onKeydown(event: KeyboardEvent) {
  if (event.key === 'Escape') tooltip.hide(true)
}
</script>

<template>
  <Primitive
    :ref="setAnchor"
    :as="as"
    :as-child="asChild"
    :type="as === 'button' && !asChild ? 'button' : undefined"
    :aria-describedby="tooltip.open.value ? tooltip.contentId : undefined"
    :data-state="tooltip.open.value ? 'open' : 'closed'"
    @pointerenter="onPointerEnter"
    @pointerleave="tooltip.hide()"
    @pointerdown="tooltip.hide(true)"
    @focus="onFocus"
    @blur="tooltip.hide(true)"
    @keydown="onKeydown"
  >
    <slot />
  </Primitive>
</template>
context.ts 15 lines
import type { Ref } from 'vue'
import { createContext } from '../../lib/context'

export interface TooltipContext {
  open: Ref<boolean>
  /** Open after the delay, or straight away with `immediate`. */
  show: (immediate?: boolean) => void
  /** Close after a short grace period, or straight away with `immediate`. */
  hide: (immediate?: boolean) => void
  anchor: Ref<HTMLElement | null>
  contentId: string
}

export const [provideTooltip, useTooltip] = createContext<TooltipContext>('Tooltip')
index.ts 5 lines
/** Tooltip — a short hint shown on hover or keyboard focus. */
export { default as Tooltip } from './Tooltip.vue'
export { default as TooltipContent } from './TooltipContent.vue'
export { default as TooltipTrigger } from './TooltipTrigger.vue'