Highlight
Keyword highlighting: splits the content into plain / hit segments via splitByKeywords from @lh-ui/share/core/highlight (case-insensitive by default, longest match wins for overlapping keywords, regex metacharacters escaped), and renders hits as <mark class="lh-highlight__mark">. All logic is consumed from @lh-ui/share/core/highlight, so the segmentation semantics are identical across all platforms.
Basic usage
Highlight (single keyword / multiple keywords / case-sensitive / truncation)
lh-ui 是一个跨端组件库家族,双血脉架构,八仓齐发lh-ui 是一个跨端组件库家族,双血脉架构,八仓齐发lh-ui 是一个跨端组件库家族,双血脉架构,八仓齐发lh-ui 是一个跨端组件库家族…
import { LhHighlight } from '@lh-ui/react'
const content = 'lh-ui 是一个跨端组件库家族,双血脉架构,八仓齐发'
export default function Demo() {
return (
<>
<LhHighlight content={content} keywords="跨端" />
<LhHighlight content={content} keywords={['lh-ui', '家族', '不存在']} />
<LhHighlight content={content} keywords="LH-UI" caseSensitive />
<LhHighlight content={content} keywords="双血脉" maxLength={16} />
</>
)
}
Custom hit rendering
renderMark takes over hit segments (the default <mark> is not rendered for those segments), receiving the framework-agnostic LhHighlightSegment (text / hit / keyword / index):
<LhHighlight
content={content}
keywords="跨端"
renderMark={(segment) => <em className="my-hit">{segment.text}</em>}
/>
highlightClass / highlightStyle append a class / inline style string to the default <mark> instead of replacing it.
Highlight API
| Prop | Description | Type | Default |
|---|---|---|---|
| content | Text content (required) | string | — |
| keywords | Keyword(s) to highlight; longest match wins for overlaps | `string | string[]` |
| caseSensitive | Case-sensitive matching (default: case-insensitive) | boolean | — |
| highlightClass | Extra class appended to the mark element | string | — |
| highlightStyle | Inline style string for the mark element | string | — |
| maxLength | Truncates the content to this length (appends …) before splitting | number | — |
| highlightLabel | Accessibility name (falls back to a 40-char content summary) | string | — |
| renderMark | Custom renderer for hit segments (replaces the default mark) | (segment: LhHighlightSegment) => ReactNode | — |
| sx | Quick layout props | LhSxProps | — |
Accessibility
- The root carries
aria-label(highlightLabelwhen given, otherwise the first 40 characters of the content). - Hit segments use the native
<mark>element, so screen readers announce them as highlighted.