Autocomplete Search (debounce + keyboard + outside click)

intermediate
By FrontendAtlas Editorial · Updated Nov 12, 2026
intermediate level
Vue 3 Composition API (ref, computed, watch, onMounted/onBeforeUnmount)Debounced async effectsKeyboard navigation stateOutside click handlingSafe HTML escaping + highlight rendering
Build a Vue 3 autocomplete search input. It should debounce queries, show a dropdown with results, support keyboard navigation (↑/↓/Enter/Esc), and close on outside click. Keep the UI styles exactly as provided.

Requirements

  • Render the autocomplete UI as the main component.
  • Typing filters a fixed city list and shows results in a dropdown.
  • Debounce input by ~300ms before searching.
  • If query is empty (after trim): close dropdown and clear results.
  • If query is non-empty: open dropdown and show results.
  • Show up to 8 results.
  • Keyboard support: ArrowUp/ArrowDown to move active option, Enter to select, Esc to close.
  • Close dropdown on outside click.
  • Highlight the matched substring using <mark> (escape HTML first).
  • Show a 'Loading…' row while async search is pending (avoid flashing 'No results' during loading).

What the checks look for

The checks find elements by control labelled "Search", option, listbox, body, mark, text "No results", text "Loading…". Keep these in your markup.

Expected behavior

  • Focus with non-empty query opens the dropdown.
  • Typing updates results after debounce + simulated latency.
  • Arrow keys cycle through visible options (wrap around).
  • Enter selects the active option, sets input value, closes dropdown.
  • Esc closes dropdown and clears active selection.
  • Clicking an option selects it (mousedown to avoid blur issues).
  • Clicking outside closes dropdown and clears active selection.
  • When loading: dropdown can be open but shows 'Loading…' instead of 'No results'.
  • When not loading and results are empty: show 'No results'.

Implementation notes

  • Use Vue 3 Composition API in <script setup>.
  • Use refs for query/isOpen/activeIndex/results/isLoading.
  • Use a debounced watcher on query to trigger async search.
  • Use stale-result protection (sequence id or cancellation flag) so older searches don’t override newer ones.
  • Use document mousedown listener for outside click.

Solution idea

Use watch(query) with a 300ms timeout to debounce. On non-empty query, open dropdown and set loading true, then call an async search(q) that simulates latency and returns up to 8 matches. Protect against stale updates using a sequence counter. Add keyboard handling to move activeIndex and select results. Use document.addEventListener('mousedown', ...) to close on outside click.

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

  • Taro: Apple front end developer interview, August 2022 · Taro, August 2022

    One self-reported 45-minute virtual interview asked for an autocomplete search bar in TypeScript with Vue or React, plus basic TypeScript and framework concept questions. This component practises the shape, not the reported wording.

  • Code360: Booking.com frontend engineer interview experience, April 2023 · Code360, April 2023

    One account describes a 40-minute round with three engineers: create an autocomplete text box in vanilla JavaScript, HTML and CSS, covering edge cases and must-haves versus nice-to-haves. This starter is a practice version in Vue; the reported round was framework-free.

  • enginebogie: Booking.com frontend engineer machine-coding round, May 2023 · enginebogie, May 2023

    One account describes a 60-minute machine-coding round: a typeahead with API calls, a live dropdown, highlighted matches, debounce or throttle, caching, keyboard navigation and error states. This starter is a practice version of that round in Vue, not its reported wording.

Reported in a public Apple interview account. Apple frontend interview questions

Reported in a public Booking.com interview account. Booking.com frontend interview questions