select HTML element.
The code that uses the default slots to extend the menu widget can be found in src/components/MenuSelect.vue.
This code has been specifically created for Vue 2.
Some modifications may be required for it to work correctly in Vue 3.
Highlight and snippet your search results
Search helps users understand what they’re seeing in the results. When users run a , they should be able to tell why a result matches. Algolia highlights the matched text in each result. Algolia can also generate snippets: short excerpts from longer text, truncated to a fixed length around the match. Highlighting is based on the results and you need to customize theais-hits widget to use the Highlighter. The ais-highlight and the ais-snippet widgets take two props:
attribute: the path to the highlighted attribute of the hit (which can be either a string or an array of strings)hit: a single result object
Usage notes
- Use the
ais-highlightwidget when you want to display the regular value of an attribute. - Use the
ais-snippetwidget when you want to display the snippet version of an attribute. To use this widget, the attribute name passed to theattributeprop must be present in “Attributes to snippet” on the Algolia dashboard or configured asattributesToSnippetwith aset settingscall to the Algolia API.
Example
This example uses the item slot of the Hit widget. In the results thename field is highlighted.
These examples use the mark tag to highlight.
This is a tag specially made for highlighting pieces of text.
Vue
Style your widgets
All Vue InstantSearch widgets use a set of conventional CSS classes compatible with the InstantSearch CSS themes. If you don’t want to use the themes, you can either write your own theme by following the existing classes and customizing the styles, or override the classes to pass yours instead.Load the InstantSearch theme
Vue InstantSearch doesn’t inject any styles by default but comes with two optional themes: Algolia and Satellite.From a CDN
The themes are available on jsDelivr. Unminified:https://cdn.jsdelivr.net/npm/instantsearch.css@8.11.0/themes/reset.csshttps://cdn.jsdelivr.net/npm/instantsearch.css@8.11.0/themes/algolia.csshttps://cdn.jsdelivr.net/npm/instantsearch.css@8.11.0/themes/satellite.css
https://cdn.jsdelivr.net/npm/instantsearch.css@8.11.0/themes/reset-min.csshttps://cdn.jsdelivr.net/npm/instantsearch.css@8.11.0/themes/algolia-min.csshttps://cdn.jsdelivr.net/npm/instantsearch.css@8.11.0/themes/satellite-min.css
reset style sheet is included by default in the Satellite theme,
so you don’t need to import it separately.
Either copy the files into your own app or use a direct link to jsDelivr.
HTML
Using npm
If your project imports CSS into JavaScript files using build tools, you can install the theme with npm.JavaScript
Translate your widgets
Vue InstantSearch doesn’t have a dedicated API for translating text, but it has slots in every component. When there’s data needed for rendering this is provided too. For example inais-pagination:
Vue
Templating
Vue InstantSearch supports templating with slots. With them you can customize the markup of each sub part of your components. For example inais-pagination:
Vue
Change the list of items in widgets
Vue InstantSearch provides two APIs for manipulating lists:sortBy, this is the most straightforward API and obviously limited to sortingtransformItems, this is the most flexible solutions but it requires more involvement on your side
transform-items prop is a function that takes the whole list of items as a parameter and it will expect to receive in return another array of items. Most of the examples in this guide will use this API.
Sorting
Using sortBy
Vue
Using transformItems
Vue
Filtering
This example filters out items when the count is lower than 150:Vue
Add manual values
By default, the values in aais-refinement-list or a ais-menu are dynamic. This means that the values are updated with the context of the search. Most of the time this is the expected behavior, but sometimes you may want to have a static list of values that never change.
This example, uses a static list of values:
Vue
Display facets with no matches
Showing facets that don’t match the current query helps users explore and refine results. Algolia returns only facet values that match the current query. To show non-matching values, add frontend logic. For example, you could cache facet values the first time you receive them. Then, if Algolia returns fewer values than your facet limit, append the cached values. This approach has limitations:- It doesn’t work with search for facet values because Algolia doesn’t return non-matching values. As a result, highlighting doesn’t work for cached values.
- You may need to re-apply sorting in the
transformItemsfunction because InstantSearch applies its internal sorting before calling the function.
Vue
Search long lists of facet values
Use thesearchable prop to add a search box to supported widgets:
Vue
Apply default value to widgets
A question that comes up frequently is “How to instantiate aais-refinement-list widget with a pre-selected item?”. For this use case, you can use the ais-configure widget.
The following example instantiates a search page with a default query of “apple” and will show a category menu where the item “Cell Phones” is already selected:
Vue
Provide search parameters
Algolia has a wide range of parameters. If one of the parameters you want to use isn’t covered by any widget, then you should use theais-configure widget.
Here’s an example configuring the number of results per page:
Vue
Dynamic update of search parameters
Updating the props of theais-configure widget will dynamically change the search parameters and will trigger a new search.
Filter your results without widgets
Widgets already provide a lot of different ways to filter your results but sometimes you might have more complicated use cases that require thefilters search parameter.
Vue
Customize the complete UI of the widgets
Extending Vue InstantSearch widgets is the second layer of the API. Read about the two others possibilities in the “What’s InstantSearch?” guide. Unlike other flavors of InstantSearch, Vue InstantSearch doesn’t provide connectors. Instead, each widget provides a top level slot to allow for complete customization while being able to reuse the logic of the widget.When to extend widgets?
Extending widgets means being able to redefine the rendering output of an existing widget. For example, if you want to render the Menu widget as an HTMLselect element, you must extend the Menu widget.
Here are some common examples that require the usage of the connectors API:
- When you want to display the widgets using another UI library
- When you want to have full control on the rendering without having to re-implement business logic
- As soon as you hit a feature wall using the default widgets
How widgets are built
Vue InstantSearch widgets have two parts:- Business logic code
- Rendering code
connectors.
Connectors are provided by InstantSearch.js and their interfaces are exposed through a scope on the default slot.
Connectors render API
The aim is to share as much of a common API between all connectors as possible. Once you know how to use one connector, you can use them all.Slot scopes
Most of the connectors will use the same naming for properties passed down to the slot.items[]: array of items to display, for example the brands list of a custom Refinement List. Every extended widget displaying a list gets an items property to the data passed to its render function.refine(value|item.value): will refine the current suiStateof the widget. Examples include: updating the query for a custom SearchBox or selecting a new item in a custom RefinementList.currentRefinement: currently applied refinement value (usually the call value of refine()).createURL(value|item.value): will return a full URL you can display for the specific refine value given you are using the routing feature.
items arrays. The shape of those objects is always the same.
item.value: The underlying precomputed state value to pass torefineorcreateURLitem.label: The label to display, for example “Samsung”item.count: The number of hits matching this itemitem.isRefined: Is the item currently selected as a filter
ais-menu widget.
Extending widget example
You can change the complete rendering with thedefault slot of every widget.
Here’s an example of that for ais-menu:
Vue