Skip to content

Tooltip ​

@ew-color-picker/tooltip is the floating hint used by Select (when the selected value or an option is truncated) and available to any other component you build. It is a plain DOM utility, not a core plugin: import it wherever you need a tooltip.

Its feature set mirrors Element Plus el-tooltip: 12 placements with viewport flipping/shifting, dark / light effects, arrow, configurable triggers, show/hide delays, controlled visibility, virtual triggering, keyboard support and lifecycle events.

ItemValue
Package@ew-color-picker/tooltip
ClassTooltip
HelperisTextTruncated(el) — also exported by @ew-color-picker/utils
Styles@ew-color-picker/style/dist/tooltip.css

Extra stylesheet

ts
import '@ew-color-picker/style/dist/tooltip.css'

The Select and Gradient components rely on it as well.

Usage ​

Declarative mode — pass a trigger element and let the tooltip manage its own events:

ts
import Tooltip from '@ew-color-picker/tooltip'

const tooltip = new Tooltip({
  target: '#save-btn',
  content: 'Save the current color',
  placement: 'top',
  effect: 'light',
})

// update options later
tooltip.setOptions({ placement: 'bottom', effect: 'dark' })

// remove node and listeners when the owner is destroyed
tooltip.destroy()

Imperative mode — useful when content depends on runtime state (this is how Select shows truncated text):

ts
import Tooltip, { isTextTruncated } from '@ew-color-picker/tooltip'

const tooltip = new Tooltip({ prefix: 'ew-' })

if (isTextTruncated(target)) {
  tooltip.show(target.textContent!.trim(), target)
}

tooltip.hide()
tooltip.destroy()

Options ​

OptionTypeDefaultDescription
prefixstring'ew-'Class prefix; keep it in sync with the picker prefix
targetHTMLElement | string—Trigger element (or selector); bind trigger events automatically
contentstring | HTMLElement—Tooltip content; strings may contain HTML when html is on
htmlbooleanfalseTreat string content as HTML (preferred; matches Button / Checkbox / Select)
rawContentbooleanfalseLegacy alias of html, kept for compatibility
effect'dark' | 'light''dark'Theme
placementTooltipPlacement'bottom'Preferred placement, 12 values (top, top-start, left-end, …)
fallbackPlacementsTooltipPlacement[][]Candidates tried when the preferred placement overflows; defaults to the opposite side
visibleboolean—Controlled visibility; once passed, update it through setOptions({ visible })
disabledbooleanfalseIgnore every trigger and show request
offsetnumber12Gap between the tooltip and its anchor, in px
arrowOffsetnumber5Minimum distance between the arrow and the tooltip edge
showArrowbooleantrueRender the arrow
showAfternumber0Delay before showing (ms)
hideAfternumber200Delay before hiding after the pointer leaves (ms)
autoClosenumber0Hide automatically N ms after showing; 0 disables
popperClassstring''Extra class name on the tooltip node
popperStylestring | Record<string, string>''Inline style on the tooltip node
appendToHTMLElement | stringdocument.bodyParent the tooltip node is appended to
teleportedbooleantruefalse renders the node next to the trigger
enterablebooleantrueAllow the pointer to move into the tooltip content
triggerTooltipTrigger | TooltipTrigger[]'hover'hover, click, focus, contextmenu; arrays combine them
virtualTriggeringbooleanfalsePosition from virtualRef instead of a DOM trigger
virtualRefHTMLElement | { getBoundingClientRect() }nullVirtual anchor used with virtualTriggering
triggerKeysstring[]['Enter', 'Space']Keys that open a click-triggered tooltip
persistentbooleantrueKeep the node in the DOM after hiding
transitionstring''Extra class name for a custom transition
ariaLabelstring''aria-label of the tooltip node
focusOnTargetbooleanfalseMove focus back to the trigger after hiding
popperOptions{ strategy?: 'fixed' | 'absolute' }{ strategy: 'fixed' }Positioning strategy: viewport or document coordinates
classNamestring''Legacy alias of popperClass
containerHTMLElement—Legacy alias of appendTo

Events ​

Pass callbacks as options (onBeforeShow …) or subscribe with on():

ts
tooltip.on('show', () => console.log('shown'))
tooltip.on('visibleChange', (visible) => console.log(visible))
EventPayloadDescription
beforeShowEvent | undefinedFired before the tooltip becomes visible
showEvent | undefinedFired after the tooltip becomes visible
beforeHideEvent | undefinedFired before hiding
hideEvent | undefinedFired after hiding
visibleChangebooleanVisibility requested/changed; use it with controlled visible

Methods ​

MethodDescription
show(content?, anchor?)Show the hint; empty content behaves like hide()
hide()Hide immediately but keep the node (unless persistent is false)
onOpen() / onClose()Same names as the exposed el-tooltip API
isVisible()Whether the hint is currently visible
getElement() / getContentElement()Tooltip node / content node
getTrigger()Resolved trigger element
isFocusInsideContent()Whether focus is inside the tooltip content
updatePopper()Recalculate position and arrow
setContent(content)Replace content while keeping the current state
setOptions(options)Merge new options at runtime
on(event, handler) / off(event, handler?)Subscribe / unsubscribe lifecycle events
destroy()Remove the node, timers and listeners

Positioning ​

The node uses position: fixed (or absolute with popperOptions.strategy) and is placed on the anchor with the requested placement. It flips to fallbackPlacements (opposite side by default) when the preferred side does not fit, then shifts along the cross axis to stay inside the viewport. Position is recalculated on show, on updatePopper(), and on window scroll/resize while open.

The arrow follows the anchor center and is clamped by arrowOffset.

Accessibility ​

When visible, the trigger receives aria-describedby pointing at the tooltip node, which carries role="tooltip" and an optional aria-label. Escape hides the tooltip; focusOnTarget returns focus to the trigger.

Classes ​

.ew-tooltip, .ew-tooltip--dark, .ew-tooltip--light, .ew-tooltip--visible, .ew-tooltip--enterable, .ew-tooltip__content, .ew-tooltip__arrow, plus a data-placement attribute on both the node and the arrow. The prefix follows the prefix option, so a my- prefix produces .my-tooltip.

Notes ​

  • el-tooltip slots are replaced by content (string / HTMLElement) and a real trigger element (target); the default slot has no direct equivalent in vanilla DOM.
  • transition accepts a class name; provide the matching CSS yourself. The default fade is built in.
  • html (or the legacy rawContent) renders HTML as-is — sanitize untrusted input yourself.

Examples ​

A complete runnable playground lives in examples/20-tooltip.html, covering the dark / light themes, all 12 placements, the four trigger modes, delays and auto close, controlled visibility, virtual anchors and the disabled state.

bash
pnpm example
# open http://localhost:3000/examples/20-tooltip.html

See also ​

  • Select — uses this package for truncated option text
  • Styles & Classes — stylesheet entries and class prefix