Working example
This demo is working example of a menu widget rendered as aselect
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 is all about helping users understand the results. This is especially true when using text-based search. When a user types a query in the search box, the results must show why the results are matching the query. That’s why Algolia implements a powerful highlight that lets you display the matching parts of text attributes in the results. 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-highlight
widget when you want to display the regular value of an attribute. - Use the
ais-snippet
widget when you want to display the snippet version of an attribute. To use this widget, the attribute name passed to theattribute
prop must be present in “Attributes to snippet” on the Algolia dashboard or configured asattributesToSnippet
with aset settings
call 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
The widgets are shipped with fixed CSS class names. The format for those class names isais-NameOfWidget-element--modifier
. This follows the naming convention defined by SUIT CSS.
The different class names used by each widget are described on their respective documentation pages. You
can also inspect the underlying DOM and style accordingly.
Load the theme
No CSS is loaded into your page automatically but there are two themes that you can load manually:reset.css
satellite.css
reset.css
to avoid visual side effects.
The reset
theme CSS is included within the satellite
CSS,
so there is no need to import it separately when you are using the satellite
theme.
From a CDN
The themes are available on jsDelivr: Unminified:- https://cdn.jsdelivr.net/npm/instantsearch.css@8.5.1/themes/reset.css
- https://cdn.jsdelivr.net/npm/instantsearch.css@8.5.1/themes/satellite.css
- https://cdn.jsdelivr.net/npm/instantsearch.css@8.5.1/themes/reset-min.css
- https://cdn.jsdelivr.net/npm/instantsearch.css@8.5.1/themes/satellite-min.css
HTML
With yarn (npm) and webpack
If you are using Vue CLI, you can import these styles without a separate loader.JavaScript
JavaScript
Other bundlers
Any other module bundler like Browserify or Parcel can be used to load Algolia’s CSS. Vue InstantSearch doesn’t rely on any specific module bundler or module loader.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
Modify 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 without matches
Hiding facets when they don’t match a query can be counter-intuitive. However, because of the way Algolia handles faceting, you have to rely on workarounds on the frontend to display facets when they have no hits. One way of displaying facets with no matches is by caching the results the first time you receive them. Then, if the amount of real facet hits that Algolia returns is below the limit set, you can append the cached facets to the list. This solution comes with limitations:- Facet hits coming from a facet search (“Search for facet values”) can’t work because Algolia doesn’t return facets that don’t match (the highlighting won’t work on cached items).
- Sorting might need to be applied again in the
transformItems
function because the internal sorting happens before this function is called
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.
Don’t use filters on a attribute already used with a widget, it will conflict.
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 suiState
of 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 torefine
orcreateURL
item.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