vue-text-highlight
Text highlighter library for Vue.js.
Installation
npm install --save vue-text-highlight
# or
yarn add vue-text-highlight
Usage
Basic Usage
import Vue from 'vue';
import TextHighlight from 'vue-text-highlight';
Vue.component('text-highlight', TextHighlight);
// new Vue ...
SomeComponent.vue
<template>
<text-highlight :queries="queries">{{ description }}</text-highlight>
</template>
data() {
return {
queries: ['birds', 'scatt'],
description: 'Tropical birds scattered as Drake veered the Jeep'
};
}
Output

More Options
All available props in TextHighlight component are:
-
queries:Array<String|RegExp>|String|RegExpThis prop accepts string, regex, and array of strings or regex. If array is given, it will highlight the union of matched strings/regex globally.
-
[caseSensitive]:BooleanWhether string being searched is case sensitive.
-
[diacriticsSensitive]:BooleanWhether string being searched is diacritics sensitive.
-
[highlightStyle]:Object|Array|StringStyles to be applied to highlighted
<mark>. Similar to style bindings in vue, it acceptsArraysyntax,Objectsyntax, or plain styling asString. This prop will then be merged with default highlight styles inTextHighlightcomponent. See style binding in Vue.js. -
[highlightClass]:Object|Array|StringClasses to be added to highlighted
<mark>. Similar to class bindings in vue, it acceptsArraysyntax,Objectsyntax, or class asString. See class binding in Vue.js. -
[highlightComponent]:Object|StringBy default vue-text-highlight uses
<mark>for the highlighting. Pass this props to override with other tag (string) or custom component (Vue component definition).This component will be passed with two props from
text-highlight:-
index:NumberIndex of highlighted component.
-
text:StringHighlighted words, equals to
this.$slots.default[0].text
For more details, see example below.
-
-
Other props and listeners that are not listed above are forwarded to the highlighted component. These props will be merged with higher precendence than
indexandtextpassed fromtext-highlight.
Advanced Usage
There might be a case where you want to do more things with the highlighted words. For that reason, vue-text-highlight supports custom component for the highlighted words. In this case, the following example alerts on click.
OtherComponent.vue
<template>
<text-highlight
:queries="queries"
:highlightComponent="MyClickableComponent"
:baz="foo"
@customlistener="alert"
>
{{ description }}
</text-highlight>
</template>
import MyClickableComponent from 'MyClickableComponent';
data() {
return {
queries: ['birds', 'scatt'],
description: 'Tropical birds scattered as Drake veered the Jeep'
MyClickableComponent,
foo: 'bar',
};
},
methods: {
alert() {},
}
MyClickableComponent.vue
<template>
<mark class="custom" @click="$emit('customlistener')">
<slot></slot>
</mark>
</template>
props: {
baz: String, // From OtherComponent.vue
index: Number, // From TextHighlight
text: String, // From TextHighlight, equals to `this.$slots.default[0].text`
}