Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

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 creme finds Crème brûlée.
  • RovingController gives the visible matches one tab stop and moves it with the arrows. getItems returns only the visible items, so the arrows skip what the filter hid and sync() 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}`;
		}
	}
}

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.

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.