Highlight 醒目標示文字
醒目標示文字中的關鍵字,適用於搜尋結果、清單篩選及記錄檢視。內容皆以純文字呈現,不解析內含的 HTML。
引入
import { HuiHighlight } from '@nscode/handy-ui'
基礎用法
text 是顯示文字,patterns 是需要標示的關鍵字,可傳入字串或字串陣列。各關鍵字的所有匹配位置皆會標示,預設不區分大小寫。
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight text="HandyUI 是一個 Vue 3 元件庫" :patterns="['元件庫', 'vue']" />
<HuiHighlight text="訂單 20260001 已發貨" patterns="20260001" />
<HuiHighlight text="蘋果、香蕉、蘋果汁、蘋果派" patterns="蘋果" />
</HuiSpace>
</template>
patterns 是空字串、空陣列或未傳入時不醒目標示,原樣顯示文字;陣列裡的空字串會被忽略,重復的詞只算一次。
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">未傳入</span>
<HuiHighlight text="原樣顯示,不醒目標示" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">空字串</span>
<HuiHighlight text="原樣顯示,不醒目標示" patterns="" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">空陣列</span>
<HuiHighlight text="原樣顯示,不醒目標示" :patterns="[]" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">有空字串</span>
<HuiHighlight
text="空字串被忽略,只醒目標示“醒目標示”"
:patterns="['', '醒目標示', '醒目標示']"
/>
</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>
搜尋結果醒目標示
配合輸入框做搜尋時,把關鍵字傳遞給 patterns 即可。下面把空白分開的多個詞都醒目標示:
<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="輸入關鍵字" clearable />
<ul>
<li v-for="item in items" :key="item">
<HuiHighlight :text="item" :patterns="patterns" />
</li>
</ul>
</HuiSpace>
</template>
重疊和相鄰的詞
多個關鍵字的匹配範圍若重疊或相鄰,會合併為單一標示區段。某個關鍵字包含另一個時,採用較長範圍,與陣列順序無關。
| 文字 | patterns | 醒目標示結果 |
|---|---|---|
北京市朝阳区 | ['北京', '北京市'] | 北京市朝阳区 |
abcdef | ['abc', 'cde'] | abcdef(重疊) |
abcdef | ['ab', 'cd'] | abcdef(相鄰) |
ab cd | ['ab', 'cd'] | ab cd(分成兩段) |
表中的輸入資料與結果保留原始字元。元件只標示匹配範圍,不會轉換簡繁體。
實際效果:
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">包含</span>
<HuiHighlight text="北京市朝陽區" :patterns="['北京', '北京市']" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">重疊</span>
<HuiHighlight text="abcdef" :patterns="['abc', 'cde']" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">相鄰</span>
<HuiHighlight text="abcdef" :patterns="['ab', 'cd']" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">分開</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-sensitive 設為 true 後只醒目標示大小寫一致的地方。不區分大小寫時,醒目標示的是原文的寫法。
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">不區分(預設)</span>
<!-- 醒目標示 Vue、vue、VUE -->
<HuiHighlight text="Vue、vue 和 VUE" patterns="vue" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">case-sensitive</span>
<!-- 只醒目標示 vue -->
<HuiHighlight text="Vue、vue 和 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>
特殊字元和正規表示式
預設(auto-escape 為 true)每個詞按字面匹配,.、*、(、$、C++ 這類帶正規表示式特殊字元的詞不用自己轉義:
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight text="C++ 和 C# 都支援" patterns="C++" />
<HuiHighlight text="價格 $100.00" patterns="$100.00" />
<HuiHighlight text="檔案名是 report(1).xlsx" patterns="(1)" />
</HuiSpace>
</template>
auto-escape 設為 false 時,每個詞按正規表示式處理,例如醒目標示所有數字:
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight text="訂單 1024 共 3 件" patterns="\d+" :auto-escape="false" />
<HuiHighlight
text="聯繫電話 13800001111,備用 13900002222"
patterns="1[3-9]\d{9}"
:auto-escape="false"
/>
<HuiHighlight
text="2026-01-05 創建,2026-03-18 更新"
:patterns="['\\d{4}-\\d{2}-\\d{2}', '更新']"
:auto-escape="false"
/>
</HuiSpace>
</template>
無效的正規表示式(例如輸入 ()不會拋出錯誤:略過無效項目,其餘項目照常匹配;全部無效時原樣顯示文字,並於開發環境輸出 [HandyUI] 警告。匹配旗標由元件決定,包含全域匹配及依 case-sensitive 決定的 i,不能在關鍵字中另設旗標。能匹配空字串的規則(例如 a*、^)不會標示空範圍。
下面可以輸入詞,切換“按正規表示式”和“區分大小寫”看效果。按正規表示式時輸入 ( 這類寫錯的正規表示式,文字原樣顯示:
<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 = '元件的類名都以 hui- 開頭,例如 hui-button、hui-input、NS-TAG。'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiInput v-model="pattern" placeholder="要醒目標示的詞" style="width: 200px" />
<HuiCheckbox v-model="regexp">按正規表示式(auto-escape 為 false)</HuiCheckbox>
<HuiCheckbox v-model="caseSensitive">區分大小寫</HuiCheckbox>
</HuiSpace>
<HuiHighlight
:text="text"
:patterns="pattern"
:auto-escape="!regexp"
:case-sensitive="caseSensitive"
/>
</HuiSpace>
</template>
自訂標籤和樣式
預設用 <mark> 包住醒目標示的文字,背景使用更醒目的金黃色(--hui-color-warning-light-5,警告色與主題背景各佔50%),文字顏色跟隨所在的文字,亮暗主題自動適配。可以改:
highlight-tag:換成別的標籤,例如strong、em、span;highlight-class:加類名,用法和 Vue 的class綁定一樣(字串、陣列、物件);highlight-style:加行內樣式,可以是物件或字串。
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight
text="訂單 20260001 已發貨"
patterns="20260001"
highlight-tag="strong"
:highlight-style="{ color: 'var(--hui-color-primary)', backgroundColor: 'transparent' }"
/>
<HuiHighlight text="庫存不足,剩餘 3 件" patterns="庫存不足" highlight-class="stock-warning" />
</HuiSpace>
</template>
<style>
.stock-warning {
color: var(--hui-color-danger);
background-color: var(--hui-color-danger-bg);
}
</style>
預設樣式寫在 :where(),選擇器特異性為 0。highlight-class 只要設定同名屬性即可覆寫,不需 !important 或 :deep()。標示元素的類別為 hui-highlight__mark,樣式未使用 scoped。
highlight-tag 換成不同的標籤時,瀏覽器給這些標籤的預設樣式會疊加在醒目標示背景上,例如 strong、b 加粗,em、i 斜體,u 加底線:
<template>
<HuiSpace direction="vertical" align="start">
<HuiHighlight text="mark(預設):醒目標示這幾個字" patterns="這幾個字" />
<HuiHighlight text="strong:醒目標示這幾個字" patterns="這幾個字" highlight-tag="strong" />
<HuiHighlight text="em:醒目標示這幾個字" patterns="這幾個字" highlight-tag="em" />
<HuiHighlight text="b:醒目標示這幾個字" patterns="這幾個字" highlight-tag="b" />
<HuiHighlight text="u:醒目標示這幾個字" patterns="這幾個字" highlight-tag="u" />
<HuiHighlight text="span:醒目標示這幾個字" patterns="這幾個字" highlight-tag="span" />
</HuiSpace>
</template>
highlight-class 和 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 字串:庫存不足" patterns="庫存不足" highlight-class="stock-warning" />
<HuiHighlight
text="class 陣列:庫存不足"
patterns="庫存不足"
:highlight-class="['stock-warning', 'stock-bold']"
/>
<HuiSpace wrap>
<HuiHighlight
text="class 物件:庫存不足"
patterns="庫存不足"
:highlight-class="{ 'stock-warning': urgent, 'stock-bold': urgent }"
/>
<HuiCheckbox v-model="urgent">加上類名</HuiCheckbox>
</HuiSpace>
<HuiHighlight
text="style 物件:已發貨"
patterns="已發貨"
:highlight-style="{ color: 'var(--hui-color-success)', backgroundColor: 'transparent' }"
/>
<HuiHighlight
text="style 字串:已發貨"
patterns="已發貨"
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>
在段落中使用
根元素是行內的 <span>,可以直接放進段落、表格儲存格、清單項裡,字級、顏色、行高都跟隨外層。醒目標示跨行時每行的醒目標示塊都帶圓角。
<template>
<HuiParagraph size="lg" style="max-width: 420px">
<HuiHighlight
text="元件庫的顏色、尺寸和間距都由主題變數控制,修改主題變數就能統一調整所有元件的外觀,切換暗色主題時所有元件也會跟隨變化。"
:patterns="['主題變數', '暗色主題時所有元件也會跟隨變化']"
/>
</HuiParagraph>
</template>
文字中有 HTML
text 裡的內容全部按文字顯示,<b>、<img onerror> 這類標籤原樣顯示,不會被解析成元素,不需要自己轉義,可以放心顯示使用者輸入的內容。
<template>
<!-- 頁面上顯示的就是 <b>加粗</b> 這幾個字 -->
<HuiHighlight text="<b>加粗</b> 和 <i>斜體</i>" patterns="<b>" />
</template>
Props
| 屬性 | 型別 | 預設值 | 說明 |
|---|---|---|---|
| text | string | '' | 要顯示的文字,按純文字顯示 |
| patterns | string | string[] | [] | 要醒目標示的詞;空字串被忽略,重疊或相鄰的匹配合併成一段 |
| caseSensitive | boolean | false | 是否區分大小寫 |
| highlightTag | string | 'mark' | 醒目標示部分用的標籤名;script、style、iframe 等會執行或載入內容的標籤和不合法的名字改用 mark |
| highlightClass | string | string[] | Record<string, boolean> | - | 加在每個醒目標示元素上的類名 |
| highlightStyle | StyleValue | - | 加在每個醒目標示元素上的樣式,可以是物件或字串 |
| autoEscape | boolean | true | 是否把詞裡的正規表示式特殊字元轉義、按字面匹配;為 false 時按正規表示式處理 |
Events
無。
Slots
無。要顯示的文字通過 text 傳,不支援預設插槽。
方法
無。
型別
import type { HighlightProps, HighlightClass } from '@nscode/handy-ui'
type HighlightClass = string | string[] | Record<string, boolean>