Highlight

Highlights the words in a text that match a search.

Your parcel arrives on Thursday before noon.

Usage

Code
import { Highlight } from '@leverui/components';
Code
<Highlight query="Hello">Hello World</Highlight>

Examples

Multiple

Pass an array of strings to the query prop to highlight multiple substrings.

Bring a helmet, gloves and a water bottle on the ride.

Custom Style

Use the markStyle prop to customize the style of the highlighted text.

The library closes at 6 pm on weekdays.

Search Query

Use the highlight component to highlight search query results.

Search results for: desk

Desktop organizer

Desk lamp

Desk chair

With Squiggle

Here's an example of how to render a custom squiggle image around the highlighted text. Useful for a more fancy effect.

Accessible by default, delightful by design.

Guides

Matching

Highlight adds no wrapper element. Each match is a Mark, a native <mark> element.

A string query marks only its first match by default. An array marks every match. Set matchAll to override either. ignoreCase matches upper and lower case alike.

The child must be a string. Any other value throws an error at runtime.

Styling matches

markClass adds a class to the matched <mark> elements only. markStyle is passed to each match as its style attribute, for one-off values.

Default match CSS: transparent background, the color of the text around it, and white-space: nowrap. It sits in a cascade layer, so your own plain CSS overrides it.

Code
<Highlight query={['Solid', 'fast']} markClass="search-match">
  Solid is great and Solid is fast
</Highlight>
Code
.search-match {
  padding-inline: 2px;
  background-color: var(--leverui-colors-orange-subtle);
  color: var(--leverui-colors-orange-fg);
}

Matches also get the MarkPropsProvider defaults: variants, palettes, classes, and native attributes. markClass is added alongside a provider class. See Mark.

useHighlight

useHighlight takes the matching options and returns a Solid accessor that holds a HighlightChunk[]. Pass the options as a function when the query or text can change. Also takes exactMatch: whole words only.

Code
import { useHighlight } from '@leverui/components';

const chunks = useHighlight(() => ({
  text: title(),
  query: search(),
  ignoreCase: true,
  exactMatch: true,
}));

// Read chunks() inside tracked Solid JSX.

For a squiggle or other custom markup, render the chunks yourself. Wrap only the chunks that have match: true.

Props

PropDefaultTypeDescription
queryRequiredstring | string[]Word or phrase to highlight, or an array.
childrenRequiredstringText to search and highlight.
markClass—stringCSS class added to each matched Mark.
markStyle—JSX.CSSProperties | stringNative style applied to each matched Mark.
ignoreCase—booleanMatch upper and lower case alike.
matchAll—booleanMark every match, or only the first.