Signature
Import
About this widget
ThehierarchicalMenu
widget displays a hierarchical navigation menu, based on facet attributes.
To create a hierarchical menu:
- Decide on an appropriate facet hierarchy
- Determine your
attributes
for faceting from the dashboard or with an API client - Display the UI with the hierarchical menu widget.
Requirements
The objects to use in the hierarchical menu must follow this structure:JSON
JSON
>
(with spaces), but you can use a different one by using the separator
option.
By default, the count of the refined root level is updated to match the count
of the actively refined parent level. Keep the root level count intact by
setting
persistHierarchicalRootCount
in instantsearch
.Examples
JavaScript
Options
The CSS Selector or
HTMLElement
to insert the widget into.The name of the attributes to generate the menu with.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
Whether to display a button that expands the number of items.
JavaScript
The maximum number of displayed items (only used when
showMore
is set to true
).JavaScript
The level separator used in the records.
JavaScript
The prefix path to use if the first level is not the root level.Make sure to also include the root path in your UI state,
for example, by setting
initialUiState
or calling setUiState()
.JavaScript
Whether to show the siblings of the selected parent level of the current refined value.This option doesn’t impact the root level. All root items are always visible.
JavaScript
How to sort refinements. Must be one or more 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"
Array.sort
.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.childList
. The child list element.item
. The list items.selectedItem
. The selected item of the list.parentItem
. The parent item of the list.link
. The link of each item.selectedItemLink
. The link of each selected item.label
. The label of each item.count
. The count of each item.showMore
. The “Show more” button.disabledShowMore
. The disabled “Show more” button.
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.The template for each item. It exposes:
label: string
. The label of the item.value: string
. The value of the item.count: number
. The number of results matching the value.isRefined: boolean
Whether the item is selected.url: string
. The URL with the applied refinement.
The template for the “Show more” button text. It exposes:
isShowingMore: boolean
. Whether the list is expanded.
HTML output
HTML
Customize the UI with connectHierarchicalMenu
If you want to create your own UI of the hierarchicalMenu
widget, you can use connectors.
To use connectHierarchicalMenu
, 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.
Rendering options
The list of available items, with each item:
label: string
. The label of the item.value: string
. The value of the item.count: number
. The number results matching this value.isRefined: boolean
. Whether the item is selected.data: object[]|null
. The list of children for the current item.
JavaScript
Whether the list is expanded.
JavaScript
Whether users can click the “Show more” button.
JavaScript
Indicates if search state can be refined.
JavaScript
Sets the path of the hierarchical filter and triggers a new search.
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
insights
middleware. eventType: 'click'
facetValue: string
JavaScript
Toggles the number of displayed values between
limit
and showMoreLimit
.JavaScript
Generates a URL for the next state.
JavaScript
All original widget options are 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 attributes to generate the menu with.To avoid unexpected behavior, you can’t use the same
attribute
prop in a different type of widget.JavaScript
The number of facet values to retrieve.
When
isShowingMore
is false
,
this is the number of facet values displayed before clicking the “Show more” button.JavaScript
The maximum number of displayed items (only used when the
showMore
feature is implemented).JavaScript
The level separator used in the records.
JavaScript
The prefix path to use if the first level is not the root level.Make sure to also include the root path in your UI state,
for example, by setting
initialUiState
or calling setUiState()
.JavaScript
Whether to show the siblings of the selected parent level of the current refined value.
JavaScript
How to sort refinements. Must be one or more of the following strings:
"count:asc"
"count:desc"
"name:asc"
"name:desc"
"isRefined"
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