Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions combobox-nav.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
/* @flow strict */

export function install(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void {
input.addEventListener('compositionstart', trackComposition)
input.addEventListener('compositionend', trackComposition)
input.addEventListener('keydown', keyboardBindings)
list.addEventListener('click', commitWithElement)
}

export function uninstall(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void {
input.removeAttribute('aria-activedescendant')
input.removeEventListener('compositionstart', trackComposition)
input.removeEventListener('compositionend', trackComposition)
input.removeEventListener('keydown', keyboardBindings)
list.removeEventListener('click', commitWithElement)
}

let isComposing = false
const ctrlBindings = !!navigator.userAgent.match(/Macintosh/)

function keyboardBindings(event: KeyboardEvent) {
if (event.shiftKey || event.metaKey || event.altKey) return
const input = event.currentTarget
if (!(input instanceof HTMLTextAreaElement || input instanceof HTMLInputElement)) return
if (isComposing) return
const list = document.getElementById(input.getAttribute('aria-owns') || '')
if (!list) return

switch (event.key) {
case 'Enter':
case 'Tab':
commit(input, list)
event.preventDefault()
if (commit(input, list)) {
event.preventDefault()
}
break
case 'Escape':
clearSelection(list)
break
case 'ArrowDown':
navigate(input, list, 1)
Expand Down Expand Up @@ -57,10 +67,11 @@ function commitWithElement(event: MouseEvent) {
event.preventDefault()
}

function commit(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void {
function commit(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): boolean {
const target = list.querySelector('[aria-selected="true"]')
if (!target) return
if (!target) return false
fireCommitEvent(target)
return true
}

function fireCommitEvent(target: Element): void {
Expand Down Expand Up @@ -96,3 +107,20 @@ export function navigate(
}
}
}

function clearSelection(list): void {
const target = list.querySelector('[aria-selected="true"]')
if (!target) return
target.setAttribute('aria-selected', 'false')
}

function trackComposition(event: Event): void {
const input = event.currentTarget
if (!(input instanceof HTMLTextAreaElement || input instanceof HTMLInputElement)) return
isComposing = event.type === 'compositionstart'

const list = document.getElementById(input.getAttribute('aria-owns') || '')
if (!list) return

clearSelection(list)
}
22 changes: 12 additions & 10 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
<style>[aria-selected="true"] { font-weight: bold; }</style>
</head>
<body>
<label>
Least favorite robot
<input aria-owns="list-id" role="combobox" type="text">
</label>
<ul role="listbox" id="list-id">
<li id="baymax" role="option">Baymax</li>
<li><del>BB-8</del></li>
<li id="hubot" role="option">Hubot</li>
<li id="r2-d2" role="option">R2-D2</li>
</ul>
<form>
<label>
Least favorite robot
<input aria-owns="list-id" role="combobox" type="text">
</label>
<ul role="listbox" id="list-id">
<li id="baymax" role="option">Baymax</li>
<li><del>BB-8</del></li>
<li id="hubot" role="option">Hubot</li>
<li id="r2-d2" role="option">R2-D2</li>
</ul>
</form>
<pre class="events"></pre>
<script type="text/javascript">
comboboxNav.install(document.querySelector('input'), document.querySelector('ul'))
Expand Down