vael-ui v0.5.7

Used with a component · Tooltip

useTooltip

The headless tooltip behind <Tooltip>: open and close delays, a warm group in which the next tooltip opens at once and glides from the last one, hover onto the tooltip, Escape, and touch long-press. It positions through useFloatingPosition and listens on the trigger for you. It doesn't render or set aria-describedby. <Tooltip> adds the panel, the teleport, the ARIA wiring and the glide styles. Use it to attach the same behavior to markup of your own.

Install

import { useTooltip } from 'vael-ui'

Examples

<script setup lang="ts">
import { shallowRef, useId, useTemplateRef } from 'vue'
import { useTooltip } from 'vael-ui'

const open = shallowRef(false)
const tipId = useId()
const triggerEl = useTemplateRef<HTMLElement>('trigger')
const positionerEl = useTemplateRef<HTMLElement>('positioner')
const { positionerStyle, transformOrigin } = useTooltip(open, {
  triggerEl,
  positionerEl,
  side: 'bottom',
})
</script>

<template>
  <button ref="trigger" :aria-describedby="open ? tipId : undefined">Copy</button>
  <Teleport to="body">
    <div v-if="open" :id="tipId" ref="positioner" role="tooltip" :style="positionerStyle">
      <div class="tip" :style="{ transformOrigin }">Copy to clipboard</div>
    </div>
  </Teleport>
</template>

Parameters

NameTypeDescription
openRef<boolean>Whether the tooltip is open (first argument). The tooltip writes to it as it opens and closes.
triggerElRef<HTMLElement | null>The element the tooltip describes and anchors to. Removing it from the page closes the tooltip.
positionerElRef<HTMLElement | null>The positioned wrapper. Hovering it keeps the tooltip open while interactive is on. Teleport it to the body: the style it gets is absolute, in page coordinates.
sideMaybeRefOrGetter<'top' | 'right' | 'bottom' | 'left'>The side of the trigger to open on. It flips when there is no room. Default top.
alignMaybeRefOrGetter<'start' | 'center' | 'end'>How it lines up along that side. Default center.
sideOffsetMaybeRefOrGetter<number>The gap from the trigger, in px. Default 8.
alignOffsetMaybeRefOrGetter<number>The shift along the aligned edge, in px. Default 0.
openDelayMaybeRefOrGetter<number>The delay before a cold open, in ms. Default 400. A warm open is always instant: another tooltip is visible, or one closed less than 300 ms ago.
closeDelayMaybeRefOrGetter<number>The wait after the pointer leaves, in ms, long enough to move onto the tooltip. Default 100.
interactiveMaybeRefOrGetter<boolean>Keeps the tooltip open while you hover it, so its text can be selected. Keyboard users can’t reach it, so put links and buttons in a Popover. Default true.
closeOnEscMaybeRefOrGetter<boolean>Closes on Escape. The tooltip takes the Escape before any dialog around it, so the dialog stays open. Default true.
onOpenChange(value: boolean, details: TooltipOpenChangeDetails) => voidFires before every close, with value: false. details.reason is 'pointer', 'focus', 'trigger', 'escape' or 'programmatic'. Call details.cancel() to keep the tooltip open. It doesn't fire on open.
beforeClose() => BeforeCloseA getter for the close guard. Return true to close, false or nothing to stay open, or a promise of either. isClosing is true while a promise is pending.

Returns

NameTypeDescription
positionerStyleRef<Record<string, string>>Bind it on the positioner. It stays hidden until the first measurement, then sets position, top, left, visibility and a translate while gliding.
panelStyleRef<Record<string, string>>The custom properties for the glide between triggers: --ui-tooltip-morph-x, --ui-tooltip-morph-y (the box scale) and --ui-tooltip-content-opacity. Bind it on the panel if you style the morph.
placementRef<Placement>The resolved placement after flipping, such as 'top' or 'bottom-start'.
transformOriginRef<string>The trigger-facing origin for a scale-in animation.
isClosingRef<boolean>true while the beforeClose guard decides.
instantRef<boolean>true when this open was warm. Skip the entrance then.
traveling / morphing / travelActiveRef<boolean>The glide state. traveling and morphing are on while the position and size transitions should run; travelActive covers the whole glide, from the staged first frame until it settles.
show / hide() => voidshow opens at once, skipping openDelay, and revives a pending close. hide closes through onOpenChange and beforeClose with the programmatic reason.
requestClose(reason: TooltipCloseReason, event?: Event) => voidCloses with your own reason, through onOpenChange and beforeClose.
cancelClose() => voidKeeps the tooltip open and ignores the pending beforeClose answer.