https://github.com/github/combobox-nav
Attach combobox navigation behavior to <input> or <textarea>.
https://github.com/github/combobox-nav
autocomplete combobox decorator
Last synced: 9 months ago
JSON representation
Attach combobox navigation behavior to <input> or <textarea>.
- Host: GitHub
- URL: https://github.com/github/combobox-nav
- Owner: github
- License: mit
- Created: 2018-10-11T20:10:35.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-03-11T18:47:09.000Z (10 months ago)
- Last Synced: 2025-04-03T00:03:39.487Z (9 months ago)
- Topics: autocomplete, combobox, decorator
- Language: JavaScript
- Homepage: https://github.github.io/combobox-nav/examples/index.html
- Size: 999 KB
- Stars: 125
- Watchers: 238
- Forks: 20
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# Combobox Navigation
Attach [combobox navigation behavior (ARIA 1.2)](https://www.w3.org/TR/wai-aria-1.2/#combobox) to ``.
## Installation
```
$ npm install @github/combobox-nav
```
## Usage
### HTML
```html
Robot
- Baymax
BB-8- Hubot
- R2-D2
```
Markup requirements:
- Each option needs to have `role="option"` and a unique `id`
- The list should have `role="listbox"`
### JS
```js
import Combobox from '@github/combobox-nav'
const input = document.querySelector('#robot-input')
const list = document.querySelector('#list-id')
// install combobox pattern on a given input and listbox
const combobox = new Combobox(input, list)
// when options appear, start intercepting keyboard events for navigation
combobox.start()
// when options disappear, stop intercepting keyboard events for navigation
combobox.stop()
// move selection to the nth+1 item in the list
combobox.navigate(1)
// reset selection
combobox.clearSelection()
// uninstall combobox pattern from the input
combobox.destroy()
```
## Events
A bubbling `combobox-commit` event is fired on the list element when an option is selected via keyboard or click.
For example, autocomplete when an option is selected:
```js
list.addEventListener('combobox-commit', function (event) {
console.log('Element selected: ', event.target)
})
```
> **Note** When using `` + `` as options, please listen on `change` instead of `combobox-commit`.
When a label is clicked on, `click` event is fired from both `` and its associated input `label.control`. Since combobox does not know about the control, `combobox-commit` cannot be used as an indicator of the item's selection state.
A bubbling `combobox-select` event is fired on the list element when an option is selected but not yet committed.
For example, autocomplete when an option is selected but not yet committed:
```js
list.addEventListener('combobox-select', function (event) {
console.log('Element selected : ', event.target)
})
```
## Settings
For advanced configuration, the constructor takes an optional third argument. For example:
```js
const combobox = new Combobox(input, list, {tabInsertsSuggestions: true})
```
These settings are available:
- `tabInsertsSuggestions: boolean = true` - Control whether the highlighted suggestion is inserted when Tab is pressed (Enter will always insert a suggestion regardless of this setting). When `true`, tab-navigation will be hijacked when open (which can have negative impacts on accessibility) but the combobox will more closely imitate a native IDE experience.
- `firstOptionSelectionMode: FirstOptionSelectionMode = 'none'` - This option dictates the default behaviour when no options have been selected yet and the user presses Enter. The following values of `FirstOptionSelectionMode` will do the following:
- `'none'`: Don't auto-select the first option at all.
- `'active'`: Place the first option in an 'active' state where it is not selected (is not the `aria-activedescendant`) but will still be applied if the user presses `Enter`. To select the second item, the user would need to press the down arrow twice. This approach allows quick application of selections without disrupting screen reader users.
> **Warning** Screen readers will not announce that the first item is the default. This should be announced explicitly with the use of `aria-live` status
- `'selected'`: Select the first item by navigating to it. This allows quick application of selections and makes it faster to select the second item, but can be disruptive or confusing for screen reader users.
- `scrollIntoViewOptions?: boolean | ScrollIntoViewOptions = undefined` - When
controlling the element marked `[aria-selected="true"]` with keyboard navigation, the selected element will be scrolled into the viewport by a call to [Element.scrollIntoView][]. Configure this value to control the scrolling behavior (either with a `boolean` or a [ScrollIntoViewOptions][] object.
[Element.scrollIntoView]: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
[ScrollIntoViewOptions]: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView#sect1
## Development
```
npm install
npm test
```