Skip to main content

useHighlight

import { useHighlight } from 'one-more-highlight';

Returns { segments, getMatchCount }. segments covers the full input text as alternating MatchSegment / TextSegment with no gaps. getMatchCount() returns the number of matching segments — useful for validating states config or rendering "N results" UI.

Signature

function useHighlight(options: UseHighlightOptions): UseHighlightResult

interface UseHighlightResult {
segments: ReadonlyArray<Segment>;
getMatchCount: () => number;
}

Options

UseHighlightOptions accepts all matching and state options from <Highlight> — the rendering options (highlightTag, highlightClassName, renderMatch, etc.) are excluded.

OptionTypeDefaultDescription
textstringrequiredText to search.
searchWordsArray<string | RegExp>requiredTerms to find.
caseSensitivebooleanfalseCase-sensitive matching.
autoEscapebooleantrueEscape regex special chars in string terms.
sanitize(s: string) => stringPre-process text before matching.
findChunks(input: FindChunksInput) => RawChunk[]Custom matcher.
overlapStrategy'merge' | 'nest' | 'first-wins''merge'Overlap resolution strategy.
statesHighlightState[]Per-match state selectors.

Return type

type Segment = MatchSegment | TextSegment;

interface MatchSegment {
text: string;
isMatch: true;
matchIndex: number; // 0-based global document order
start: number; // character offset in `text`
end: number;
states: ReadonlyArray<string>; // names of states selecting this match
}

interface TextSegment {
text: string;
isMatch: false;
start: number;
end: number;
}

Memoization

useHighlight uses useMemo internally. Re-computation is triggered only when searchWords contents change (deep comparison) or when any other option changes by reference. Pass stable references (or useMemo/useCallback) for states and findChunks to avoid unnecessary recalculation.