A search box that opens a panel with recent and trending searches as soon as it gets focus. It is built with plain CSS and vanilla JavaScript and works well for help centers, documentation sites, stores and dashboards where people often repeat the same queries.
How it works
The root .rs-search wraps a form with role="search". The .rs-input is an ARIA combobox: aria-expanded tells assistive technology whether the .rs-panel is open, and aria-controls points to the .rs-listbox. Inside the listbox there are two groups: .rs-group--recent, rendered from an array by the script, and .rs-group--trending, a static list with numbered .rs-rank badges.
The script opens the panel on focus and click, and filters both groups on every input event. Arrow keys move a virtual highlight, the .is-active class, while focus stays in the field; aria-activedescendant points to the highlighted option. Enter on an option runs the search, Delete removes a highlighted recent search, and Escape closes the panel. Clicking the .rs-remove cross removes one entry, .rs-clear empties the list, and .rs-nomatch appears when nothing matches.
A search is saved to the top of the recent list, duplicates are removed, and the list is cut to data-rs-max items. The list is stored in localStorage when it is available. Every storage call is wrapped in try/catch, so sandboxed iframes and private windows fall back to the items written in the markup. The search itself is simulated: replace runSearch() with a fetch() call or a redirect to your results page.
Customization
data-rs-maxon.rs-search: how many recent searches are kept, 5 by default.data-rs-storage-key: thelocalStoragekey. Use a different key per search box so their histories stay separate.- Remove
data-rs-open-on-loadso the panel opens only when the field is focused. It is set here so the preview shows the panel. --rs-accentinstyle.css: the orange used for the focus shadow, rank badges, Clear all and the active option bar.--rs-width: the maximum width of the search box and panel.- Edit the
liitems in.rs-group--trendingto change trending searches; keepdata-valuein sync with the text.
Use cases
- Ecommerce headers: focusing the search box opens recent and trending searches
- Documentation sites: arrow-key navigation moves through suggestions without the mouse
- Content apps: remove buttons and Clear all let users manage their search history
- Marketplaces: trending terms help visitors who are not sure what to type
Accessibility
The input uses role="combobox" with aria-autocomplete="list", options use role="option" with aria-selected, and each group is labelled with aria-labelledby. A visually hidden .rs-hint linked through aria-describedby explains the arrow keys and the Delete shortcut. The .rs-status live region announces searches and removals. Buttons have visible focus outlines, and the drop-in animation is disabled with prefers-reduced-motion.
Usage notes
Several search boxes can share a page: give each input and listbox unique ids, and the script scopes everything to its own .rs-search.