Skip to main content

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​

PropDescriptionTypeDefault
contentText content (required)string—
keywordsKeyword(s) to highlight; longest match wins for overlaps`stringstring[]`
caseSensitiveCase-sensitive matching (default: case-insensitive)boolean—
highlightClassExtra class appended to the mark elementstring—
highlightStyleInline style string for the mark elementstring—
maxLengthTruncates the content to this length (appends …) before splittingnumber—
highlightLabelAccessibility name (falls back to a 40-char content summary)string—
renderMarkCustom renderer for hit segments (replaces the default mark)(segment: LhHighlightSegment) => ReactNode—
sxQuick layout propsLhSxProps—

Accessibility​

  • The root carries aria-label (highlightLabel when given, otherwise the first 40 characters of the content).
  • Hit segments use the native <mark> element, so screen readers announce them as highlighted.