Skip to content

v-tooltip

Display tooltips on hover, focus, or click with customizable positioning and styling.

Since: 1.2.0

Usage

Basic

vue
<template>
  <button v-tooltip="'Tooltip text'">Hover me</button>
</template>

With Options

vue
<template>
  <button v-tooltip="{
    content: 'Tooltip content',
    placement: 'top',
    trigger: 'hover'
  }">
    Hover for tooltip
  </button>
</template>

Rich Content

vue
<template>
  <span v-tooltip="{ content: '<strong>Bold</strong> tooltip' }">
    Rich tooltip
  </span>
</template>

API

Types

typescript
type TooltipPlacement = 'top' | 'bottom' | 'left' | 'right'
type TooltipTrigger = 'hover' | 'focus' | 'click' | 'manual'

interface TooltipOptions {
  /** Tooltip content (text or HTML) */
  content?: string
  /** Tooltip placement @default 'top' */
  placement?: TooltipPlacement
  /** Trigger type @default 'hover' */
  trigger?: TooltipTrigger
  /** Show delay in milliseconds @default 0 */
  delay?: number
  /** Hide delay in milliseconds @default 0 */
  hideDelay?: number
  /** Custom class for tooltip */
  class?: string
  /** Disable tooltip @default false */
  disabled?: boolean
  /** Offset from element in pixels @default 8 */
  offset?: number
  /** Z-index @default 9999 */
  zIndex?: number
  /** Maximum width */
  maxWidth?: string
  /** Allow HTML content @default true */
  html?: boolean
  /** Show arrow @default true */
  arrow?: boolean
  /** Theme: 'dark' or 'light' @default 'dark' */
  theme?: 'dark' | 'light'
}

type TooltipBinding = string | TooltipOptions

Options

OptionTypeDefaultDescription
contentstring-Tooltip content
placementstring'top'Tooltip position
triggerstring'hover'How to trigger tooltip
delaynumber0Show delay (ms)
hideDelaynumber0Hide delay (ms)
classstring-Custom CSS class
disabledbooleanfalseDisable tooltip
offsetnumber8Distance from element
zIndexnumber9999Z-index
maxWidthstring-Maximum width
htmlbooleantrueAllow HTML content
arrowbooleantrueShow arrow
themestring'dark'Color theme

Examples

Different Placements

vue
<template>
  <div class="demo-grid">
    <button v-tooltip="{ content: 'Top', placement: 'top' }">Top</button>
    <button v-tooltip="{ content: 'Bottom', placement: 'bottom' }">Bottom</button>
    <button v-tooltip="{ content: 'Left', placement: 'left' }">Left</button>
    <button v-tooltip="{ content: 'Right', placement: 'right' }">Right</button>
  </div>
</template>

Click Trigger

vue
<template>
  <button v-tooltip="{ content: 'Click tooltip', trigger: 'click' }">
    Click me
  </button>
</template>

Manual Control

vue
<template>
  <button
    v-tooltip="{
      content: 'Manual tooltip',
      trigger: 'manual',
      show: isVisible
    }"
    @click="isVisible = !isVisible"
  >
    Toggle tooltip
  </button>
</template>

<script setup>
import { ref } from 'vue'

const isVisible = ref(false)
</script>

Light Theme

vue
<template>
  <button v-tooltip="{ content: 'Light tooltip', theme: 'light' }">
    Light theme
  </button>
</template>

Composable API

For programmatic use, you can use the useTooltip composable:

typescript
import { useTooltip } from 'directix'

const { isVisible, show, hide, toggle, bind } = useTooltip({
  content: 'Tooltip text',
  placement: 'top',
  trigger: 'hover',
  delay: 0,
  hideDelay: 0,
  arrow: true
})

UseTooltipOptions

OptionTypeDefaultDescription
contentstring | Ref<string>-Tooltip content
placement'top' | 'bottom' | 'left' | 'right''top'Tooltip placement
trigger'hover' | 'click' | 'focus' | 'manual''hover'Trigger type
delaynumber0Show delay (ms)
hideDelaynumber0Hide delay (ms)
arrowbooleantrueShow arrow
classstring-Custom CSS class
disabledboolean | Ref<boolean>falseDisable tooltip
onShow() => void-Callback when tooltip shows
onHide() => void-Callback when tooltip hides

UseTooltipReturn

PropertyTypeDescription
isVisibleReadonly<Ref<boolean>>Whether tooltip is visible
show() => voidShow the tooltip
hide() => voidHide the tooltip
toggle() => voidToggle the tooltip
bind(element: HTMLElement) => () => voidBind tooltip to an element

Example

vue
<script setup>
import { ref, onMounted } from 'vue'
import { useTooltip } from 'directix'

const buttonRef = ref(null)

const { isVisible, bind } = useTooltip({
  content: 'Tooltip text',
  placement: 'top'
})

onMounted(() => {
  if (buttonRef.value) {
    bind(buttonRef.value)
  }
})
</script>

<template>
  <button ref="buttonRef">Hover me</button>
</template>

Code Generator

Quick Code Generator
<template>
  <div v-tooltip="{ content: 'Tooltip text', placement: 'top' }">
    <!-- Display tooltips on hover, focus, or click with customizable positioning and styling. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = { content: 'Tooltip text', placement: 'top' }
</script>

Released under the MIT License.