Render each item
Autocomplete uses a virtual DOM for rendering. This ensures optimal performance even with frequent updates, safeguards against cross-site scripting (XSS) attacks, and supports inline event handling. Templates can return any valid virtual DOM elements (VNodes). For example, templates can return strings, HTML strings, or Preact components.Strings
JavaScript
HTML strings
Templates can return HTML strings using anhtml
tagged template:
JavaScript
Preact components
Templates can return Preact components using JSX:JSX
Autocomplete uses Preact 10 to render templates by default.
It isn’t compatible with earlier versions.
Return HTML
Native HTML elements aren’t valid VNodes, which means you can’t return a template string that contains HTML, or an HTML element. But if you’re not using a virtual DOM implementation in your app, you can still return HTML with the providedhtml
tagged template.
Every Autocomplete template provides an html
function that you can use as a tagged template.
Using html
lets you provide templates as an HTML string
It works directly in the browser without a build step.
The
html
function is only available starting from Autocomplete v1.6.0.JavaScript
Internet Explorer 11 doesn’t support tagged template literals. If you need to support Internet Explorer 11, check out the suggested solutions.
Components and layouts
You can use the provided components in your templates by using their function form or interpolating them.html
function is also exposed in render
and renderNoResults
to customize the panel layout.
Loops and conditional rendering
You can use plain JavaScript to build dynamic templates. For example, useArray.map
to loop over an array and display a list.
JavaScript
Passing a unique
key
attribute is helpful when mapping over items. It helps the virtual DOM keep track of each element when they change, and update the UI efficiently.JavaScript
HTML in variables
Only the HTML provided as a string literal is evaluated. HTML in variables (for example, stored in Algolia records) isn’t supported and is rendered as-is.JavaScript
Internet Explorer 11 support
You can’t usehtml
as a tagged template in Internet Explorer 11.
Depending on your project setup, you can work around this problem to still use html
while providing compatible code to your Internet Explorer 11 users.
With Babel
If you have a build step and are using Babel to compile your code for legacy browsers, you can transform allhtml
expressions into regular function calls.
The recommended setup is to use @babel/preset-env
, which provides this transformation along with other common ones based on a list of browsers to support.
.babelrc
This transform is available as a Babel plugin.
With a shim
If you don’t have a build step in your project, write a . Tagged templates are regular functions with a specific signature, so you can wrap their calls with a friendlier API to avoid using tagged template notation. This function takes templates as static strings or an array of interspersed chunks, splits them, and passes them to thehtml
function.
The documented shim assumes every even array entry is a template string, and every odd entry is a dynamic value.
Change the shim if you need it to do something different.
Further optimizations
The providedhtml
function works in the browser without any build step, with a negligible impact on memory and bundle size (< 600 bytes).
To ensure optimal performance,
use
babel-plugin-htm
to compile HTML tagged templates.
This plugin replaces html
calls, so your code must expose a consistent parameter name to the compiler.
Avoid destructuring directly in the function signature.
Instead, either name the parameter and destructure it inside the function body,
or pass the object as-is.
The parameter name (for example, params
) must be consistent across all templates.JavaScript
.babelrc
If you’re destructuring objects, ensure you also transpile it using
@babel/preset-env
.Return virtual nodes directly
You can return virtual nodes with JSX orcreateElement
.
With JSX
The JSX syntax compiles down to VNodes. If you’re using JSX in your project, you can directly return JSX templates.JSX
By default, Autocomplete uses Preact 10 to render templates.
If you’re using another virtual DOM implementation, you can pass a custom
renderer
.With createElement
Each template function provides access to createElement
and Fragment
to create VNodes.
JavaScript
By default,
createElement
and Fragment
default to Preact’s preact.createElement
(or h
) and preact.Fragment
.
If you’re using another virtual DOM implementation, you can replace them.Render a header and footer
In addition to rendering items, you can customize what to display before and after the list of items using theheader
and footer
templates.
JavaScript
Components
Autocomplete exposescomponents
to all templates to share them everywhere in the instance.
Highlight
to highlight matches in Algolia results.Snippet
to snippet matches in Algolia results.ReverseHighlight
to reverse highlight matches in Algolia results.ReverseSnippet
to reverse highlight and snippet matches in Algolia results.
Highlight and snippet
Templates expose a set of built-incomponents
to handle highlighting and snippeting.
Use these components to highlight or snippet results from the getAlgoliaResults
function.
Render a no-results state
If there are no results, you might want to display a message to inform users or let them know what to do next. Do this with thenoResults
template.
JavaScript