Highlight
Highlight keywords within text, such as search results, filtered lists, or logs. Content is rendered as plain text; embedded HTML is not parsed.
Import
import { HuiHighlight } from '@nscode/handy-ui'
Basic Usage
text is the content to display. patterns contains the terms to highlight, as a string or an array of strings. Every occurrence of each term is highlighted, case-insensitively by default.
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight
text="HandyUI is a Vue 3 component library"
:patterns="['component library', 'vue']"
/>
<HuiHighlight text="Order 20260001 has shipped" patterns="20260001" />
<HuiHighlight text="apple, banana, apple juice, apple pie" patterns="apple" />
</HuiSpace>
</template>
When patterns is an empty string, an empty array, or omitted, the text is shown unchanged. Empty strings within an array are ignored, and duplicate terms are considered once.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Omitted</span>
<HuiHighlight text="Shown unchanged, without highlighting" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Empty string</span>
<HuiHighlight text="Shown unchanged, without highlighting" patterns="" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Empty array</span>
<HuiHighlight text="Shown unchanged, without highlighting" :patterns="[]" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Includes empty strings</span>
<HuiHighlight
text="Empty strings are ignored; only 'highlight' is highlighted"
:patterns="['', 'highlight', 'highlight']"
/>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 80px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Highlighting Search Results
For a search input, pass its keywords to patterns. The example below highlights multiple whitespace-separated terms:
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiHighlight, HuiInput, HuiSpace } from '@nscode/handy-ui'
const keyword = ref('')
const items = ['用户管理', '角色管理', '菜单管理', '操作日志']
const patterns = computed(() => keyword.value.split(/\s+/).filter(Boolean))
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiInput v-model="keyword" placeholder="Enter keywords" clearable />
<ul>
<li v-for="item in items" :key="item">
<HuiHighlight
:text="
{
用户管理: 'User management',
角色管理: 'Role management',
菜单管理: 'Menu management',
操作日志: 'Action logs'
}[item]
"
:patterns="patterns"
/>
</li>
</ul>
</HuiSpace>
</template>
Overlapping and Adjacent Matches
Overlapping or adjacent matches are merged into one highlighted segment rather than separate touching blocks. When one term is contained within another, the longer range is highlighted regardless of their order in the array:
| Text | patterns | Highlighted result |
|---|---|---|
Beijing City, Chaoyang District | ['Beijing', 'Beijing City'] | Beijing City, Chaoyang District |
abcdef | ['abc', 'cde'] | abcdef(Overlapping) |
abcdef | ['ab', 'cd'] | abcdef(Adjacent) |
ab cd | ['ab', 'cd'] | ab cd(Two segments) |
The rendered results:
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Contained</span>
<HuiHighlight
text="Beijing City, Chaoyang District"
:patterns="['Beijing', 'Beijing City']"
/>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Overlapping</span>
<HuiHighlight text="abcdef" :patterns="['abc', 'cde']" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Adjacent</span>
<HuiHighlight text="abcdef" :patterns="['ab', 'cd']" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Separate</span>
<HuiHighlight text="ab cd" :patterns="['ab', 'cd']" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 48px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Case Sensitivity
Set case-sensitive to true to highlight only matches with identical casing. Case-insensitive matching preserves the original text's casing.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Case-insensitive (default)</span>
<!-- Highlight Vue, vue, and VUE -->
<HuiHighlight text="Vue, vue and VUE" patterns="vue" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">case-sensitive</span>
<!-- Highlight only vue -->
<HuiHighlight text="Vue, vue and VUE" patterns="vue" case-sensitive />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 112px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Special Characters and Regular Expressions
By default (auto-escape is true), each term is matched literally. You do not need to escape regular-expression characters in terms such as ., *, (, $, or C++:
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight text="Both C++ and C# are supported" patterns="C++" />
<HuiHighlight text="Price $100.00" patterns="$100.00" />
<HuiHighlight text="The file name is report(1).xlsx" patterns="(1)" />
</HuiSpace>
</template>
With auto-escape set to false, each term is treated as a regular expression, for example to highlight all numbers:
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight text="Order 1024 contains 3 items" patterns="\d+" :auto-escape="false" />
<HuiHighlight
text="Phone 13800001111, backup 13900002222"
patterns="1[3-9]\d{9}"
:auto-escape="false"
/>
<HuiHighlight
text="Created 2026-01-05, updated 2026-03-18"
:patterns="['\\d{4}-\\d{2}-\\d{2}', 'updated']"
:auto-escape="false"
/>
</HuiSpace>
</template>
Invalid expressions, such as a user entering (, do not throw. Invalid terms are ignored while the remaining terms are highlighted. If all terms are invalid, the text is shown unchanged. Development mode logs a warning prefixed with [HandyUI]. The component determines the flags: global matching, plus i according to case-sensitive. Flags cannot be specified inside a term. Empty matches from expressions such as a* or ^ are not highlighted.
Enter a pattern below and toggle regular-expression matching or case sensitivity. With regular-expression matching enabled, an invalid expression such as ( leaves the text unchanged:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiHighlight, HuiInput, HuiSpace } from '@nscode/handy-ui'
const pattern = ref('hui-\\w+')
const regexp = ref(true)
const caseSensitive = ref(false)
const text = 'Component classes start with hui-, for example hui-button, hui-input, and NS-TAG.'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiInput v-model="pattern" placeholder="Pattern to highlight" style="width: 200px" />
<HuiCheckbox v-model="regexp">Regular expression (auto-escape is false)</HuiCheckbox>
<HuiCheckbox v-model="caseSensitive">Case-sensitive</HuiCheckbox>
</HuiSpace>
<HuiHighlight
:text="text"
:patterns="pattern"
:auto-escape="!regexp"
:case-sensitive="caseSensitive"
/>
</HuiSpace>
</template>
Custom Tags and Styles
Highlighted text is wrapped in <mark> by default. Its golden background uses --hui-color-warning-light-5, a 50/50 blend of the warning color and theme background. Text color is inherited, and light and dark themes are supported automatically. Customize it with:
highlight-tag: use another tag, such asstrong,em, orspan.highlight-class: add classes using the same string, array, or object forms as Vue'sclassbinding.highlight-style: add inline styles as an object or string.
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight
text="Order 20260001 has shipped"
patterns="20260001"
highlight-tag="strong"
:highlight-style="{ color: 'var(--hui-color-primary)', backgroundColor: 'transparent' }"
/>
<HuiHighlight
text="Low stock, 3 items remaining"
patterns="Low stock"
highlight-class="stock-warning"
/>
</HuiSpace>
</template>
<style>
.stock-warning {
color: var(--hui-color-danger);
background-color: var(--hui-color-danger-bg);
}
</style>
The default styles have zero specificity because they use :where(). A class supplied through highlight-class can override the same properties without !important or :deep(). The highlight element has the hui-highlight__mark class, and its styles are not scoped.
Changing highlight-tag also applies the browser's default styling for that tag on top of the highlight background: strong and b are bold, em and i are italic, and u is underlined:
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight text="mark (default): highlight these words" patterns="these words" />
<HuiHighlight
text="strong: highlight these words"
patterns="these words"
highlight-tag="strong"
/>
<HuiHighlight text="em: highlight these words" patterns="these words" highlight-tag="em" />
<HuiHighlight text="b: highlight these words" patterns="these words" highlight-tag="b" />
<HuiHighlight text="u: highlight these words" patterns="these words" highlight-tag="u" />
<HuiHighlight text="span: highlight these words" patterns="these words" highlight-tag="span" />
</HuiSpace>
</template>
Different forms of highlight-class and highlight-style:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiHighlight, HuiSpace } from '@nscode/handy-ui'
const urgent = ref(true)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight
text="class string: Low stock"
patterns="Low stock"
highlight-class="stock-warning"
/>
<HuiHighlight
text="class array: Low stock"
patterns="Low stock"
:highlight-class="['stock-warning', 'stock-bold']"
/>
<HuiSpace wrap>
<HuiHighlight
text="class object: Low stock"
patterns="Low stock"
:highlight-class="{ 'stock-warning': urgent, 'stock-bold': urgent }"
/>
<HuiCheckbox v-model="urgent">Apply classes</HuiCheckbox>
</HuiSpace>
<HuiHighlight
text="style object: Shipped"
patterns="Shipped"
:highlight-style="{ color: 'var(--hui-color-success)', backgroundColor: 'transparent' }"
/>
<HuiHighlight
text="style string: Shipped"
patterns="Shipped"
highlight-style="background-color: transparent; text-decoration: underline wavy var(--hui-color-success)"
/>
</HuiSpace>
</template>
<style>
.stock-warning {
color: var(--hui-color-danger);
background-color: var(--hui-color-danger-bg);
}
.stock-bold {
font-weight: var(--hui-font-weight-bold);
}
</style>
Within Paragraphs
The root is an inline <span>, suitable for paragraphs, table cells, and list items. It inherits font size, color, and line height. When a highlight spans lines, its segment on each line has rounded corners.
<template>
<HuiParagraph size="lg" style="max-width: 420px">
<HuiHighlight
text="Component colors, sizes, and spacing are controlled by theme variables. Changing theme variables updates the appearance of all components together; switching to the dark theme updates every component as well."
:patterns="['theme variables', 'dark theme updates every component as well']"
/>
</HuiParagraph>
</template>
Text Containing HTML
All text content is rendered literally. Tags such as <b> or <img onerror> are displayed as text rather than parsed into elements. No manual escaping is required when displaying user input.
<template>
<!-- The page displays the literal text <b>Bold</b> -->
<HuiHighlight text="<b>Bold</b> and <i>italic</i>" patterns="<b>" />
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| text | string | '' | Text to display, rendered as plain text |
| patterns | string | string[] | [] | Terms to highlight; empty strings are ignored and overlapping or adjacent matches are merged |
| caseSensitive | boolean | false | Whether matching is case-sensitive |
| highlightTag | string | 'mark' | Tag for highlighted segments. Invalid names and tags that execute or load content, such as script, style, or iframe, fall back to mark |
| highlightClass | string | string[] | Record<string, boolean> | - | Classes applied to each highlighted element |
| highlightStyle | StyleValue | - | Styles applied to each highlighted element, as an object or string |
| autoEscape | boolean | true | Whether to escape regular-expression characters for literal matching; false treats terms as regular expressions |
Events
None.
Slots
None. Provide content through text; the default slot is not supported.
Methods
None.
Types
import type { HighlightProps, HighlightClass } from '@nscode/handy-ui'
type HighlightClass = string | string[] | Record<string, boolean>