Signature
Import
JavaScript
About this widget
UseCompareBar to let users compare products with an Agent Studio agent.
The widget renders a comparison bar with the products users selected for comparison and a Compare button.
When users select Compare,
the widget opens the Chat widget on the same index and sends a comparison request with the selected records attached.
The widget clears the selection after sending the request.
The chat displays the agent’s comparison of the attached records without showing the request as a user message.
The widget doesn’t include controls for selecting products.
Add a Compare checkbox to each product in your Hits component.
Use useInstantSearch().indexRenderState.compare to read and update the shared selection, as shown in the example.
To build a custom comparison bar or Compare button, use the useCompare Hook.
All CompareBar components and useCompare Hooks in the same InstantSearch component share the same selection,
so the toggles and comparison bar update together.
The comparison bar stays hidden while the selection is empty.
The comparison bar is an entry point for the chat, so you don’t need a ChatTrigger widget when you use both CompareBar and Chat.
The widget requires a comparison configuration, which controls the comparison goal and instructions.
Create it in the Agent Studio dashboard and pass its ID as configurationId.
See also: Compare products in chat
Examples
JavaScript
Props
string
required
The ID of the comparison configuration that controls how the agent compares products.
Create comparison configurations in the Components section of your agent in the Agent Studio dashboard.
For more information, see Compare products in chat.The widget sends
__ALGOLIA_COMPARISON_<configurationId>__ as the user message,
with comparison_configuration_id and selected_products as per-turn context.
selected_products holds the selected records as a JSON string, with internal attributes such as _highlightResult removed.Agent Studio replaces the placeholder with the configured agent instructions, and the chat doesn’t show a user message for it.
If the configuration doesn’t exist or is turned off,
the placeholder reaches the agent unchanged, so only set the ID of an enabled configuration.JavaScript
number
default:"2"
The minimum number of products users must select before they can compare.
The Compare button is unavailable until users select this number of products.
The value must be at least
2.Set minItems to the Minimum products value in your comparison configuration.JavaScript
number
default:"3"
The maximum number of products users can select.
When the selection reaches this limit, the widget ignores attempts to add more products and shows a hint.
The value must be greater than or equal to
minItems.Set maxItems to the Maximum products value in your comparison configuration.JavaScript
(items: CompareItem[]) => CompareItem[]
A function that receives the selected records and returns the records to display in the comparison bar.
The widget sends these transformed records to the agent when users select Compare.
Use this function to remove long attributes, such as product descriptions.Return one record for each selected record and preserve its
objectID.
The selection itself, including isSelected and the limits, uses the original records.JavaScript
(props: { item: CompareItem }) => JSX.Element
A component to replace the label of each selected item.
It receives the selected record as the
item prop.By default, the label is the record’s name or title, falling back to its objectID.JavaScript
Partial<CompareBarClassNames>
The CSS classes you can override and pass to the widget’s elements.
It’s useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.
root. The root element of the widget.header. The header element.title. The title element.count. The selection count element.list. The list of selected items.item. Each selected item.itemLabel. The label of each selected item.itemRemove. The button that removes an item from the selection.hint. The hint shown when users select fewer thanminItemsor reachmaxItems.actions. The container of the action buttons.clearButton. The button that clears the selection.compareButton. The button that starts the comparison.
JavaScript
Partial<CompareBarTranslations>
A dictionary of translations to customize the UI text and support internationalization.
title(string). The title of the comparison bar. The default isCompare.countText((count: number, maxItems: number) => string). The selection count. The default is${count} of ${maxItems}.compareButtonText((count: number) => string). The label of the Compare button. The default isCompare ${count}.clearButtonText(string). The label of the Clear all button.removeItemLabel((itemLabel: string) => string). The accessible label of each remove button. The default isRemove ${itemLabel} from comparison.minItemsHint((minItems: number) => string). The hint shown while the selection is belowminItems. The default isSelect at least ${minItems} products to compare.limitReachedHint(string). The hint shown when the selection reachesmaxItems. The default isCompare limit reached — remove a product to add another..
JavaScript
Hook
Use theuseCompare Hook to build a custom comparison bar or Compare button.
It accepts the same props as CompareBar: configurationId (required), minItems, maxItems, and transformItems.
It returns:
items(CompareItem[]). The selected records, in selection order, aftertransformItems. Each item has at least anobjectID.isSelected((objectID: string) => boolean). Whether the record with thisobjectIDis in the selection.addItem((item: CompareItem) => void). Adds a record to the selection. It doesn’t add the record if it’s already selected or the selection has reachedmaxItems. The label usesnameortitle, falling back toobjectID. Each record must have anobjectID.removeItem((objectID: string) => void). Removes the record with thisobjectIDfrom the selection.toggleItem((item: CompareItem) => void). Adds the record to the selection when it isn’t selected, and removes it otherwise.clearItems(() => void). Empties the selection.canAddItems(boolean). Whether the selection is belowmaxItems.canCompare(boolean). Whether the selection has reachedminItems.minItems(number). The resolvedminItemsvalue.maxItems(number). The resolvedmaxItemsvalue.compare(() => boolean). Opens theChatwidget on the same index, sends the compare request with the selected records attached, and clears the selection. Returnstruewhen it submits the message andfalsewhen the selection is belowminItems, the page doesn’t have aChatwidget, or the chat is busy.
useCompare Hook and CompareBar component in the same InstantSearch shares the selection.
Pass the same configurationId, minItems, and maxItems to each so they use the same configuration and limits.
Each useCompare call mounts a widget, so for a toggle on every hit, read the selection from useInstantSearch().indexRenderState.compare instead, as in the example.
JavaScript