Skip to content

CSS

A searchable dropdown has no classes of its own. It is a Field whose input is a combobox, with a Dropdown panel holding a List of options. The examples below show the panel open; the Recipe wires it up.

<div class="tng-field">
<div class="tng-field-control">
<label
class="tng-field-control-label"
for="searchable-dropdown-default"
>
City
</label>
<input
class="tng-field-control-input"
id="searchable-dropdown-default"
type="text"
role="combobox"
aria-autocomplete="list"
aria-expanded="true"
aria-controls="searchable-dropdown-default-listbox"
aria-activedescendant="searchable-dropdown-default-madrid"
autocomplete="off"
value="Ma"
/>
<button
class="p-absolute tng-icon-button is-ghost"
type="button"
tabindex="-1"
aria-label="Cities"
aria-expanded="true"
aria-controls="searchable-dropdown-default-listbox"
>
<i class="tng-icon icon-chevron-up" aria-hidden="true"></i>
</button>
</div>
</div>
<div class="tng-dropdown">
<ul
class="tng-list tng-has-dividers"
id="searchable-dropdown-default-listbox"
role="listbox"
aria-label="Cities"
>
<li
class="tng-list-option is-active"
id="searchable-dropdown-default-madrid"
role="option"
aria-selected="false"
>
<span>Madrid</span>
</li>
<li class="tng-list-option" role="option" aria-selected="false">
<span>Milan</span>
</li>
<li
class="tng-list-option"
role="option"
aria-selected="false"
hidden
>
<span>Paris</span>
</li>
</ul>
</div>
  • Madrid
  • Milan

A tng-field-control-input with role="combobox". With .is-lg on the label, the label sits inside the empty field and floats above it on focus; keep .is-lg while the input is empty and remove it once it has a value.

A placeholder floats the label as soon as it’s set, so add it when the input gets focus and remove it on blur. It takes the muted foreground colour.

The chevron is a tng-icon-button in the field’s trailing slot. It needs p-absolute, and tabindex="-1" keeps it out of the tab sequence — the input is the only stop. Swap icon-chevron-down for icon-chevron-up while the list is open.

Put a tng-field-info under the control, and swap it for a tng-field-error when validation fails.

<div class="tng-field">
<div class="tng-field-control">
<label
class="tng-field-control-label is-lg"
for="searchable-dropdown-helper"
>
City
</label>
<input
class="tng-field-control-input"
id="searchable-dropdown-helper"
type="text"
role="combobox"
aria-autocomplete="list"
aria-expanded="false"
aria-describedby="searchable-dropdown-helper-info"
autocomplete="off"
/>
<button
class="p-absolute tng-icon-button is-ghost"
type="button"
tabindex="-1"
aria-label="Cities"
aria-expanded="false"
>
<i class="tng-icon icon-chevron-down" aria-hidden="true"></i>
</button>
</div>
<div class="tng-field-info" id="searchable-dropdown-helper-info">
<span>Start typing to filter the list.</span>
</div>
</div>
Start typing to filter the list.

Each option is a tng-list-option with role="option".

When no option matches, show one muted row in place of the options.

<div class="tng-dropdown">
<ul class="tng-list" role="listbox" aria-label="Cities">
<li
class="tng-list-option fg-muted"
role="option"
aria-disabled="true"
>
No results found, please try another city.
</li>
</ul>
</div>
  • No results found, please try another city.

Put hidden on the options that don’t match the query.

.is-active marks the best match, or the option the arrow keys moved to. Set it together with aria-activedescendant on the input — DOM focus never leaves the input, so this is the only focus indicator the option gets.

aria-selected="true" gives the option its selected background.

<div class="tng-dropdown">
<ul
class="tng-list tng-has-dividers"
role="listbox"
aria-label="Cities"
>
<li
class="tng-list-option is-active"
role="option"
aria-selected="false"
>
<span>Madrid</span>
</li>
<li class="tng-list-option" role="option" aria-selected="true">
<span>Milan</span>
</li>
</ul>
</div>
  • Madrid
  • Milan

.is-invalid on the control, or :user-invalid on a required input, shows the error state.

<div class="tng-field">
<div class="tng-field-control is-invalid">
<label
class="tng-field-control-label is-lg"
for="searchable-dropdown-error"
>
City
</label>
<input
class="tng-field-control-input"
id="searchable-dropdown-error"
type="text"
role="combobox"
aria-autocomplete="list"
aria-expanded="false"
aria-invalid="true"
aria-describedby="searchable-dropdown-error-message"
autocomplete="off"
required
/>
<button
class="p-absolute tng-icon-button is-ghost"
type="button"
tabindex="-1"
aria-label="Cities"
aria-expanded="false"
>
<i class="tng-icon icon-chevron-down" aria-hidden="true"></i>
</button>
</div>
<div class="tng-field-error" id="searchable-dropdown-error-message">
<i class="tng-icon icon-error" aria-hidden="true"></i>
<span>Select a city.</span>
</div>
</div>
Select a city.

Disable both the input and the icon button.

<div class="tng-field-control">
<label
class="tng-field-control-label is-lg"
for="searchable-dropdown-disabled"
>
City
</label>
<input
class="tng-field-control-input"
id="searchable-dropdown-disabled"
type="text"
role="combobox"
aria-autocomplete="list"
aria-expanded="false"
autocomplete="off"
disabled
/>
<button
class="p-absolute tng-icon-button is-ghost"
type="button"
tabindex="-1"
aria-label="Cities"
disabled
>
<i class="tng-icon icon-chevron-down" aria-hidden="true"></i>
</button>
</div>
Start typing to filter the list.
  • Amsterdam
  • Antwerp
  • Athens
  • Barcelona
  • Berlin
  • Brussels
  • Budapest
  • Copenhagen
  • Dublin
  • Lisbon
  • Madrid
  • Milan
  • Paris
  • Prague
  • Rome
  • Stockholm
  • Vienna
  • Warsaw
source code