Skip to content

CdsHighlight

New in v4.0

Highlighting explains why a row made it into the results: the component finds the matches with the query (query) or with ready ranges (matches) in the text and wraps them into mark. Take it for search results, autocomplete suggestions and filtered lists.

Storybook GitLab
Test coverage: 100% — good coverage
API in specs
9 / 9 props, events, slots and exposed members with a test
Test cases
16 unit · 0 browser
Code coverage
100% lines · 100% branches measured in v4.0.0-alpha.13
A11y (axe)
2 recommendations in 10 stories measured in v4.0.0-alpha.11

In CdsAutocomplete, CdsCombobox and CdsSelect the highlighting of the matches is already built in — a standalone CdsHighlight is not needed there. For a permanent label next to the text take CdsTag, for a counter take CdsBadge.

Variations ​

Search query ​

The text prop takes the source string, query takes what the user was looking for. The query can be a string or an array of terms: all of them get highlighted.

All occurrences ​

By default the first occurrence of every term is highlighted. The match-all prop highlights all of them — in a long text this shows the full picture.

Case ​

The ignore-case prop looks for matches regardless of the case. You need it almost always when the query is typed by the user.

Ready ranges ​

When the matches are found by the server rather than by the browser, pass them into matches — an array of [start, end] pairs by the indexes of the source string. The ranges take precedence over query, and overlapping ones are merged automatically.

Color and density ​

The color prop sets the color of the highlighting — the background is tinted with it too, so a single value is enough. opacity changes the density of the background.

Markup ​

The tag prop sets the tag of the wrapper (span by default), mark-class adds your own class to every match.

Guidelines ​

Scenarios ​

Searching through a list ​

The same query filters the list and highlights the matches in the found rows. match-all and ignore-case keep the highlighting and the filter consistent: once a row made it into the results, the match inside it is visible.

A query of several words ​

Split the typed phrase into words and pass them as an array into query — the component highlights every word separately, even if they stand far apart in the text.

Matches from the server ​

The full-text search on the backend already knows which fragments count as matches: it accounts for morphology, synonyms and typos. Give those ranges to matches — the client will not search anything again.

API ​

Props

NameTypeDefaultDescription
colorstring | undefined—Color of the highlighted text.
ignoreCaseboolean | undefined—Ignores the case when searching by query.
markClassstring | undefined—Extra CSS class for the <mark> element.
matchAllboolean | undefined—Highlights every occurrence, not just the first one.
matchesreadonly MatchRange[] | undefined—Ready match ranges [start, end]. They take precedence over query.
opacitystring | number | undefined—Opacity of the highlight background (the --cds-highlight-opacity CSS variable).
querystring | string[] | undefined—Search query: a string or an array of term strings.
tagstring | JSXComponent<any> | undefined'span'Custom HTML tag or component to render
textstring | undefined''The text the highlighting is performed in.