// combobox.mjs — the list under the caret that [[ and / open (spec §6.6, // §6.12). While the list is open the editor has aria-controls and // aria-activedescendant, and a polite live region says how many choices // there are ("3 pages"). Not aria-expanded: a textbox may not carry it // (ARIA 1.2; axe's aria-allowed-attr, build note 49). ↑ ↓ move, // ↵ or Tab choose, Esc closes. The list is drawn under the caret, kept // on screen, and above the phone's keyboard (visualViewport). // // Given an input (the bubble's Page link field, build note 81), the // list is that field's instead: drawn under it, and the input is the // combobox, so it carries aria-expanded as ARIA asks of one. let serial = 0 function element (tag, attrs = {}, text = '') { const node = document.createElement(tag) for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) if (text) node.textContent = text return node } export class Combobox { constructor (view, { label, count, input = null }) { this.view = view this.count = count this.input = input this.id = `mapa-list-${++serial}` this.list = element('ul', { id: this.id, role: 'listbox', 'aria-label': label, 'mapa-combo': '' }) this.list.hidden = true this.live = element('div', { 'mapa-sr': '', role: 'status', 'aria-live': 'polite' }) document.body.append(this.list, this.live) this.items = [] this.active = 0 this.list.addEventListener('mousedown', event => event.preventDefault()) } get open () { return !this.list.hidden } // show lists items ({label, detail, run}) under the document position at. show (items, at) { this.items = items this.active = 0 this.list.replaceChildren(...items.map((item, index) => { const row = element('li', { id: `${this.id}-${index}`, role: 'option', 'aria-selected': 'false' }) row.append(element('span', {}, item.label)) if (item.detail) row.append(element('small', {}, item.detail)) row.addEventListener('click', () => this.choose(index)) return row })) this.list.hidden = items.length === 0 this.place(at) this.mark() const said = this.count(items) if (this.live.textContent !== said) this.live.textContent = said } place (at) { if (this.list.hidden) return const coords = this.input ? this.input.getBoundingClientRect() : this.view.coordsAtPos(at) const viewport = window.visualViewport const bottom = viewport ? viewport.offsetTop + viewport.height : window.innerHeight const height = this.list.offsetHeight const below = coords.bottom + 4 const top = below + height > bottom ? Math.max(coords.top - height - 4, 4) : below const width = this.list.offsetWidth const left = Math.max(4, Math.min(coords.left, window.innerWidth - width - 4)) this.list.style.setProperty('top', `${top}px`) this.list.style.setProperty('left', `${left}px`) } mark () { const rows = this.list.children for (let index = 0; index < rows.length; index++) { rows[index].setAttribute('aria-selected', index === this.active ? 'true' : 'false') } const dom = this.input ?? this.view.dom if (this.input) dom.setAttribute('aria-expanded', this.open ? 'true' : 'false') if (this.open) { dom.setAttribute('aria-controls', this.id) dom.setAttribute('aria-activedescendant', `${this.id}-${this.active}`) rows[this.active]?.scrollIntoView({ block: 'nearest' }) } else if (dom.getAttribute('aria-controls') === this.id) { dom.removeAttribute('aria-controls') dom.removeAttribute('aria-activedescendant') } } close () { if (!this.open) return this.list.hidden = true this.items = [] this.live.textContent = '' this.mark() } choose (index) { const item = this.items[index] this.close() // Focus first: a row may move focus on, as / → Date does to its // picker (build note 83). if (!this.input) this.view.focus() if (item) item.run(this.view) } // keys is the list's share of a keydown; true when it took the key. keys (event) { if (!this.open || event.isComposing) return false const count = this.items.length switch (event.key) { case 'ArrowDown': case 'ArrowUp': this.active = (this.active + (event.key === 'ArrowDown' ? 1 : -1) + count) % count this.mark() return true case 'Enter': case 'Tab': this.choose(this.active) return true case 'Escape': this.close() return true } return false } destroy () { this.list.remove() this.live.remove() } }