Attach combobox navigation behavior to or .</body></html>
Перейти к файлу
Kristján Oddsson abb436761f Set end of line to `lf` in gitattributes
See https://prettier.io/docs/en/options.html#end-of-line
2022-03-30 16:33:31 +01:00
.github/workflows Create .github/workflows/publish.yml 2022-03-25 19:21:00 +00:00
examples Remove old demo page 2020-04-29 14:20:14 -04:00
src Prettier 2022-03-30 16:33:31 +01:00
test Prettier 2022-03-30 16:33:31 +01: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
LICENSE Initial commit 2018-10-11 13:10:36 -07:00
README.md Add markup requirements 2020-09-18 12:31:22 -04:00
karma.config.cjs configure karma to use chromium 2022-01-29 17:24:35 +00:00
package-lock.json Update lint, test and compiler tools to their latest version 2022-03-30 16:33:26 +01:00
package.json Update ESLint config after updating 2022-03-30 16:33:29 +01:00
tsconfig.json Switch to TS and exports only ESmodule 2020-04-23 16:26:28 -04:00

README.md

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.

Development

npm install
npm test