typeahead()
Find a menu item by typing.
Try the example
Focus a destination, then type “p” to jump to Poland. Press “p” again to cycle to Portugal. Type “net” quickly to reach Netherlands. Arrow keys also work.
Focus a destination, then type its name.
Add it to your project
- Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
- Connect the behavior. Copy the JavaScript tab and call its exported
mount(element)after your component’s elements exist. Replace simulated delays and data operations with your own application code. - Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.
API and lifecycle
Provide a root plus items() returning enabled visible options. Typed characters match item-label prefixes; repeated letters cycle matches. The default buffer timeout is 600 ms. label(item) can customize the searchable text.
- Interface
root, items(), label, timeout; returns cleanup- Lifecycle
- The return value is a cleanup function for the keydown listener. Update items() when options change; the helper does not manage selection or ARIA roles.
Accessibility and safety
Implementation notes
Initialize after the DOM is available. Keep native controls and keyboard interactions intact; release listeners and timers on unmount.
Use on a custom menu or listbox, not a text input. The helper skips composition and editable targets; combine it with the widget's own arrow-key navigation.
What to check
Type a full prefix, repeat a letter to cycle and wait for the buffer to reset. Check disabled options and text-entry fields are not intercepted.