Skip to main content

<Highlight> props

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

Required props

PropTypeDescription
textstringThe text to search and render.
searchWordsArray<string | RegExp>Terms to find. String terms are auto-escaped by default. RegExps are cloned with g flag forced on.

Matching options

PropTypeDefaultDescription
caseSensitivebooleanfalseMatch case (string terms only; regex flags are honored as-is).
autoEscapebooleantrueEscape regex special chars in string terms.
sanitize(s: string) => stringPre-process text and search source before matching. Use for diacritic-insensitive search.
findChunks(input: FindChunksInput) => RawChunk[]Custom matcher; completely replaces the default matchAll-based implementation.
overlapStrategy'merge' | 'nest' | 'first-wins''merge'How overlapping matches are resolved.

State options

PropTypeDefaultDescription
statesHighlightState[]Per-match layered styling. Each entry selects a subset of matches and applies a className and/or style on top of the base.

Rendering options

PropTypeDefaultDescription
highlightTagkeyof JSX.IntrinsicElements | ComponentType'mark'Element or component for matches. Custom components receive matchIndex and states as extra props.
highlightClassNamestringBase className applied to every match.
highlightStyleCSSPropertiesBase inline style applied to every match.
unhighlightTagkeyof JSX.IntrinsicElementsElement to wrap non-match segments (default: no wrapper).
unhighlightClassNamestringclassName for non-match wrappers (only applied when unhighlightTag is set).
unhighlightStyleCSSPropertiesInline style for non-match wrappers.
renderMatch(seg: MatchSegment, defaults: MatchDefaults) => ReactNodeFull render-prop control over match output.

Root element options

PropTypeDefaultDescription
askeyof JSX.IntrinsicElements'span'Root wrapper element.
classNamestringclassName on the root wrapper.
styleCSSPropertiesInline style on the root wrapper.

Additional HTML attributes are forwarded to the root element.

<Highlight> is wrapped with forwardRef — you can pass a ref and it will be attached to the root element (the as element, default <span>).

Regex defenses

When you pass a RegExp instance in searchWords, <Highlight> does three things to avoid common footguns:

  • Clones the regex so the consumer-supplied object's lastIndex is never mutated (regex objects carry stateful lastIndex when used with g or y flags).
  • Forces the g flag on in the clone so matchAll walks the full string.
  • Drops the sticky y flag from the clone, because anchored sticky matching is incompatible with this component's "find all occurrences" semantics. A one-time console.warn fires in development (NODE_ENV !== 'production') when a sticky regex is passed.

The original regex you passed in is never modified.