Giter VIP home page Giter VIP logo

combobox-nav's Introduction

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

combobox-nav's People

Contributors

anleac avatar bnjamin avatar broccolinisoup avatar camertron avatar colinwm avatar dependabot[bot] avatar dgraham avatar feelepxyz avatar iansan5653 avatar jfuchs avatar keithamus avatar koddsson avatar latentflip avatar manuelpuyol avatar muan avatar nicowenterodt avatar seanpdoyle avatar siddharthkp avatar theinterned avatar wxiaoguang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

combobox-nav's Issues

example not working

Hi, I noticed the example isn't working. When I focus the input field this error message pops up in the console:

Uncaught TypeError: comboboxNav.stop is not a function
    at HTMLInputElement.toggleList ((index):33)

This happens on Chrome/Firefox/Opera/Safari OSX.

image

Click by mouse not fire commit

In example click by mouse on list item not fire commit event.
That's because blur event executes before click event. Solution: change click listener to mousedown listener.

v2.3.0 broke the UX of text-expander (for me)

A quick report that v2.3.0 and the new scrollIntoView broke the UX of @github/text-expander for me. It would push the expanded <ul> to the top of the screen. After spending a few hours debugging what's the core of the issue, I noticed that the undesirable UX happens after the combobox-nav dependency of text-expander is upgraded to v2.3.0.

If someone else is facing this issue, my solution was to fix combobox-nav to v2.2.0 through "resolutions" section of package.json.

Element.scrollIntoView scrolls entire page when combobox is contained within position: sticky element

#75 replaced scrolling logic with Element.scrollIntoView. However, this causes the entire page to scroll when the combobox is contained with a position: sticky element.

Here is a simple reproduction:
https://stackblitz.com/edit/js-ktdbvw

Observe that when you navigate through the combobox options with the Up/Down Arrow keys, the entire page scrolls towards the bottom.

Tested in Chrome and Safari.

Flaky MacOS test suite

The CI test suite intermittently fails on MacOS due to what looks like a race condition:

npm ERR! code 1
npm ERR! path /Users/runner/work/combobox-nav/combobox-nav/node_modules/chromium
npm ERR! command failed
npm ERR! command sh -c node install.js
npm ERR! Step 1. Retrieving Chromium revision number
npm ERR! Step 2. Downloading Chromium revision 1025666
npm ERR! /Users/runner/work/combobox-nav/combobox-nav/node_modules/p-cancelable/index.js:48
npm ERR! 					throw new Error('The `onCancel` handler was attached after the promise settled.');
npm ERR! 					      ^
npm ERR! 
npm ERR! Error: The `onCancel` handler was attached after the promise settled.
npm ERR!     at onCancel (/Users/runner/work/combobox-nav/combobox-nav/node_modules/p-cancelable/index.js:48:12)
npm ERR!     at makeRequest (/Users/runner/work/combobox-nav/combobox-nav/node_modules/got/dist/source/as-promise/index.js:38:13)
npm ERR!     at Request.<anonymous> (/Users/runner/work/combobox-nav/combobox-nav/node_modules/got/dist/source/as-promise/index.js:143:17)
npm ERR!     at Object.onceWrapper (node:events:642:26)
npm ERR!     at Request.emit (node:events:527:28)
npm ERR!     at Timeout.retry (/Users/runner/work/combobox-nav/combobox-nav/node_modules/got/dist/source/core/index.js:1278:30)
npm ERR!     at listOnTimeout (node:internal/timers:55[9](https://github.com/github/combobox-nav/runs/7410232560?check_suite_focus=true#step:4:10):17)
npm ERR!     at processTimers (node:internal/timers:502:7)
npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/runner/.npm/_logs/2022-07-19T[13](https://github.com/github/combobox-nav/runs/7410232560?check_suite_focus=true#step:4:14)_32_24_7[14](https://github.com/github/combobox-nav/runs/7410232560?check_suite_focus=true#step:4:15)Z-debug-0.log

This doesn't happen on Ubuntu/Windows.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.