DocsMenus & navigation
Get started

Search Subtly documentation

Search by a task, helper name or framework.

Menus & navigation

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.

Actual npm package · local demonstration

Add it to your project

  1. 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.
  2. 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.
  3. 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.