Searches the text projected into its default slot and highlights the matches. It uses the native CSS Custom Highlight API, so it does not change the DOM.

The component supports case-sensitive matching and programmatic navigation between matches. Navigation scrolls the active match into view. When the content changes, the component searches it again and keeps the active match where it can.

Element
igc-highlight
Slots
default — The content to search. Only content projected here takes part in the matching.

Example

Basic usage - wrap your text and set the search-text attribute:

<igc-highlight search-text="world">
  <p>Hello, world! The world is a wonderful place.</p>
</igc-highlight>

Whether the matching distinguishes upper and lower case. When true, only exact-case occurrences of searchText are highlighted.

caseSensitive: boolean = false

Defined in src/components/highlight/highlight.ts:104

The string to search and highlight in the DOM content of the component. A change of this property starts a new search. An empty string clears all highlights.

searchText: string = ''

Defined in src/components/highlight/highlight.ts:115

Array of styles to apply to the element. The styles should be defined using the css tag function, via constructible stylesheets, or imported from native CSS module scripts.

Note on Content Security Policy:

Element styles are implemented with <style> tags when the browser doesn't support adopted StyleSheets. To use such <style> tags with the style-src CSP directive, the style-src value must either include 'unsafe-inline' or nonce-<base64-value> with <base64-value> replaced be a server-generated nonce.

To provide a nonce to use on generated <style> elements, set window.litNonce to a server-generated nonce in your page's HTML, before loading application code:

``

styles: CSSResult[]

Defined in src/components/highlight/highlight.ts:72

The zero-based index of the active match. Returns 0 when there are no matches.

get current(): number

Defined in src/components/highlight/highlight.ts:123

Returns number

The total number of matches found for the current searchText. Returns 0 when there are no matches or searchText is empty.

get size(): number

Defined in src/components/highlight/highlight.ts:118

Returns number

Moves the active highlight to the next match. Wraps around to the first match after the last one.

next(options: HighlightNavigation): void

Defined in src/components/highlight/highlight.ts:143

Parameters

  • options: HighlightNavigation

    Optional navigation options (e.g. preventScroll).

Returns void

Moves the active highlight to the previous match. Wraps around to the last match when going back from the first one.

previous(options: HighlightNavigation): void

Defined in src/components/highlight/highlight.ts:153

Parameters

  • options: HighlightNavigation

    Optional navigation options (e.g. preventScroll).

Returns void

Re-runs the highlight search based on the current searchText and caseSensitive values, and makes the first match active.

The component also searches again by itself after its content changes.

search(): void

Defined in src/components/highlight/highlight.ts:173

Returns void

Moves the active highlight to the match at the specified zero-based index.

setActive(index: number, options: HighlightNavigation): void

Defined in src/components/highlight/highlight.ts:163

Parameters

  • index: number

    The zero-based index of the match to activate.

  • options: HighlightNavigation

    Optional navigation options (e.g. preventScroll).

Returns void