Add keyboard navigation to your existing client-side search interface.
Перейти к файлу
Zeke Sikelianos 0b8b261a1f
fix: only hijack up and down arrows when query exists (#5)
2018-06-20 14:29:49 -07:00
cypress fix: superfluous change to trigger new release (#3) 2018-06-18 13:55:05 -07:00
script first working version 2018-06-01 21:22:39 +02:00
.gitignore first working version 2018-06-01 21:22:39 +02:00
.travis.yml fix: prevent window scrolling on arrow up and down (#2) 2018-06-18 13:11:33 -07:00
cypress.json first working version 2018-06-01 21:22:39 +02:00
demo.css fix: only hijack up and down arrows when query exists (#5) 2018-06-20 14:29:49 -07:00
demo.js fix: only hijack up and down arrows when query exists (#5) 2018-06-20 14:29:49 -07:00
index.js fix: only hijack up and down arrows when query exists (#5) 2018-06-20 14:29:49 -07:00
package.json fix: prevent window scrolling on arrow up and down (#2) 2018-06-18 13:11:33 -07:00
readme.md add readme 2018-06-03 21:44:48 +02:00

readme.md

search-with-your-keyboard

Add keyboard navigation to your existing client-side search interface.

This module is designed to make Algolia InstantSearch results navigable with a keyboard. It's not Algolia-specific though, and should work with any search setup so long as it includes a search input and a list of client-side-updated results.

Behavior

Key Action
/ Focuses the search input.
esc Focuses the search input and clears it.
down Adds an active class to the next result.
up Adds an active class to the previous result. If already on the first search result, the search input is focused.
enter Sets window.location to the href of the first <a> tag in the current .active result, if present.

Installation

npm install search-with-your-keyboard

Usage

const searchWithYourKeyboard = require('search-with-your-keyboard')

searchWithYourKeyboard('#search-input', '.ais-hits--item')  

Styles

You should specify styles for the .active class on your results, to make the currently active result visible to the user. You may want this to be the same style as :hovering on a result:

.ais-hits--item:hover, .ais-hits--item.active {
  background-color: #F0F0F0;
}

API

searchWithYourKeyboard(inputSelector, hitsSelector)

Arguments:

  • inputSelector String (required) - A CSS selector used to find the search input, e.g. #search-input
  • hitsSelector String (required) - A CSS selector used to find all results, e.g. .ais-hits--item

Tests

npm install
npm test

License

MIT