├── .gitattributes ├── .gitignore ├── CODEOWNERS ├── .eslintignore ├── .travis.yml ├── test ├── .eslintrc.json └── test.js ├── tsconfig.json ├── .eslintrc.json ├── karma.config.cjs ├── .github └── workflows │ ├── nodejs.yml │ └── publish.yml ├── LICENSE ├── package.json ├── examples └── index.html ├── README.md └── src └── index.ts /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @github/primer-reviewers 2 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | prettier.config.js 3 | karma.config.js 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: osx 2 | language: node_js 3 | sudo: required 4 | node_js: 5 | - "node" 6 | addons: 7 | chrome: stable 8 | cache: 9 | directories: 10 | - node_modules 11 | -------------------------------------------------------------------------------- /test/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "espree", 3 | "parserOptions": { 4 | "ecmaVersion": 8 5 | }, 6 | "env": { 7 | "mocha": true 8 | }, 9 | "globals": { 10 | "assert": true 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "esnext", 4 | "target": "es2017", 5 | "strict": true, 6 | "declaration": true, 7 | "outDir": "dist", 8 | "removeComments": true 9 | }, 10 | "files": [ 11 | "src/index.ts" 12 | ] 13 | } -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": ["github"], 3 | "extends": [ 4 | "plugin:github/browser", 5 | "plugin:github/recommended", 6 | "plugin:github/typescript" 7 | ], 8 | "globals": { 9 | "Combobox": "readable" 10 | }, 11 | "overrides": [ 12 | { 13 | "files": "test/**/*.js", 14 | "rules": { 15 | "github/no-inner-html": "off", 16 | "github/unescaped-html-literal": "off", 17 | "import/extensions": "off", 18 | "import/no-unresolved": "off" 19 | } 20 | } 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /karma.config.cjs: -------------------------------------------------------------------------------- 1 | process.env.CHROME_BIN = require('chromium').path 2 | 3 | module.exports = function(config) { 4 | config.set({ 5 | frameworks: ['mocha', 'chai'], 6 | files: [ 7 | {pattern: 'dist/index.js', type: 'module'}, 8 | {pattern: 'test/test.js', type: 'module'} 9 | ], 10 | reporters: ['mocha'], 11 | port: 9876, 12 | colors: true, 13 | logLevel: config.LOG_INFO, 14 | browsers: ['ChromeHeadless'], 15 | autoWatch: false, 16 | singleRun: true, 17 | concurrency: Infinity 18 | }) 19 | } 20 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | build: 13 | runs-on: ${{ matrix.os }} 14 | 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | os: [ubuntu-22.04, windows-latest, macos-latest] 19 | 20 | steps: 21 | - uses: actions/checkout@v1 22 | - name: Use Node.js 18.x 23 | uses: actions/setup-node@v1 24 | with: 25 | node-version: 18.x 26 | - name: npm install, build, and test 27 | run: | 28 | npm install 29 | npm run build --if-present 30 | npm test 31 | env: 32 | CI: true 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 GitHub 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@github/combobox-nav", 3 | "description": "Attach combobox navigation behavior to an input.", 4 | "version": "2.1.5", 5 | "main": "dist/index.js", 6 | "module": "dist/index.js", 7 | "type": "module", 8 | "types": "dist/index.d.ts", 9 | "license": "MIT", 10 | "repository": "github/combobox-nav", 11 | "prettier": "@github/prettier-config", 12 | "files": [ 13 | "dist" 14 | ], 15 | "scripts": { 16 | "clean": "rm -rf dist", 17 | "lint": "eslint .", 18 | "prebuild": "npm run clean && npm run lint && mkdir dist", 19 | "build": "tsc", 20 | "test": "karma start karma.config.cjs", 21 | "pretest": "npm run build", 22 | "prepublishOnly": "npm run build" 23 | }, 24 | "devDependencies": { 25 | "@github/prettier-config": "^0.0.6", 26 | "chai": "^4.3.9", 27 | "chromium": "^3.0.3", 28 | "eslint": "^8.50.0", 29 | "eslint-plugin-github": "^4.10.1", 30 | "karma": "^6.4.2", 31 | "karma-chai": "^0.1.0", 32 | "karma-chrome-launcher": "^3.2.0", 33 | "karma-mocha": "^2.0.1", 34 | "karma-mocha-reporter": "^2.2.5", 35 | "mocha": "^10.2.0", 36 | "typescript": "^5.2.2" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | publish-npm: 9 | name: Publish to npm 10 | runs-on: ubuntu-latest 11 | permissions: 12 | contents: read 13 | id-token: write 14 | steps: 15 | - uses: actions/checkout@v3 16 | - uses: actions/setup-node@v3 17 | with: 18 | node-version: 18 19 | registry-url: https://registry.npmjs.org/ 20 | cache: npm 21 | - run: npm ci 22 | - run: npm test 23 | - run: npm version ${TAG_NAME} --git-tag-version=false 24 | env: 25 | TAG_NAME: ${{ github.event.release.tag_name }} 26 | # Install latest version of npm for publishing with provenance 27 | - run: npm install -g npm 28 | - run: npm whoami; npm --ignore-scripts publish --provenance --access public 29 | env: 30 | NODE_AUTH_TOKEN: ${{secrets.npm_token}} 31 | publish-github: 32 | name: Publish to GitHub Packages 33 | runs-on: ubuntu-latest 34 | permissions: 35 | contents: read 36 | packages: write 37 | steps: 38 | - uses: actions/checkout@v3 39 | - uses: actions/setup-node@v3 40 | with: 41 | node-version: 18 42 | registry-url: https://npm.pkg.github.com 43 | cache: npm 44 | scope: '@github' 45 | - run: npm ci 46 | - run: npm test 47 | - run: npm version ${TAG_NAME} --git-tag-version=false 48 | env: 49 | TAG_NAME: ${{ github.event.release.tag_name }} 50 | - run: npm --ignore-scripts publish --access public 51 | env: 52 | NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 53 | -------------------------------------------------------------------------------- /examples/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | combobox-nav demo 6 | 7 | 8 | 9 |
10 | 14 | 21 |
22 |

23 | 
24 |   
61 | 
62 | 
63 | 


--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
  1 | # Combobox Navigation
  2 | 
  3 | Attach [combobox navigation behavior (ARIA 1.2)](https://www.w3.org/TR/wai-aria-1.2/#combobox) to ``.
  4 | 
  5 | ## Installation
  6 | 
  7 | ```
  8 | $ npm install @github/combobox-nav
  9 | ```
 10 | 
 11 | ## Usage
 12 | 
 13 | ### HTML
 14 | 
 15 | ```html
 16 | 
 20 | 
 27 | ```
 28 | 
 29 | Markup requirements:
 30 | 
 31 | - Each option needs to have `role="option"` and a unique `id`
 32 | - The list should have `role="listbox"`
 33 | 
 34 | ### JS
 35 | 
 36 | ```js
 37 | import Combobox from '@github/combobox-nav'
 38 | const input = document.querySelector('#robot-input')
 39 | const list = document.querySelector('#list-id')
 40 | 
 41 | // install combobox pattern on a given input and listbox
 42 | const combobox = new Combobox(input, list)
 43 | // when options appear, start intercepting keyboard events for navigation
 44 | combobox.start()
 45 | // when options disappear, stop intercepting keyboard events for navigation
 46 | combobox.stop()
 47 | 
 48 | // move selection to the nth+1 item in the list
 49 | combobox.navigate(1)
 50 | // reset selection
 51 | combobox.clearSelection()
 52 | // uninstall combobox pattern from the input
 53 | combobox.destroy()
 54 | ```
 55 | 
 56 | ## Events
 57 | 
 58 | A bubbling `combobox-commit` event is fired on the list element when an option is selected via keyboard or click.
 59 | 
 60 | For example, autocomplete when an option is selected:
 61 | 
 62 | ```js
 63 | list.addEventListener('combobox-commit', function (event) {
 64 |   console.log('Element selected: ', event.target)
 65 | })
 66 | ```
 67 | 
 68 | > **Note** When using `