Signature
Import
About this widget
Themenu
widget displays a menu that lets users choose a single value for a specific attribute.
The
menu
widget uses a hierarchical refinement internally, so it can’t refine values that include the default separator (>
).
To support these values, use the hierarchicalMenu
widget instead.Requirements
Theattribute
provided to the widget must be in attributes for faceting,
either on the dashboard or using the attributesForFaceting
parameter with the API.
Examples
JavaScript
Options
The CSS Selector of the DOM element inside which the widget is inserted.
The name of the attribute in the records.To avoid unexpected behavior, you can’t use the same
attribute
prop in a different type of widget.JavaScript
How many facet values to retrieve.
When you enable the
showMore
feature,
this is the number of facet values to display before clicking the “Show more” button.JavaScript
How many facet values to retrieve when showing more.
JavaScript
How to sort refinements. Must be one of the following strings:
"count"
(same as"count:desc"
)"count:asc"
"count:desc"
"name"
(same as"name:asc"
)"name:asc"
"name:desc"
"isRefined"
(same as"isRefined:asc"
)"isRefined:asc"
"isRefined:desc"
["isRefined", "name:asc]
It’s also possible to give a function, which receives items two by two, like JavaScript’s Array.sort
.If facetOrdering
is set for this facet in renderingContent
,
and no value for sortBy
is passed to this widget, facetOrdering
is used,
and the default order as a fallback.The CSS classes you can override:
root
. The root element of the widget.noRefinementRoot
. The root element if there are no refinements.list
. The list of results.item
. The list items. They contain the link and separator.selectedItem
. The selected item in the list. This is the last one, or the root one if there are no refinements.link
. The link element of each item.label
. The label element of each item.count
. The count element of each item.showMore
. The “Show more” button.disabledShowMore
. The “Show more” button when disabled.
JavaScript
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.JavaScript
Templates
You can customize parts of a widget’s UI using the Templates API. Each template includes anhtml
function,
which you can use as a tagged template.
This function safely renders templates as HTML strings and works directly in the browser—no build step required.
For details, see Templating your UI.
The
html
function is available in InstantSearch.js version 4.46.0 or later.Item template. It exposes:
count
. The number of occurrences of the facet in the result set.isRefined
. Returnstrue
if the value is selected.label
. The label to display.value
. The value used for refining.url
. The URL with the selected refinement.cssClasses
. An object containing all the computed classes for the item.
The template for the “Show more” button text. It exposes:
isShowingMore: boolean
. Whether the list is expanded.
HTML output
HTML
Customize the UI with connectMenu
If you want to create your own UI of the menu
widget, you can use connectors.
This connector is also used to build the
menuSelect
widget.connectMenu
, you can import it with the declaration relevant to how you installed InstantSearch.js.
JavaScript
Create a render function
This rendering function is called before the first search (init
lifecycle step)
and each time results come back from Algolia (render
lifecycle step).
JavaScript
If SEO is important for your search page, ensure that your custom HTML is optimized for search engines:
- Use
<a>
tags withhref
attributes to allow search engine bots to follow links. - Use semantic HTML and include structured data when relevant.
Render options
The elements that can be refined for the current search results. With each item:
value: string
. The value of the menu item.label: string
. The label of the menu item.count: number
. The number of results matched after a refinement is applied.isRefined: boolean
. Indicates if the refinement is applied.
JavaScript
Indicates if search state can be refined.
JavaScript
A function to toggle a refinement.
JavaScript
The function to send
click
events.-
The
view
event is automatically sent when the facets are rendered. -
The
click
event is automatically sent whenrefine
is called. - You can learn more about the middleware.
-
eventType: 'click'
-
facetValue: string
JavaScript
Generates a URL for the corresponding search state.
JavaScript
Returns
true
if the menu is displaying all the menu items.JavaScript
Returns
true
if the “Show more” button can be activated (enough items to display more and not already displaying more than limit
items).JavaScript
Toggles the number of displayed values between
limit
and showMoreLimit
.JavaScript
All original widget options forwarded to the render function.
JavaScript
Create and instantiate the custom widget
First, create your custom widgets using a rendering function. Then, instantiate them with parameters. There are two kinds of parameters you can pass:- Instance parameters. Predefined options that configure Algolia’s behavior.
- Custom parameters. Parameters you define to make the widget reusable and adaptable.
renderFunction
, both instance and custom parameters are accessible through connector.widgetParams
.
JavaScript
Instance options
The name of the attribute for faceting.To avoid unexpected behavior, you can’t use the same
attribute
prop in a different type of widget.JavaScript
How many facet values to retrieve. When you enable the
showMore
feature, this is the number of facet
values to display before clicking the “Show more” button.JavaScript
How many facet values to retrieve when showing more.
JavaScript
How to sort refinements. Must be one or more of the following strings:
"count:asc"
"count:desc"
"name:asc"
"name:desc"
"isRefined"
["isRefined", "name:asc"]
It’s also possible to give a function, which receives items two by two, like JavaScript’s Array.sort
.If facetOrdering
is set for this facet in renderingContent
, and no value for sortBy
is passed to this widget, facetOrdering
is used, and the default order as a fallback.A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.JavaScript