|
1 |
| -const comboboxStates = new WeakMap() |
| 1 | +const ctrlBindings = !!navigator.userAgent.match(/Macintosh/) |
2 | 2 |
|
3 |
| -export function install(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void { |
4 |
| - if (comboboxStates.get(input)) { |
5 |
| - uninstall(input) |
6 |
| - } |
| 3 | +export default class Combobox { |
| 4 | + isComposing: boolean |
| 5 | + list: HTMLElement |
| 6 | + input: HTMLTextAreaElement | HTMLInputElement |
| 7 | + keyboardEventHandler: (event: KeyboardEvent) => void |
| 8 | + compositionEventHandler: (event: Event) => void |
| 9 | + |
| 10 | + constructor(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement) { |
| 11 | + this.input = input |
| 12 | + this.list = list |
| 13 | + this.isComposing = false |
| 14 | + |
| 15 | + if (!list.id) { |
| 16 | + list.id = `combobox-${Math.random() |
| 17 | + .toString() |
| 18 | + .slice(2, 6)}` |
| 19 | + } |
7 | 20 |
|
8 |
| - if (!list.id) { |
9 |
| - list.id = `combobox-${Math.random() |
10 |
| - .toString() |
11 |
| - .slice(2, 6)}` |
| 21 | + this.keyboardEventHandler = event => keyboardBindings(event, this) |
| 22 | + this.compositionEventHandler = event => trackComposition(event, this) |
| 23 | + input.setAttribute('role', 'combobox') |
| 24 | + input.setAttribute('aria-controls', list.id) |
| 25 | + input.setAttribute('aria-expanded', 'false') |
| 26 | + input.setAttribute('aria-autocomplete', 'list') |
| 27 | + input.setAttribute('aria-haspopup', 'listbox') |
12 | 28 | }
|
13 | 29 |
|
14 |
| - input.setAttribute('role', 'combobox') |
15 |
| - input.setAttribute('aria-controls', list.id) |
16 |
| - input.setAttribute('aria-expanded', 'false') |
17 |
| - input.setAttribute('aria-autocomplete', 'list') |
18 |
| - input.setAttribute('aria-haspopup', 'listbox') |
19 |
| - comboboxStates.set(input, {list, isComposing: false}) |
20 |
| -} |
| 30 | + destroy() { |
| 31 | + this.clearSelection() |
| 32 | + this.stop() |
21 | 33 |
|
22 |
| -export function uninstall(input: HTMLTextAreaElement | HTMLInputElement): void { |
23 |
| - const {list} = comboboxStates.get(input) || {} |
24 |
| - if (!list) return |
25 |
| - clearSelection(input, list) |
26 |
| - stop(input) |
27 |
| - |
28 |
| - input.removeAttribute('role') |
29 |
| - input.removeAttribute('aria-controls') |
30 |
| - input.removeAttribute('aria-expanded') |
31 |
| - input.removeAttribute('aria-autocomplete') |
32 |
| - input.removeAttribute('aria-haspopup') |
33 |
| - comboboxStates.delete(input) |
34 |
| -} |
| 34 | + this.input.removeAttribute('role') |
| 35 | + this.input.removeAttribute('aria-controls') |
| 36 | + this.input.removeAttribute('aria-expanded') |
| 37 | + this.input.removeAttribute('aria-autocomplete') |
| 38 | + this.input.removeAttribute('aria-haspopup') |
| 39 | + } |
35 | 40 |
|
36 |
| -export function start(input: HTMLTextAreaElement | HTMLInputElement): void { |
37 |
| - const {list} = comboboxStates.get(input) || {} |
38 |
| - if (!list) return |
| 41 | + start(): void { |
| 42 | + this.input.setAttribute('aria-expanded', 'true') |
| 43 | + this.input.addEventListener('compositionstart', this.compositionEventHandler) |
| 44 | + this.input.addEventListener('compositionend', this.compositionEventHandler) |
| 45 | + ;(this.input as HTMLElement).addEventListener('keydown', this.keyboardEventHandler) |
| 46 | + this.list.addEventListener('click', commitWithElement) |
| 47 | + } |
39 | 48 |
|
40 |
| - input.setAttribute('aria-expanded', 'true') |
41 |
| - input.addEventListener('compositionstart', trackComposition) |
42 |
| - input.addEventListener('compositionend', trackComposition) |
43 |
| - ;(input as HTMLInputElement).addEventListener('keydown', keyboardBindings) |
44 |
| - list.addEventListener('click', commitWithElement) |
45 |
| -} |
| 49 | + stop(): void { |
| 50 | + this.input.removeAttribute('aria-activedescendant') |
| 51 | + this.input.setAttribute('aria-expanded', 'false') |
| 52 | + this.input.removeEventListener('compositionstart', this.compositionEventHandler) |
| 53 | + this.input.removeEventListener('compositionend', this.compositionEventHandler) |
| 54 | + ;(this.input as HTMLElement).removeEventListener('keydown', this.keyboardEventHandler) |
| 55 | + this.list.removeEventListener('click', commitWithElement) |
| 56 | + } |
46 | 57 |
|
47 |
| -export function stop(input: HTMLTextAreaElement | HTMLInputElement): void { |
48 |
| - const {list} = comboboxStates.get(input) || {} |
49 |
| - if (!list) return |
| 58 | + navigate(indexDiff: -1 | 1 = 1): void { |
| 59 | + const focusEl = Array.from(this.list.querySelectorAll<HTMLElement>('[aria-selected="true"]')).filter(visible)[0] |
| 60 | + const els = Array.from(this.list.querySelectorAll<HTMLElement>('[role="option"]')).filter(visible) |
| 61 | + const focusIndex = els.indexOf(focusEl) |
| 62 | + let indexOfItem = indexDiff === 1 ? 0 : els.length - 1 |
| 63 | + if (focusEl && focusIndex >= 0) { |
| 64 | + const newIndex = focusIndex + indexDiff |
| 65 | + if (newIndex >= 0 && newIndex < els.length) indexOfItem = newIndex |
| 66 | + } |
50 | 67 |
|
51 |
| - input.removeAttribute('aria-activedescendant') |
52 |
| - input.setAttribute('aria-expanded', 'false') |
53 |
| - input.removeEventListener('compositionstart', trackComposition) |
54 |
| - input.removeEventListener('compositionend', trackComposition) |
55 |
| - ;(input as HTMLInputElement).removeEventListener('keydown', keyboardBindings) |
56 |
| - list.removeEventListener('click', commitWithElement) |
57 |
| -} |
| 68 | + const target = els[indexOfItem] |
| 69 | + if (!target) return |
| 70 | + for (const el of els) { |
| 71 | + if (target === el) { |
| 72 | + this.input.setAttribute('aria-activedescendant', target.id) |
| 73 | + target.setAttribute('aria-selected', 'true') |
| 74 | + scrollTo(this.list, target) |
| 75 | + } else { |
| 76 | + el.setAttribute('aria-selected', 'false') |
| 77 | + } |
| 78 | + } |
| 79 | + } |
58 | 80 |
|
59 |
| -const ctrlBindings = !!navigator.userAgent.match(/Macintosh/) |
| 81 | + clearSelection(): void { |
| 82 | + this.input.removeAttribute('aria-activedescendant') |
| 83 | + for (const el of this.list.querySelectorAll('[aria-selected="true"]')) { |
| 84 | + el.setAttribute('aria-selected', 'false') |
| 85 | + } |
| 86 | + } |
| 87 | +} |
60 | 88 |
|
61 |
| -function keyboardBindings(event: KeyboardEvent) { |
| 89 | +function keyboardBindings(event: KeyboardEvent, combobox: Combobox) { |
62 | 90 | if (event.shiftKey || event.metaKey || event.altKey) return
|
63 |
| - const input = event.currentTarget |
64 |
| - if (!(input instanceof HTMLTextAreaElement || input instanceof HTMLInputElement)) return |
65 |
| - const {list, isComposing} = comboboxStates.get(input) || {} |
66 |
| - if (!list || isComposing) return |
| 91 | + if (combobox.isComposing) return |
67 | 92 |
|
68 | 93 | switch (event.key) {
|
69 | 94 | case 'Enter':
|
70 | 95 | case 'Tab':
|
71 |
| - if (commit(input, list)) { |
| 96 | + if (commit(combobox.input, combobox.list)) { |
72 | 97 | event.preventDefault()
|
73 | 98 | }
|
74 | 99 | break
|
75 | 100 | case 'Escape':
|
76 |
| - clearSelection(input, list) |
| 101 | + combobox.clearSelection() |
77 | 102 | break
|
78 | 103 | case 'ArrowDown':
|
79 |
| - navigate(input, list, 1) |
| 104 | + combobox.navigate(1) |
80 | 105 | event.preventDefault()
|
81 | 106 | break
|
82 | 107 | case 'ArrowUp':
|
83 |
| - navigate(input, list, -1) |
| 108 | + combobox.navigate(-1) |
84 | 109 | event.preventDefault()
|
85 | 110 | break
|
86 | 111 | case 'n':
|
87 | 112 | if (ctrlBindings && event.ctrlKey) {
|
88 |
| - navigate(input, list, 1) |
| 113 | + combobox.navigate(1) |
89 | 114 | event.preventDefault()
|
90 | 115 | }
|
91 | 116 | break
|
92 | 117 | case 'p':
|
93 | 118 | if (ctrlBindings && event.ctrlKey) {
|
94 |
| - navigate(input, list, -1) |
| 119 | + combobox.navigate(-1) |
95 | 120 | event.preventDefault()
|
96 | 121 | }
|
97 | 122 | break
|
@@ -126,51 +151,13 @@ function visible(el: HTMLElement): boolean {
|
126 | 151 | )
|
127 | 152 | }
|
128 | 153 |
|
129 |
| -export function navigate( |
130 |
| - input: HTMLTextAreaElement | HTMLInputElement, |
131 |
| - list: HTMLElement, |
132 |
| - indexDiff: -1 | 1 = 1 |
133 |
| -): void { |
134 |
| - const focusEl = Array.from(list.querySelectorAll<HTMLElement>('[aria-selected="true"]')).filter(visible)[0] |
135 |
| - const els = Array.from(list.querySelectorAll<HTMLElement>('[role="option"]')).filter(visible) |
136 |
| - const focusIndex = els.indexOf(focusEl) |
137 |
| - let indexOfItem = indexDiff === 1 ? 0 : els.length - 1 |
138 |
| - if (focusEl && focusIndex >= 0) { |
139 |
| - const newIndex = focusIndex + indexDiff |
140 |
| - if (newIndex >= 0 && newIndex < els.length) indexOfItem = newIndex |
141 |
| - } |
142 |
| - |
143 |
| - const target = els[indexOfItem] |
144 |
| - if (!target) return |
145 |
| - for (const el of els) { |
146 |
| - if (target === el) { |
147 |
| - input.setAttribute('aria-activedescendant', target.id) |
148 |
| - target.setAttribute('aria-selected', 'true') |
149 |
| - scrollTo(list, target) |
150 |
| - } else { |
151 |
| - el.setAttribute('aria-selected', 'false') |
152 |
| - } |
153 |
| - } |
154 |
| -} |
155 |
| - |
156 |
| -export function clearSelection(input: HTMLTextAreaElement | HTMLInputElement, list: HTMLElement): void { |
157 |
| - input.removeAttribute('aria-activedescendant') |
158 |
| - for (const el of list.querySelectorAll('[aria-selected="true"]')) { |
159 |
| - el.setAttribute('aria-selected', 'false') |
160 |
| - } |
161 |
| -} |
162 |
| - |
163 |
| -function trackComposition(event: Event): void { |
164 |
| - const input = event.currentTarget |
165 |
| - if (!(input instanceof HTMLTextAreaElement || input instanceof HTMLInputElement)) return |
166 |
| - const state = comboboxStates.get(input) |
167 |
| - if (!state) return |
168 |
| - state.isComposing = event.type === 'compositionstart' |
| 154 | +function trackComposition(event: Event, combobox: Combobox): void { |
| 155 | + combobox.isComposing = event.type === 'compositionstart' |
169 | 156 |
|
170 |
| - const list = document.getElementById(input.getAttribute('aria-controls') || '') |
| 157 | + const list = document.getElementById(combobox.input.getAttribute('aria-controls') || '') |
171 | 158 | if (!list) return
|
172 | 159 |
|
173 |
| - clearSelection(input, list) |
| 160 | + combobox.clearSelection() |
174 | 161 | }
|
175 | 162 |
|
176 | 163 | function scrollTo(container: HTMLElement, target: HTMLElement) {
|
|
0 commit comments