Skip to content

v-highlight

Highlight keywords in text content.

Since: 1.5.0

Usage

Basic

vue
<template>
  <p v-highlight="'important'">This is an important message.</p>
</template>

Multiple Keywords

vue
<template>
  <p v-highlight="['Vue', 'React']">Vue and React are popular frameworks.</p>
</template>

With Options

vue
<template>
  <p v-highlight="{
    keywords: 'highlight',
    className: 'my-highlight',
    style: 'background: yellow; color: black;',
    caseSensitive: true
  }">
    This will highlight the word.
  </p>
</template>

API

Options

OptionTypeDefaultDescription
keywordsstring | string[]-Keywords to highlight
classNamestring'v-highlight'CSS class for highlighted text
stylestring-Inline style for highlighted text
tagstring'mark'HTML tag for highlighted text
caseSensitivebooleanfalseCase sensitive matching
wholeWordbooleanfalseMatch whole words only

Binding Types

ts
type HighlightBinding = string | string[] | HighlightOptions

Examples

Search Highlight

vue
<template>
  <input v-model="searchQuery" placeholder="Search..." />
  <p v-highlight="{ keywords: searchQuery, caseSensitive: false }">
    This is some searchable content with keywords to find.
  </p>
</template>

With Custom Style

vue
<template>
  <p v-highlight="{
    keywords: ['important', 'urgent'],
    style: 'background: #ffeb3b; padding: 0 4px; border-radius: 2px;'
  }">
    This is an important and urgent notification.
  </p>
</template>

Composable API

For programmatic use, you can use the useHighlight composable:

typescript
import { useHighlight } from 'directix'

const { highlight, clear, updateKeywords } = useHighlight({
  keywords: ['important', 'key'],
  className: 'highlight',
  caseSensitive: false
})

// Highlight text manually
const highlighted = highlight('This is important content')

// Clear highlights
clear()

// Update keywords
updateKeywords(['new', 'keywords'])

UseHighlightReturn

PropertyTypeDescription
highlight(text: string) => stringHighlight text manually
clear() => voidClear all highlights
updateKeywords(keywords: string[]) => voidUpdate keywords to highlight

Code Generator

Quick Code Generator
<template>
  <div v-highlight="{ keyword: 'important', color: '#ffff00' }">
    <!-- Highlight keywords in text content. directive -->
  </div>
</template>

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

// Configure your options here
const options = { keyword: 'important', color: '#ffff00' }
</script>

Released under the MIT License.