| 1 | // combobox.mjs — the list under the caret that [[ and / open (spec §6.6, |
| 2 | // §6.12). While the list is open the editor has aria-controls and |
| 3 | // aria-activedescendant, and a polite live region says how many choices |
| 4 | // there are ("3 pages"). Not aria-expanded: a textbox may not carry it |
| 5 | // (ARIA 1.2; axe's aria-allowed-attr, build note 49). ↑ ↓ move, |
| 6 | // ↵ or Tab choose, Esc closes. The list is drawn under the caret, kept |
| 7 | // on screen, and above the phone's keyboard (visualViewport). |
| 8 | // |
| 9 | // Given an input (the bubble's Page link field, build note 81), the |
| 10 | // list is that field's instead: drawn under it, and the input is the |
| 11 | // combobox, so it carries aria-expanded as ARIA asks of one. |
| 12 | |
| 13 | let serial = 0 |
| 14 | |
| 15 | function element (tag, attrs = {}, text = '') { |
| 16 | const node = document.createElement(tag) |
| 17 | for (const [name, value] of Object.entries(attrs)) node.setAttribute(name, value) |
| 18 | if (text) node.textContent = text |
| 19 | return node |
| 20 | } |
| 21 | |
| 22 | export class Combobox { |
| 23 | constructor (view, { label, count, input = null }) { |
| 24 | this.view = view |
| 25 | this.count = count |
| 26 | this.input = input |
| 27 | this.id = `mapa-list-${++serial}` |
| 28 | this.list = element('ul', { id: this.id, role: 'listbox', 'aria-label': label, 'mapa-combo': '' }) |
| 29 | this.list.hidden = true |
| 30 | this.live = element('div', { 'mapa-sr': '', role: 'status', 'aria-live': 'polite' }) |
| 31 | document.body.append(this.list, this.live) |
| 32 | this.items = [] |
| 33 | this.active = 0 |
| 34 | this.list.addEventListener('mousedown', event => event.preventDefault()) |
| 35 | } |
| 36 | |
| 37 | get open () { return !this.list.hidden } |
| 38 | |
| 39 | // show lists items ({label, detail, run}) under the document position at. |
| 40 | show (items, at) { |
| 41 | this.items = items |
| 42 | this.active = 0 |
| 43 | this.list.replaceChildren(...items.map((item, index) => { |
| 44 | const row = element('li', { id: `${this.id}-${index}`, role: 'option', 'aria-selected': 'false' }) |
| 45 | row.append(element('span', {}, item.label)) |
| 46 | if (item.detail) row.append(element('small', {}, item.detail)) |
| 47 | row.addEventListener('click', () => this.choose(index)) |
| 48 | return row |
| 49 | })) |
| 50 | this.list.hidden = items.length === 0 |
| 51 | this.place(at) |
| 52 | this.mark() |
| 53 | const said = this.count(items) |
| 54 | if (this.live.textContent !== said) this.live.textContent = said |
| 55 | } |
| 56 | |
| 57 | place (at) { |
| 58 | if (this.list.hidden) return |
| 59 | const coords = this.input ? this.input.getBoundingClientRect() : this.view.coordsAtPos(at) |
| 60 | const viewport = window.visualViewport |
| 61 | const bottom = viewport ? viewport.offsetTop + viewport.height : window.innerHeight |
| 62 | const height = this.list.offsetHeight |
| 63 | const below = coords.bottom + 4 |
| 64 | const top = below + height > bottom ? Math.max(coords.top - height - 4, 4) : below |
| 65 | const width = this.list.offsetWidth |
| 66 | const left = Math.max(4, Math.min(coords.left, window.innerWidth - width - 4)) |
| 67 | this.list.style.setProperty('top', `${top}px`) |
| 68 | this.list.style.setProperty('left', `${left}px`) |
| 69 | } |
| 70 | |
| 71 | mark () { |
| 72 | const rows = this.list.children |
| 73 | for (let index = 0; index < rows.length; index++) { |
| 74 | rows[index].setAttribute('aria-selected', index === this.active ? 'true' : 'false') |
| 75 | } |
| 76 | const dom = this.input ?? this.view.dom |
| 77 | if (this.input) dom.setAttribute('aria-expanded', this.open ? 'true' : 'false') |
| 78 | if (this.open) { |
| 79 | dom.setAttribute('aria-controls', this.id) |
| 80 | dom.setAttribute('aria-activedescendant', `${this.id}-${this.active}`) |
| 81 | rows[this.active]?.scrollIntoView({ block: 'nearest' }) |
| 82 | } else if (dom.getAttribute('aria-controls') === this.id) { |
| 83 | dom.removeAttribute('aria-controls') |
| 84 | dom.removeAttribute('aria-activedescendant') |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | close () { |
| 89 | if (!this.open) return |
| 90 | this.list.hidden = true |
| 91 | this.items = [] |
| 92 | this.live.textContent = '' |
| 93 | this.mark() |
| 94 | } |
| 95 | |
| 96 | choose (index) { |
| 97 | const item = this.items[index] |
| 98 | this.close() |
| 99 | // Focus first: a row may move focus on, as / → Date does to its |
| 100 | // picker (build note 83). |
| 101 | if (!this.input) this.view.focus() |
| 102 | if (item) item.run(this.view) |
| 103 | } |
| 104 | |
| 105 | // keys is the list's share of a keydown; true when it took the key. |
| 106 | keys (event) { |
| 107 | if (!this.open || event.isComposing) return false |
| 108 | const count = this.items.length |
| 109 | switch (event.key) { |
| 110 | case 'ArrowDown': |
| 111 | case 'ArrowUp': |
| 112 | this.active = (this.active + (event.key === 'ArrowDown' ? 1 : -1) + count) % count |
| 113 | this.mark() |
| 114 | return true |
| 115 | case 'Enter': |
| 116 | case 'Tab': |
| 117 | this.choose(this.active) |
| 118 | return true |
| 119 | case 'Escape': |
| 120 | this.close() |
| 121 | return true |
| 122 | } |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | destroy () { |
| 127 | this.list.remove() |
| 128 | this.live.remove() |
| 129 | } |
| 130 | } |
| 131 | |