Component reference
Options and API
Reference for option data, configuration, events, and keyboard behavior.
Option shape
Every option needs a string or numeric value (or valeur) and a string label. Optional icon values accept Web Awesome icon names or descriptors in the form {name, family?, variant?}, SVG/PNG URL strings, or image descriptors in the form {src, fallback?}. The fallback text appears if the image cannot load. Other properties, such as department, region, and country, are preserved as option metadata.
{
"value": "paris",
"label": "Paris",
"icon": "location-dot",
"department": "75",
"region": "Île-de-France",
"country": "France"
}
Local options
Provide local choices in either HTML or JavaScript. Direct wa-option children are read as the initial list. Assigning an array to field.options is useful for generated data and replaces any child options on that field. Visible suggestions are rendered with Web Awesome wa-option elements, including results from remote search.
Static choices with wa-option
Set value on each child. Its trimmed text becomes the visible label unless a label attribute is provided. Use data-icon for an icon and other data-* attributes for string metadata; dashed names become camel-case keys on the option.
<lgs1920-autocomplete id="markup-city-search" label="City" name="city">
<wa-option value="paris" data-icon="location-dot" data-department="75" data-region="Île-de-France" data-country="France">Paris</wa-option>
<wa-option value="reims" data-icon="city" data-department="51" data-region="Grand Est" data-country="France">Reims</wa-option>
</lgs1920-autocomplete>
JavaScript-managed choices
Use an empty autocomplete element when the list comes from JavaScript. Each object uses the same option shape, and its additional fields remain available in the selected option and selection events.
<lgs1920-autocomplete id="js-city-search" label="City" name="city"></lgs1920-autocomplete>
const field = document.querySelector('#js-city-search')
field.options = [
{value: 'paris', label: 'Paris', icon: 'location-dot', department: '75', region: 'Île-de-France', country: 'France'},
{value: 'reims', label: 'Reims', icon: 'city', department: '51', region: 'Grand Est', country: 'France'},
]
Properties and attributes
Use the listed names as HTML attributes. In JavaScript, hyphenated attribute names become camel-case properties, such as max-options-visible and maxOptionsVisible.
| Name | Type | Default | Description |
|---|---|---|---|
options | Option[] property | [] | Local suggestions. Direct wa-option children provide initial choices; assigning this property replaces them and supports JavaScript-managed data. Matching checks label and value, without case sensitivity, and highlights matching text in labels. When recent suggestions are open, the disclosure chevron replaces them with the complete local list and points up. Activate it again to close the list. With an empty query and no recent suggestions, the chevron opens the full list directly. With multiple, options can be toggled without closing it. |
renderOption | Function property | null | Synchronous renderer (option, {query}) => Node for custom suggestion content. It replaces the default label rendering; option.label remains the search text, selected input text, and accessible name. Option icons still follow icon-position. Return a fresh, non-interactive DOM node for each suggestion; set to null to restore the default label view. |
optionTemplate / option-template | HTML template property / template ID attribute | null | Selects an HTML template for custom suggestion content, either by assigning the template element or its ID in the attribute. Declare the template before the autocomplete element. It replaces the default label rendering and query highlighting; option.label still controls matching, selected text, and the accessible name. Text-node placeholders use {{field}} syntax and dot-separated paths; values are inserted as plain text. Setting either renderer clears the other. |
searchOptions | Function property | null | Async provider (query, {signal}) => Promise<Option[]>. Setting it enables remote search. |
persistence | {id?, group?, limit} property | null | Requires an id or a group. Saves the last selected options under an id-specific key, or under a shared group key when group is set. Components with the same group share their recent history. count aliases limit. |
value | String, number, or array property | '' or [] | Raw selected value. With multiple, this is an array. Setting it selects matching options already available to the component; it does not start a remote search. |
query | Read-only string property | '' | Current text in the input, including while the user is typing an unselected query. |
selectedOption | Read-only Option or null property | null | The selected object in single mode; use selectedOptions in multiple mode. |
selectedOptions | Read-only Option[] property | [] | All selected option objects, including host-owned metadata. In single mode it contains zero or one option. |
focus() | Method | — | Focus the underlying Web Awesome input. |
label | String attribute/property | '' | Accessible visible label forwarded to wa-input. |
placeholder | String attribute/property | '' | Hint displayed while the query is empty and, in multiple mode, no options are selected. |
name | String attribute/property | '' | Form field name for selected values. Multiple selection submits repeated entries under this name. |
disabled | Boolean attribute/property | false | Disables the input and excludes its value from form submission. |
with-clear | Boolean attribute / withClear property | false | Adds an accessible query-clear control. In multiple mode, a separate button clears all selected tags. |
multiple | Boolean attribute/property | false | Allows several options to be selected. Adding an option clears the current query and keeps the panel open. Local search shows the complete list; remote search keeps its current results available until the next query. The placeholder stays hidden while tags are selected. Values are submitted as repeated form entries under the same name. |
max-options-visible | Non-negative number attribute / maxOptionsVisible property | 5 | Maximum selected tags shown before a +N control opens the full selection list, paginated 10 items per page. Set to 0 to show every tag inline. |
size | xs, s, m, l, xl | m | Forwards the Web Awesome input size. The small, medium, and large aliases are also supported. |
appearance | filled, outlined, or filled-outlined | outlined | Forwards the Web Awesome input appearance. |
pill | Boolean attribute/property | false | Forwards pill-style rounding to the Web Awesome input. |
hint | String attribute/property | '' | Forwards hint text to the Web Awesome input. |
readonly | Boolean attribute/property | false | Makes the query field read-only while keeping suggestions selectable. |
icon-position | Attribute / iconPosition property: start or end | start | Places each option icon before or after its label in suggestions and, in multiple mode, selected tags. |
min-chars | Number attribute / minChars property | 1 | Minimum non-empty query length before invoking the async provider. Set to 0 to allow any non-empty query; an empty query never invokes the provider. |
debounce | Milliseconds attribute/property | 150 | Wait after the latest keystroke before invoking the async provider. New input restarts the delay and aborts any stale request. |
page-size | Positive number attribute / pageSize property | 10 | Suggestions shown per page. When more than one page is available, a compact footer shows the current result range and a compact page counter with previous/next controls. |
max-results | Positive number attribute / maxResults property | 50 | Maximum filtered local or remote results. Opening the complete local list from the disclosure chevron includes every option. |
Custom suggestion content
Set renderOption to return a fresh DOM node for each suggestion. This example renders location metadata in a wa-card. The option label still controls searching, the selected input value, and the accessible option name; custom content does not receive the default query highlighting.
field.renderOption = option => {
const card = document.createElement('wa-card')
card.setAttribute('appearance', 'outlined')
for (const [label, value] of [
['City', option.city],
['Department', option.department],
['Region', option.region],
['Country', option.country],
]) {
if (!value) continue
const line = document.createElement('div')
line.textContent = `${label}: ${value}`
card.append(line)
}
return card
}
For layouts that only need markup and field insertions, use an HTML template. It replaces the default label rendering and query highlighting. Put {{field}} placeholders in text nodes; dot-separated paths are supported, and values are inserted as plain text. Reference the template ID with option-template, or assign it through field.optionTemplate when configuring the field in JavaScript.
<template id="city-option-template">
<wa-card appearance="outlined">
<strong>{{city}}</strong>
<small>{{region}} · {{country}}</small>
</wa-card>
</template>
<lgs1920-autocomplete id="templated-city-search" label="City" option-template="city-option-template"></lgs1920-autocomplete>
const field = document.querySelector('#templated-city-search')
field.options = [
{value: 'paris', label: 'Paris', city: 'Paris', region: 'Île-de-France', country: 'France'},
{value: 'reims', label: 'Reims', city: 'Reims', region: 'Grand Est', country: 'France'},
]
Option icons
Set icon on each option to show an icon beside its label in suggestions and selected tags. The component accepts several icon sources; you can mix them in one options array. icon-position="start" places them before labels, and icon-position="end" places them after. In mixed suggestion lists, options without icons reserve the same slot to keep their labels aligned. Lists with no icons do not reserve extra space.
| Source | Option value | How it works |
|---|---|---|
| Web Awesome icon | icon: 'location-dot' | Uses the default Web Awesome icon library. A descriptor such as {name: 'house', family: 'classic', variant: 'solid'} sets the icon properties explicitly. See wa-icon. |
| Local SVG or PNG | icon: '/assets/icons/home.svg' | A string ending in .svg or .png is rendered as an image. |
| Remote SVG or PNG | icon: 'https://flagcdn.io/fr.svg' | Pass the image URL directly when it ends in .svg or .png. |
| Blob, data, or extensionless URL | icon: {src: imageUrl, fallback: '🇫🇷'} | Use an image descriptor when the URL has no file extension, or when you want fallback text if image loading fails. |
Here the options use different formats together. For a URL from a remote API, map its image URL to {src} and provide optional fallback text:
field.options = [
{value: 'paris', label: 'Paris', icon: 'location-dot'},
{value: 'home', label: 'Home', icon: '/assets/icons/home.svg'},
{value: 'france', label: 'France', icon: {src: 'https://flagcdn.io/fr.svg', fallback: '🇫🇷'}},
]
field.searchOptions = async (query, {signal}) => {
const response = await fetch('/api/search?q=' + encodeURIComponent(query), {signal})
const results = await response.json()
return results.map(item => ({
value: item.id,
label: item.name,
icon: item.iconUrl
? {src: item.iconUrl, fallback: item.iconEmoji}
: 'location-dot',
}))
}
For files bundled by Vite, Vite glob imports resolve SVG/PNG files to URLs. The autocomplete component itself does not depend on Vite.
const iconUrls = import.meta.glob('./icons/*.{svg,png}', {
eager: true,
import: 'default',
query: '?url',
})
field.options = Object.entries(iconUrls).map(([path, src]) => ({
value: path,
label: path.split('/').pop(),
icon: {src},
}))
Input slots
Use the start and end slots for fixed content inside the input, such as a search icon. Slots decorate the input; they do not set icons for individual suggestions. For those, use the option's icon property. The disclosure button remains in the end area.
<lgs1920-autocomplete label="City" placeholder="Search cities">
<wa-icon slot="start" name="magnifying-glass" aria-hidden="true"></wa-icon>
</lgs1920-autocomplete>
Recent selections
Use a stable, unique id for an independent history, or set the same group on multiple components to share recent selections. The browser stores selected option objects as JSON in localStorage, and shows the last selected items when the query is empty. With no recent selections, the panel stays closed instead of listing every option. Only a user selection updates the history.
field.persistence = {id: 'city-search', limit: 5}
firstField.persistence = {group: 'location-search', limit: 5}
secondField.persistence = {group: 'location-search', limit: 5}
// The count alias is also accepted:
field.persistence = {id: 'city-search', count: 5}
Changing the id when no group is set, or changing the group, loads a separate history. When both are set, group determines the shared history. Reducing the limit removes older entries; raising it later cannot restore removed entries. Browser storage or JSON serialization failures emit wa-persistence-error with {error, action, id, group?}.
Styling
| CSS custom property | Default | Description |
|---|---|---|
--lgs1920-autocomplete-max-height | 16rem | Maximum height of the suggestion panel. |
--lgs1920-autocomplete-placeholder-color | var(--wa-color-text-quiet) | Input placeholder color. |
--lgs1920-autocomplete-panel-background | var(--wa-color-surface-raised) | Suggestion and selected-options panel background. |
--lgs1920-autocomplete-panel-color | var(--wa-color-text-normal) | Suggestion and selected-options panel text color. |
--lgs1920-autocomplete-panel-border-color | var(--wa-color-surface-border) | Suggestion and selected-options panel border color. |
--lgs1920-autocomplete-option-color | var(--wa-color-text-normal) | Option text color. |
--lgs1920-autocomplete-option-icon-color | var(--wa-color-text-quiet) | Option icon color. |
--lgs1920-autocomplete-option-active-background | var(--wa-color-neutral-fill-quiet) | Keyboard-active option background. |
--lgs1920-autocomplete-option-hover-background | var(--wa-color-neutral-fill-quiet) | Pointer-hover option background. |
--lgs1920-autocomplete-option-selected-background | var(--wa-color-brand-fill-quiet) | Selected option background. |
--lgs1920-autocomplete-option-selected-color | var(--wa-color-brand-on-quiet) | Selected option text color. |
The component follows the active Web Awesome theme by default. Blend an accent token with the active surface to tint individual states while preserving light and dark themes.
lgs1920-autocomplete {
/* Keep the suggestion panel on the active theme surface. */
--lgs1920-autocomplete-panel-background: color-mix(in srgb, var(--wa-color-surface-raised) 88%, var(--wa-color-purple) 12%);
/* Follow the active theme's foreground color. */
--lgs1920-autocomplete-panel-color: var(--wa-color-text-normal);
/* Tint hovered options without replacing the theme surface. */
--lgs1920-autocomplete-option-hover-background: color-mix(in srgb, var(--wa-color-surface-raised) 86%, var(--wa-color-purple) 14%);
/* Keep selected text readable on the tinted surface. */
--lgs1920-autocomplete-option-selected-background: color-mix(in srgb, var(--wa-color-surface-raised) 68%, var(--wa-color-purple) 32%);
/* Follow the active theme's foreground color. */
--lgs1920-autocomplete-option-selected-color: var(--wa-color-text-normal);
}
Remote API example
The callback receives an abort signal. Forward it to fetch so the browser can stop obsolete requests as the query changes.
field.searchOptions = async (query, {signal}) => {
const response = await fetch(`/api/cities?q=${encodeURIComponent(query)}`, {signal})
if (!response.ok) throw new Error('City search failed')
return response.json()
}
The endpoint should return an array in the documented option shape. The live city example uses Photon, filters results to cities, and maps each result to a stable OpenStreetMap identifier plus its coordinates and country metadata. Photon’s public demo server is intended for reasonable request volumes and does not provide an availability guarantee. A failed request shows a status message and emits wa-search-error. The selected value participates in form submission and is cleared when the owning form resets.
Events
| Event | Detail | When |
|---|---|---|
input | Standard component event | Whenever the user edits the query. Read its text from event.target.query. |
change | Standard component event | When the selected value changes or the input value is committed. Read the selected value from event.target.value. |
focus / blur | Standard component events | When the underlying input receives or loses focus. |
wa-clear | Custom component event | When the query-clear or clear-all control is activated. |
wa-select | {option, value, label, selectedOptions} | After a suggestion is selected. In multiple mode, value is an array. |
wa-deselect | {option, value, label, selectedOptions} | When a selected option is toggled off in multiple mode. |
wa-search-error | {error, query} | When a current async search fails. |
wa-option-render-error | {error, option} | When renderOption throws, returns something other than a DOM node, or optionTemplate cannot be rendered. The default label view is used for that suggestion. |
wa-option-template-error | {error, templateId} | When the option-template attribute does not reference an HTML <template> element in the same document. |
wa-persistence-error | {error, action, id, group?} | When browser storage cannot be read or written. For a group-only configuration, id contains the group name. |
Keyboard and accessibility
| Key | Behavior |
|---|---|
| Arrow Down / Arrow Up | Move the active suggestion, wrapping at either end. |
| Home / End | Move to the first or last result, changing pages if needed. |
| Enter | Select the active suggestion. |
Enter / Space on +N | Open the expanded selected-options list. |
| Escape | Close the open suggestion or selected-options panel. |
| Tab | Continue normal focus navigation; the panel closes when focus leaves. |