UI libraries / InstantSearch.js / Widgets
Signature
components.Highlight({
  attribute: string,
  hit: object,
  // Optional parameters
  highlightedTagName: string,
  nonHighlightedTagName: string,
  separator: string,
  cssClasses: object,
});

About this widget

Displays the highlighted attributes of your search results. It isn’t a widget but a component available from the hits or infiniteHits widgets templates.

Examples

1
2
3
4
5
6
7
8
9
10
11
hits({
  container: '#hits',
  templates: {
    item(hit, { html, components }) {
      return html`
        <h2>${components.Highlight({ attribute: 'name', hit })}</h2>
        <p>${hit.description}</p>
      `;
    },
  },
});

Options

attribute
type: string
Required

The highlighted attribute.

Specify a dot-separated value for nested objects, like actor.name

1
2
3
4
components.Highlight({
  // ...
  attribute: 'actor.name',
});
hit
type: object
Required

The original hit object provided to the function. It contains all the data for that result, including the highlighted parts.

The hit object must contain a _highlightResult[attribute].value property.

1
2
3
4
components.Highlight({
  // ...
  hit: item,
});
highlightedTagName
type: string
default: "mark"
Optional

The HTML element that wraps the highlight.

1
2
3
4
components.Highlight({
  // ...
  highlightedTagName: 'em',
});
nonHighlightedTagName
type: string
default: "span"
Optional

The HTML element that wraps the non-highlighted parts of the string.

1
2
3
4
components.Highlight({
  // ...
  nonHighlightedTagName: 'div',
});
separator
type: string
default: ", "
Optional

The character between each item when the attribute to highlight is an array.

1
2
3
4
components.Highlight({
  // ...
  separator: ' - ',
});
cssClasses
type: object
default: {}
Optional

The CSS classes you can override:

  • root: the component’s root element.
  • highlighted: the highlighted parts.
  • nonHighlighted: the non-highlighted parts.
  • separator: the separator elements between highlighted parts.
1
2
3
4
5
6
7
8
9
10
components.Highlight({
  // ...
  cssClasses: {
    root: 'MyCustomHighlight',
    highlighted: [
      'MyCustomHighlightPart',
      'MyCustomHighlightPart--subclass',
    ],
  },
});

HTML output

1
<span>This is the <mark class="ais-Highlight-highlighted">highlighted text</mark></span>
Did you find this page helpful?