Sikau

Project: Team workflow · Day 4 · about 40 min

Fast, safe, accessible search

Debounce typing, render results without XSS, announce them, and support Escape.

Three things at once

A live search must be fast (don't run on every keystroke — debounce), safe (a menu name from the server must never become HTML) and accessible (screen-reader users hear how many results there are; Escape clears it). Web essentials, all in one small feature.

The search code

function debounce(fn, ms) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), ms);
  };
}

const input = document.querySelector("#search");
const results = document.querySelector("#results");
const status = document.querySelector("#status");

function show(query) {
  const q = query.trim().toLowerCase();
  results.replaceChildren();
  if (!q) {
    status.textContent = "";
    return;
  }
  const found = MENU.filter((name) => name.toLowerCase().includes(q));
  for (const name of found) {
    const li = document.createElement("li");
    li.textContent = name;
    results.append(li);
  }
  status.textContent = found.length ? `${found.length} results` : "No results";
}

input.addEventListener("input", debounce(() => show(input.value), 150));

input.addEventListener("keydown", (event) => {
  if (event.key === "Escape") {
    input.value = "";
    show("");
  }
});

The rest of this lesson

  • Typing the searchexplain
  • Keyboard and screen reader detailsexplain
  • How many calls?predict
  • Debounced callspredict
  • Build the searchwrite code
  • Badge quizquiz
  • What you builtrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 5 lessons of Project: Team workflow