Searchable list
A search field that narrows a list as you type, and arrow keys that walk what is left.
Goal
A command list, a country picker or a long settings page: type to narrow, press the Down arrow to leave the field and walk the matches, press Enter to choose. The list is always on the page, so there is no popup to open or close.
Primitives
- textContains decides whether an item matches. It ignores case and accents by default, so
cremefindsCrème brûlée. - RovingController gives the visible matches one tab stop and moves it with the arrows.
getItemsreturns only the visible items, so the arrows skip what the filter hid andsync()after each filter keeps the tab stop on a visible item.
It does not use a combobox popup, where a field keeps focus while a popup of options is navigated with aria-activedescendant. A list that is already on the page needs no popup, and moving real focus with the roving controller is less code.
Left to the recipe: reading the query, hiding the items that do not match, the Down arrow that moves from the field to the list, and what choosing does.
Example
Type to filter, press ArrowDown to move into the list, use the arrow keys, and press Enter to choose. Pick an authoring pattern below; they all behave the same, and the one you pick is remembered on the other recipes.
The same behavior as a RadiantElement. The markup is authored inside the element and the element finds its parts with @query; there is nothing to mount, clean up or run again.
- Apple
- Apricot
- Banana
- Cherry
- Date
5 of 5 shown
import { RadiantElement, customElement, onEvent, query } from '@ecopages/radiant';
import { createRovingController } from '@ecopages/radiant-ui/controllers';
import { textContains } from '@ecopages/radiant-ui/helpers';
@customElement('searchable-list-element')
export class SearchableListElement extends RadiantElement {
@query('input') private input!: HTMLInputElement;
@query('ul') private list!: HTMLElement;
@query('[data-status]') private status!: HTMLElement;
private readonly roving = createRovingController(this, {
getItems: () => this.visible,
getTarget: () => this.list,
orientation: 'vertical',
wrap: false,
});
private get all(): HTMLElement[] {
return [...this.list.children] as HTMLElement[];
}
private get visible(): HTMLElement[] {
return this.all.filter((item) => !item.hidden);
}
override onConnected(): void {
this.roving.sync();
}
@onEvent({ selector: 'input', type: 'input' })
filter(): void {
for (const item of this.all) {
item.hidden = !textContains(item.textContent, this.input.value);
}
this.roving.sync();
const shown = this.visible.length;
this.status.textContent = shown === 0 ? 'No matches' : `${shown} of ${this.all.length} shown`;
}
@onEvent({ selector: 'input', type: 'keydown' })
enterList(event: KeyboardEvent): void {
const first = this.visible[0];
if (event.key === 'ArrowDown' && first) {
event.preventDefault();
this.roving.focus(first);
}
}
@onEvent({ selector: 'ul', type: 'keydown' })
choose(event: KeyboardEvent): void {
if (event.key === 'Enter' && event.target instanceof HTMLElement) {
this.status.textContent = `Chose ${event.target.textContent}`;
}
}
}<searchable-list-element>
<label for="fruit-filter">Filter fruit</label>
<input id="fruit-filter" type="search" placeholder="Type a fruit" />
<ul role="listbox" aria-label="Fruit">
<li role="option">Apple</li>
<li role="option">Apricot</li>
<li role="option">Banana</li>
<li role="option">Cherry</li>
<li role="option">Date</li>
</ul>
<p data-status aria-live="polite">5 of 5 shown</p>
</searchable-list-element>:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(label) {
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search'] {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x) var(--space-control-y) calc(var(--space-control-x) + 1.5rem);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low)
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E")
no-repeat var(--space-control-x) center / 1rem;
color: inherit;
font: inherit;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search']:focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
ul {
display: flex;
flex-direction: column;
gap: 0;
margin: 0 calc(var(--space-2) * -1);
padding: 0;
border: 0;
background: transparent;
list-style: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
padding: var(--space-control-y) var(--space-control-x);
border: 0;
border-radius: var(--radius-control);
background: transparent;
transition: background-color var(--duration-fast) var(--ease-out-strong);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li[hidden] {
display: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:hover) {
background: var(--surface-container-high);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:focus-visible) {
outline: 0;
background: color-mix(in srgb, var(--primary) 14%, var(--surface));
color: var(--primary);
font-weight: 600;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where([data-status]) {
margin: 0;
font-size: 0.75rem;
opacity: 0.65;
}
@media (prefers-reduced-motion: reduce) {
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
transition: none;
}
}createRovingController(this, ...) attaches the controller when the element connects and destroys it when the element disconnects, and @onEvent listeners follow the same lifecycle. So removing the element undoes everything, and the router adding a copy on another page starts it again without a mount call. sync() goes in onConnected because the items exist only after the element has its children.
What the Radiant form removes: the mount function and its registration, the cleanup to return and call, and the re-run after a client-side navigation.
The same behavior as a RadiantElement. The markup is authored inside the element and the element finds its parts with @query; there is nothing to mount, clean up or run again.
- Apple
- Apricot
- Banana
- Cherry
- Date
5 of 5 shown
import { RadiantElement, customElement, onEvent, query } from '@ecopages/radiant';
import { createRovingController } from '@ecopages/radiant-ui/controllers';
import { textContains } from '@ecopages/radiant-ui/helpers';
@customElement('searchable-list-element')
export class SearchableListElement extends RadiantElement {
@query('input') private input!: HTMLInputElement;
@query('ul') private list!: HTMLElement;
@query('[data-status]') private status!: HTMLElement;
private readonly roving = createRovingController(this, {
getItems: () => this.visible,
getTarget: () => this.list,
orientation: 'vertical',
wrap: false,
});
private get all(): HTMLElement[] {
return [...this.list.children] as HTMLElement[];
}
private get visible(): HTMLElement[] {
return this.all.filter((item) => !item.hidden);
}
override onConnected(): void {
this.roving.sync();
}
@onEvent({ selector: 'input', type: 'input' })
filter(): void {
for (const item of this.all) {
item.hidden = !textContains(item.textContent, this.input.value);
}
this.roving.sync();
const shown = this.visible.length;
this.status.textContent = shown === 0 ? 'No matches' : `${shown} of ${this.all.length} shown`;
}
@onEvent({ selector: 'input', type: 'keydown' })
enterList(event: KeyboardEvent): void {
const first = this.visible[0];
if (event.key === 'ArrowDown' && first) {
event.preventDefault();
this.roving.focus(first);
}
}
@onEvent({ selector: 'ul', type: 'keydown' })
choose(event: KeyboardEvent): void {
if (event.key === 'Enter' && event.target instanceof HTMLElement) {
this.status.textContent = `Chose ${event.target.textContent}`;
}
}
}<searchable-list-element>
<label for="fruit-filter">Filter fruit</label>
<input id="fruit-filter" type="search" placeholder="Type a fruit" />
<ul role="listbox" aria-label="Fruit">
<li role="option">Apple</li>
<li role="option">Apricot</li>
<li role="option">Banana</li>
<li role="option">Cherry</li>
<li role="option">Date</li>
</ul>
<p data-status aria-live="polite">5 of 5 shown</p>
</searchable-list-element>:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(label) {
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search'] {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x) var(--space-control-y) calc(var(--space-control-x) + 1.5rem);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low)
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E")
no-repeat var(--space-control-x) center / 1rem;
color: inherit;
font: inherit;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search']:focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
ul {
display: flex;
flex-direction: column;
gap: 0;
margin: 0 calc(var(--space-2) * -1);
padding: 0;
border: 0;
background: transparent;
list-style: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
padding: var(--space-control-y) var(--space-control-x);
border: 0;
border-radius: var(--radius-control);
background: transparent;
transition: background-color var(--duration-fast) var(--ease-out-strong);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li[hidden] {
display: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:hover) {
background: var(--surface-container-high);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:focus-visible) {
outline: 0;
background: color-mix(in srgb, var(--primary) 14%, var(--surface));
color: var(--primary);
font-weight: 600;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where([data-status]) {
margin: 0;
font-size: 0.75rem;
opacity: 0.65;
}
@media (prefers-reduced-motion: reduce) {
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
transition: none;
}
}createRovingController(this, ...) attaches the controller when the element connects and destroys it when the element disconnects, and @onEvent listeners follow the same lifecycle. So removing the element undoes everything, and the router adding a copy on another page starts it again without a mount call. sync() goes in onConnected because the items exist only after the element has its children.
What the Radiant form removes: the mount function and its registration, the cleanup to return and call, and the re-run after a client-side navigation.
The same behavior as one self-contained element. The list is not authored on the page: the element holds the fruit and the query as state and draws the label, the field, the rows and the status line in render().
import { RadiantElement, customElement, onEvent, state } from '@ecopages/radiant';
import { createRovingController } from '@ecopages/radiant-ui/controllers';
import { textContains } from '@ecopages/radiant-ui/helpers';
const fruit = ['Apple', 'Apricot', 'Banana', 'Cherry', 'Date'];
@customElement('searchable-list-app')
export class SearchableListApp extends RadiantElement<{ status: string }> {
@state query = '';
@state status = `${fruit.length} of ${fruit.length} shown`;
private readonly roving = createRovingController(this, {
getItems: () => this.items,
getTarget: () => this.querySelector('ul'),
orientation: 'vertical',
wrap: false,
});
private get items(): HTMLElement[] {
return [...this.querySelectorAll<HTMLElement>('li')];
}
private get matching(): string[] {
return fruit.filter((name) => textContains(name, this.query));
}
/**
* Attaches the roving controller to the list the render produced, and gives the tab stop back to its rows.
*
* @remarks
* The controller attaches when the element connects, before the first render, when no list exists yet to listen
* on; a second `attach()` resolves it again and changes nothing once it is attached. A filter replaces the rows,
* so each commit syncs the tab stop.
*/
protected override updated(): void {
this.roving.attach();
this.roving.sync();
}
@onEvent({ selector: 'input', type: 'input' })
filter(event: Event): void {
this.query = (event.target as HTMLInputElement).value;
const shown = this.matching.length;
this.status = shown === 0 ? 'No matches' : `${shown} of ${fruit.length} shown`;
}
@onEvent({ selector: 'input', type: 'keydown' })
enterList(event: KeyboardEvent): void {
const first = this.items[0];
if (event.key === 'ArrowDown' && first) {
event.preventDefault();
this.roving.focus(first);
}
}
@onEvent({ selector: 'ul', type: 'keydown' })
choose(event: KeyboardEvent): void {
if (event.key === 'Enter' && event.target instanceof HTMLElement) {
this.status = `Chose ${event.target.textContent}`;
}
}
override render() {
return (
<>
<label for="fruit-filter">Filter fruit</label>
<input id="fruit-filter" type="search" placeholder="Type a fruit" />
<ul role="listbox" aria-label="Fruit">
{this.matching.map((name) => (
<li key={name} role="option">
{name}
</li>
))}
</ul>
<p data-status aria-live="polite">
{this.$.status}
</p>
</>
);
}
}:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(label) {
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search'] {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x) var(--space-control-y) calc(var(--space-control-x) + 1.5rem);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low)
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E")
no-repeat var(--space-control-x) center / 1rem;
color: inherit;
font: inherit;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search']:focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
ul {
display: flex;
flex-direction: column;
gap: 0;
margin: 0 calc(var(--space-2) * -1);
padding: 0;
border: 0;
background: transparent;
list-style: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
padding: var(--space-control-y) var(--space-control-x);
border: 0;
border-radius: var(--radius-control);
background: transparent;
transition: background-color var(--duration-fast) var(--ease-out-strong);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li[hidden] {
display: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:hover) {
background: var(--surface-container-high);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:focus-visible) {
outline: 0;
background: color-mix(in srgb, var(--primary) 14%, var(--surface));
color: var(--primary);
font-weight: 600;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where([data-status]) {
margin: 0;
font-size: 0.75rem;
opacity: 0.65;
}
@media (prefers-reduced-motion: reduce) {
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
transition: none;
}
}Filtering writes query and status, and render() draws only the matching rows, so a hidden item is no longer an element with hidden set but a row that is not there. A render replaces the rows the roving controller knew, so updated(), which runs after each commit, calls sync() to put the tab stop back on a row that exists. The list is drawn by the first render, after the controller attached on connect and found nothing to listen on, so updated() attaches it again. The input is not bound to state, so typing never replaces the field and focus stays in it.
Compared with the Radiant element above, the markup and the data live inside the element, so there is nothing to keep in step with the page, but restyling the structure means editing the class. It also needs the render path to have content before the script runs: this docs build prerenders the tag empty and the client draws it, so the page shows nothing until the script loads, which a page of authored children does not.
The same behavior as a RadiantController attached to markup that already exists. The root is a plain div carrying data-controller="searchable-list", and the class finds its parts with @query just as the element does.
- Apple
- Apricot
- Banana
- Cherry
- Date
5 of 5 shown
import { RadiantController, controller, onEvent, query } from '@ecopages/radiant';
import { createRovingController } from '@ecopages/radiant-ui/controllers';
import { textContains } from '@ecopages/radiant-ui/helpers';
@controller('searchable-list')
export class SearchableListController extends RadiantController {
@query('input') private input!: HTMLInputElement;
@query('ul') private list!: HTMLElement;
@query('[data-status]') private status!: HTMLElement;
private readonly roving = createRovingController(this, {
getItems: () => this.visible,
getTarget: () => this.list,
orientation: 'vertical',
wrap: false,
});
private get all(): HTMLElement[] {
return [...this.list.children] as HTMLElement[];
}
private get visible(): HTMLElement[] {
return this.all.filter((item) => !item.hidden);
}
override connect(): void {
super.connect();
this.roving.sync();
}
@onEvent({ selector: 'input', type: 'input' })
filter(): void {
for (const item of this.all) {
item.hidden = !textContains(item.textContent, this.input.value);
}
this.roving.sync();
const shown = this.visible.length;
this.status.textContent = shown === 0 ? 'No matches' : `${shown} of ${this.all.length} shown`;
}
@onEvent({ selector: 'input', type: 'keydown' })
enterList(event: KeyboardEvent): void {
const first = this.visible[0];
if (event.key === 'ArrowDown' && first) {
event.preventDefault();
this.roving.focus(first);
}
}
@onEvent({ selector: 'ul', type: 'keydown' })
choose(event: KeyboardEvent): void {
if (event.key === 'Enter' && event.target instanceof HTMLElement) {
this.status.textContent = `Chose ${event.target.textContent}`;
}
}
}<div data-controller="searchable-list">
<label for="fruit-filter">Filter fruit</label>
<input id="fruit-filter" type="search" placeholder="Type a fruit" />
<ul role="listbox" aria-label="Fruit">
<li role="option">Apple</li>
<li role="option">Apricot</li>
<li role="option">Banana</li>
<li role="option">Cherry</li>
<li role="option">Date</li>
</ul>
<p data-status aria-live="polite">5 of 5 shown</p>
</div>:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(label) {
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search'] {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x) var(--space-control-y) calc(var(--space-control-x) + 1.5rem);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low)
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E")
no-repeat var(--space-control-x) center / 1rem;
color: inherit;
font: inherit;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search']:focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
ul {
display: flex;
flex-direction: column;
gap: 0;
margin: 0 calc(var(--space-2) * -1);
padding: 0;
border: 0;
background: transparent;
list-style: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
padding: var(--space-control-y) var(--space-control-x);
border: 0;
border-radius: var(--radius-control);
background: transparent;
transition: background-color var(--duration-fast) var(--ease-out-strong);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li[hidden] {
display: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:hover) {
background: var(--surface-container-high);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:focus-visible) {
outline: 0;
background: color-mix(in srgb, var(--primary) 14%, var(--surface));
color: var(--primary);
font-weight: 600;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where([data-status]) {
margin: 0;
font-size: 0.75rem;
opacity: 0.65;
}
@media (prefers-reduced-motion: reduce) {
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
transition: none;
}
}There is no custom element tag to define, so the markup can come from a server template or a CMS and the class attaches to it through data-controller. One registry that the docs site starts over the document connects it, including copies inserted later. createRovingController(this, ...) takes the controller as its host exactly as it takes the element, so the logic is the element form's, and removing the root runs the same cleanup.
A mount function wires the controllers to one element and returns the cleanup that undoes it.
- Apple
- Apricot
- Banana
- Cherry
- Date
5 of 5 shown
import { RovingController } from '@ecopages/radiant-ui/controllers';
import { textContains } from '@ecopages/radiant-ui/helpers';
export function mount(root: HTMLElement): () => void {
const input = root.querySelector('input')!;
const list = root.querySelector('ul')!;
const status = root.querySelector('[data-status]')!;
const all = [...list.children] as HTMLElement[];
const visible = () => all.filter((item) => !item.hidden);
const roving = new RovingController({
getItems: visible,
getTarget: () => list,
orientation: 'vertical',
wrap: false,
});
roving.attach();
roving.sync();
input.addEventListener('input', () => {
for (const item of all) {
item.hidden = !textContains(item.textContent, input.value);
}
roving.sync();
const shown = visible().length;
status.textContent = shown === 0 ? 'No matches' : `${shown} of ${all.length} shown`;
});
input.addEventListener('keydown', (event) => {
const first = visible()[0];
if (event.key === 'ArrowDown' && first) {
event.preventDefault();
roving.focus(first);
}
});
list.addEventListener('keydown', (event) => {
if (event.key === 'Enter' && event.target instanceof HTMLElement) {
status.textContent = `Chose ${event.target.textContent}`;
}
});
return () => roving.destroy();
}<div data-example="searchable-list">
<label for="fruit-filter">Filter fruit</label>
<input id="fruit-filter" type="search" placeholder="Type a fruit" />
<ul role="listbox" aria-label="Fruit">
<li role="option">Apple</li>
<li role="option">Apricot</li>
<li role="option">Banana</li>
<li role="option">Cherry</li>
<li role="option">Date</li>
</ul>
<p data-status aria-live="polite">5 of 5 shown</p>
</div>:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
) {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--space-stack);
box-sizing: border-box;
width: min(100%, 24rem);
padding: var(--space-inset);
border: 1px solid var(--border);
border-radius: var(--radius-container);
background: var(--surface);
color: var(--on-surface);
box-shadow: var(--shadow-overlay);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(label) {
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.7;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search'] {
box-sizing: border-box;
width: 100%;
min-height: var(--size-control-lg);
padding: var(--space-control-y) var(--space-control-x) var(--space-control-y) calc(var(--space-control-x) + 1.5rem);
border: 1px solid var(--border);
border-radius: var(--radius-control);
background: var(--surface-container-low)
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E")
no-repeat var(--space-control-x) center / 1rem;
color: inherit;
font: inherit;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
input[type='search']:focus-visible {
outline: 2px solid var(--focus-ring);
outline-offset: 1px;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
ul {
display: flex;
flex-direction: column;
gap: 0;
margin: 0 calc(var(--space-2) * -1);
padding: 0;
border: 0;
background: transparent;
list-style: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
padding: var(--space-control-y) var(--space-control-x);
border: 0;
border-radius: var(--radius-control);
background: transparent;
transition: background-color var(--duration-fast) var(--ease-out-strong);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li[hidden] {
display: none;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:hover) {
background: var(--surface-container-high);
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where(li:focus-visible) {
outline: 0;
background: color-mix(in srgb, var(--primary) 14%, var(--surface));
color: var(--primary);
font-weight: 600;
}
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
:where([data-status]) {
margin: 0;
font-size: 0.75rem;
opacity: 0.65;
}
@media (prefers-reduced-motion: reduce) {
:is(
searchable-list-element,
searchable-list-app,
div[data-controller='searchable-list'],
div[data-example='searchable-list']:not(.primitive-example)
)
li {
transition: none;
}
}mount runs once for each example element on the page, and the function it returns undoes it; see how the examples run.
Styles
The stylesheet is the searchable-list.css tab in the files above. It styles the field, the rows and the status from the theme variables, and marks the row that holds the tab stop with :focus-visible, so the walked row needs no class the script would have to keep in step. The selectors are scoped to the example on this site; drop the scope in your own project.