Signature
Import
- Component
- Plugin
To ensure optimal bundle sizes,
see Optimize build size.
Vue
About this widget
Use theais-compare-bar widget 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 ais-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 ais-hits widget with a custom widget that uses the connectCompare connector, as shown in the example.
To build a custom comparison bar or Compare button, use the same connector.
All ais-compare-bar widgets and connectCompare widgets in the same ais-instant-search share the same selection,
so the toggles and comparison bar update together.
The comparison bar stays hidden while the selection is empty.
Because ais-compare-bar opens the ais-chat widget,
you don’t need an ais-chat-trigger widget.
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 configuration-id.
See also: Compare products in chat
Examples
TheCompareToggle component is a custom widget that renders a Compare checkbox for one hit.
It uses connectCompare with the same configuration and limits as the comparison bar.
CompareToggle.vue
App.vue
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 you turned it off,
the placeholder reaches the agent unchanged, so only set the ID of an enabled configuration.Vue
number
default:"2"
The 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 min-items to the lower product limit in your comparison configuration.Vue
number
default:"3"
The largest 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
min-items.Set max-items to the upper product limit in your comparison configuration.Vue
(items: object[]) => object[]
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.Vue
object
default:"{}"
The CSS classes you can override:
ais-CompareBar. The root element of the widget.ais-CompareBar-header. The header element.ais-CompareBar-title. The title element.ais-CompareBar-count. The selection count element.ais-CompareBar-list. The list of selected items.ais-CompareBar-item. Each selected item.ais-CompareBar-itemLabel. The label of each selected item.ais-CompareBar-itemRemove. The button that removes an item from the selection.ais-CompareBar-hint. The hint shown when users select fewer thanmin-itemsor reachmax-items.ais-CompareBar-actions. The container of the action buttons.ais-CompareBar-clearButton. The button that clears the selection.ais-CompareBar-compareButton. The button that starts the comparison.
Vue
object
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 belowmin-items. The default isSelect at least ${minItems} products to compare.limitReachedHint(string). The hint shown when the selection reachesmax-items. The default isCompare limit reached — remove a product to add another..
Vue
Customize the UI
The slot to replace the label of each selected item.By default, the label is the record’s
name or title, falling back to its objectID.Scopeitem: object. The selected record.
Vue
HTML output
HTML
min-items or at max-items, it adds a div.ais-CompareBar-hint element before the actions.
The instantsearch.css chat theme anchors the bar to the bottom center of the viewport.
Import instantsearch.css/components/chat.css to style it.
Connector render state
A custom widget that usesconnectCompare receives the following properties in state:
items(object[]). 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: object) => void). Adds a record to the selection. It doesn’t add the record if it’s already selected or the selection has reachedmaxItems. Each record must have anobjectID.removeItem((objectID: string) => void). Removes the record with thisobjectIDfrom the selection.toggleItem((item: object) => 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 theais-chatwidget 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 anais-chatwidget, or the chat is busy.
configurationId (required), minItems, maxItems, and transformItems.
Pass the same configurationId, minItems, and maxItems to every connectCompare widget and ais-compare-bar so they use the same configuration and limits.