No description
  • JavaScript 66.6%
  • TypeScript 33.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Marie Lucca 62372e4c5c
Merge pull request #107 from github/dependabot/npm_and_yarn/npm_and_yarn-b3b4accef5
Bump ws from 8.17.1 to 8.21.0 in the npm_and_yarn group across 1 directory
2026-07-14 13:52:52 -04:00
.github/workflows Merge branch 'main' into alert-autofix-2 2026-07-13 15:53:43 -07:00
examples Remove old demo page 2020-04-29 14:20:14 -04:00
src Avoid automatically assigning an ID that collides with an existing ID 2026-05-06 20:43:54 +02:00
test Avoid automatically assigning an ID that collides with an existing ID 2026-05-06 20:43:54 +02:00
.eslintignore Switch to TS and exports only ESmodule 2020-04-23 16:26:28 -04:00
.eslintrc.json Update ESLint config after updating 2022-03-30 16:33:29 +01:00
.gitattributes Set end of line to lf in gitattributes 2022-03-30 16:33:31 +01:00
.gitignore Initial commit 2018-11-06 15:37:49 -05:00
.travis.yml Set env to osx to test ctrl np shortcut 2018-11-08 08:21:49 -05:00
CODEOWNERS move AOR to primer 2022-09-23 18:01:47 +01:00
karma.config.cjs Fix macOS ARM64 ChromeHeadless failure by using pre-installed Google Chrome 2026-03-09 21:34:41 +00:00
LICENSE Initial commit 2018-10-11 13:10:36 -07:00
package-lock.json Bump ws in the npm_and_yarn group across 1 directory 2026-07-13 22:56:26 +00:00
package.json fix: override tmp to 0.2.7 for CI 2026-06-18 20:10:02 +00:00
README.md Update wording 2024-02-22 16:31:14 +00:00
tsconfig.json Switch to TS and exports only ESmodule 2020-04-23 16:26:28 -04:00

Combobox Navigation

Attach combobox navigation behavior (ARIA 1.2) to <input>.

Installation

$ npm install @github/combobox-nav

Usage

HTML

<label>
  Robot
  <input id="robot-input" type="text" />
</label>
<ul role="listbox" id="list-id" hidden>
  <li id="baymax" role="option">Baymax</li>
  <li><del>BB-8</del></li>
  <!-- `role=option` needs to be present for item to be selectable -->
  <li id="hubot" role="option">Hubot</li>
  <li id="r2-d2" role="option">R2-D2</li>
</ul>

Markup requirements:

  • Each option needs to have role="option" and a unique id
  • The list should have role="listbox"

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:

list.addEventListener('combobox-commit', function (event) {
  console.log('Element selected: ', event.target)
})

Note

When using <label> + <input> as options, please listen on change instead of combobox-commit.

When a label is clicked on, click event is fired from both <label> 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:

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:

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.

Development

npm install
npm test