@splendidlabz/svelte 4.15.2 → 4.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/ContentList/ContentList.svelte +33 -13
- package/components/Form/Combobox/AutocompleteInput.svelte +22 -59
- package/components/Form/Combobox/AutocompleteOptions.svelte +52 -67
- package/components/Form/Combobox/ComboboxInput.svelte +37 -0
- package/components/Form/Combobox/ComboboxOptions.svelte +49 -0
- package/components/Form/Combobox/autocomplete-matching.js +55 -100
- package/components/Form/Combobox/autocomplete.svelte.js +222 -0
- package/components/Form/Combobox/combobox.svelte.js +276 -0
- package/components/Form/Combobox/index.js +0 -3
- package/components/Form/Combobox/listbox.js +173 -0
- package/components/Icon/Icon.svelte +2 -4
- package/package.json +1 -1
- package/components/Form/Combobox/autocomplete.js +0 -393
- package/components/Form/Combobox/combobox-store.js +0 -65
- package/components/Form/Combobox/combobox.js +0 -360
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Filters a combobox's options as someone types.
|
|
3
|
+
*
|
|
4
|
+
* Filtering reads each option's `data-text`. The match highlight is computed separately from the option's rendered `textContent`, because an `option` snippet can render text that differs from `data-text`, and the highlight has to sit on what's on screen.
|
|
5
|
+
*/
|
|
6
|
+
import { debounce, omitEmpty } from '@splendidlabz/utils'
|
|
7
|
+
import { addListeners, removeListeners } from '@splendidlabz/utils/dom'
|
|
8
|
+
import { SvelteMap } from 'svelte/reactivity'
|
|
9
|
+
import { getMatches, hasPerfectMatch } from './autocomplete-matching.js'
|
|
10
|
+
import { closeCombobox, initCombobox, openCombobox } from './combobox.svelte.js'
|
|
11
|
+
import { clearHighlight, getAllOptions } from './listbox.js'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* autocompleteInput
|
|
15
|
+
* Narrows the options a combobox shows to the ones its text matches.
|
|
16
|
+
*
|
|
17
|
+
* @param {HTMLInputElement} node
|
|
18
|
+
* @param {object} [props]
|
|
19
|
+
* @param {object} [props.options]
|
|
20
|
+
* @param {(query: string) => Promise<any[]>} [props.options.search] Replaces local filtering
|
|
21
|
+
* @param {number} [props.options.debounceMs] Wait before searching
|
|
22
|
+
* @param {(item: any) => void} [props.options.onselect] Receives the selected item
|
|
23
|
+
*/
|
|
24
|
+
export function autocompleteInput(node, props = {}) {
|
|
25
|
+
const id = node.getAttribute('target')
|
|
26
|
+
let onselect = props.options?.onselect
|
|
27
|
+
|
|
28
|
+
const combobox = initCombobox({
|
|
29
|
+
id,
|
|
30
|
+
state: { searchState: 'idle' },
|
|
31
|
+
options: {
|
|
32
|
+
debounceMs: 250,
|
|
33
|
+
...omitEmpty(props.options),
|
|
34
|
+
onselect: selectItem,
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const searchCache = new SvelteMap()
|
|
39
|
+
|
|
40
|
+
const fetchResults = debounce(
|
|
41
|
+
async query => {
|
|
42
|
+
const results = await combobox.options.search(query)
|
|
43
|
+
searchCache.set(query, results)
|
|
44
|
+
|
|
45
|
+
// Someone kept typing while this was in flight
|
|
46
|
+
if (node.value.trim() !== query) return
|
|
47
|
+
showResults(results)
|
|
48
|
+
},
|
|
49
|
+
() => combobox.options.debounceMs,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const listeners = [{ event: 'input', node, handler: onInput }]
|
|
53
|
+
|
|
54
|
+
addListeners(listeners)
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
update(nextProps = {}) {
|
|
58
|
+
onselect = nextProps.options?.onselect
|
|
59
|
+
Object.assign(combobox.options, omitEmpty(nextProps.options), {
|
|
60
|
+
onselect: selectItem,
|
|
61
|
+
})
|
|
62
|
+
},
|
|
63
|
+
destroy() {
|
|
64
|
+
removeListeners(listeners)
|
|
65
|
+
fetchResults.cancel()
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// The combobox layer hands back the option element. Items live here, so this maps it back.
|
|
70
|
+
function selectItem(option) {
|
|
71
|
+
if (!onselect) return
|
|
72
|
+
|
|
73
|
+
const { index } = option.dataset
|
|
74
|
+
if (index === undefined) return onselect(option)
|
|
75
|
+
onselect($state.snapshot(combobox.state.items[Number(index)]))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function onInput() {
|
|
79
|
+
// The options can hydrate as their own island, after the input
|
|
80
|
+
if (!combobox.nodes.listbox) return
|
|
81
|
+
|
|
82
|
+
const query = node.value.trim()
|
|
83
|
+
if (combobox.options.search) searchOptions(query)
|
|
84
|
+
else showMatchingOptions({ combobox, query })
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function searchOptions(query) {
|
|
88
|
+
if (!query) {
|
|
89
|
+
combobox.state.results = null
|
|
90
|
+
combobox.state.searchState = 'idle'
|
|
91
|
+
closeCombobox(combobox)
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Selecting dispatches `input`, which shouldn't search for what was just chosen
|
|
96
|
+
if (query === combobox.state.selectedText) return
|
|
97
|
+
|
|
98
|
+
if (searchCache.has(query)) {
|
|
99
|
+
showResults(searchCache.get(query))
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
combobox.state.searchState = 'searching'
|
|
104
|
+
openCombobox(combobox)
|
|
105
|
+
fetchResults(query)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function showResults(results) {
|
|
109
|
+
combobox.state.results = results
|
|
110
|
+
combobox.state.searchState = 'done'
|
|
111
|
+
openCombobox(combobox)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* autocompleteOptions
|
|
117
|
+
* Registers the items the options were rendered from, so selecting one can return the original.
|
|
118
|
+
*
|
|
119
|
+
* @param {HTMLElement} node Wrapper holding the listbox
|
|
120
|
+
* @param {object} [props]
|
|
121
|
+
* @param {any[]} [props.items]
|
|
122
|
+
*/
|
|
123
|
+
export function autocompleteOptions(node, props = {}) {
|
|
124
|
+
const combobox = initCombobox({
|
|
125
|
+
id: node.id,
|
|
126
|
+
state: { items: props.items },
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
update(nextProps = {}) {
|
|
131
|
+
combobox.state.items = nextProps.items
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Hides what the query misses, ranks what's left, and paints the letters that matched
|
|
137
|
+
function showMatchingOptions({ combobox, query }) {
|
|
138
|
+
const { input, listbox } = combobox.nodes
|
|
139
|
+
const options = getAllOptions(listbox)
|
|
140
|
+
|
|
141
|
+
// The list is about to change, so the current highlight points nowhere
|
|
142
|
+
clearHighlight(listbox)
|
|
143
|
+
input.removeAttribute('aria-activedescendant')
|
|
144
|
+
|
|
145
|
+
if (!query) {
|
|
146
|
+
showAllOptions(options)
|
|
147
|
+
paintMatches([])
|
|
148
|
+
closeCombobox(combobox)
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const texts = options.map(option => option.dataset.text)
|
|
153
|
+
const matches = getMatches({ texts, query })
|
|
154
|
+
const matchedOptions = matches.map(match => options[match.index])
|
|
155
|
+
|
|
156
|
+
for (const option of options) option.hidden = true
|
|
157
|
+
rankOptions(matchedOptions)
|
|
158
|
+
paintMatches(
|
|
159
|
+
matchedOptions.flatMap(option => getTextRanges({ option, query })),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
const isSettled = !matches.length || hasPerfectMatch({ texts, query })
|
|
163
|
+
if (isSettled) closeCombobox(combobox)
|
|
164
|
+
else openCombobox(combobox)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Puts every option back to unfiltered
|
|
168
|
+
function showAllOptions(options) {
|
|
169
|
+
for (const option of options) {
|
|
170
|
+
option.hidden = false
|
|
171
|
+
option.style.order = ''
|
|
172
|
+
option.removeAttribute('aria-posinset')
|
|
173
|
+
option.removeAttribute('aria-setsize')
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Shows the best matches first through `order`, which leaves every node where Svelte put it
|
|
178
|
+
function rankOptions(options) {
|
|
179
|
+
options.forEach((option, rank) => {
|
|
180
|
+
option.hidden = false
|
|
181
|
+
option.style.order = rank
|
|
182
|
+
option.setAttribute('aria-posinset', rank + 1)
|
|
183
|
+
option.setAttribute('aria-setsize', options.length)
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Paints the matched letters. A browser without the Highlight API filters without painting.
|
|
188
|
+
function paintMatches(ranges) {
|
|
189
|
+
if (!CSS.highlights) return
|
|
190
|
+
|
|
191
|
+
if (!ranges.length) {
|
|
192
|
+
CSS.highlights.delete('autocomplete-match')
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
CSS.highlights.set('autocomplete-match', new Highlight(...ranges))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Turns matched character positions into DOM ranges the Highlight API can paint
|
|
200
|
+
function getTextRanges({ option, query }) {
|
|
201
|
+
const positions = getMatches({ texts: [option.textContent], query })[0]
|
|
202
|
+
?.ranges
|
|
203
|
+
if (!positions) return []
|
|
204
|
+
|
|
205
|
+
const walker = document.createTreeWalker(option, NodeFilter.SHOW_TEXT)
|
|
206
|
+
const chunks = []
|
|
207
|
+
let offset = 0
|
|
208
|
+
|
|
209
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
210
|
+
chunks.push({ node, start: offset, end: offset + node.length })
|
|
211
|
+
offset += node.length
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return positions.map(([start, end]) => {
|
|
215
|
+
const startChunk = chunks.find(chunk => start < chunk.end)
|
|
216
|
+
const endChunk = chunks.find(chunk => end <= chunk.end)
|
|
217
|
+
const range = new Range()
|
|
218
|
+
range.setStart(startChunk.node, start - startChunk.start)
|
|
219
|
+
range.setEnd(endChunk.node, end - endChunk.start)
|
|
220
|
+
return range
|
|
221
|
+
})
|
|
222
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Links a combobox input to its listbox by id.
|
|
3
|
+
*
|
|
4
|
+
* The input and the options can hydrate as separate Astro islands in either order, so both parts register into one store keyed by id.
|
|
5
|
+
*/
|
|
6
|
+
import { omitEmpty } from '@splendidlabz/utils'
|
|
7
|
+
import { addListeners, isTab, removeListeners } from '@splendidlabz/utils/dom'
|
|
8
|
+
import { initFloating } from '../../Disclosure/Popover/floating.js'
|
|
9
|
+
import {
|
|
10
|
+
clearHighlight,
|
|
11
|
+
getAdjacentOption,
|
|
12
|
+
getHighlightedOption,
|
|
13
|
+
getVisibleOptions,
|
|
14
|
+
highlightOption,
|
|
15
|
+
prepareOptions,
|
|
16
|
+
selectOption,
|
|
17
|
+
} from './listbox.js'
|
|
18
|
+
|
|
19
|
+
// ========================================
|
|
20
|
+
// Shared state
|
|
21
|
+
// ========================================
|
|
22
|
+
const comboboxes = $state([])
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* getCombobox
|
|
26
|
+
* Gets the combobox registered under an id.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} id
|
|
29
|
+
*/
|
|
30
|
+
export function getCombobox(id) {
|
|
31
|
+
return comboboxes.find(combobox => combobox.id === id)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* initCombobox
|
|
36
|
+
* Registers one part of a combobox, merging it with parts already registered.
|
|
37
|
+
*
|
|
38
|
+
* @param {object} opts
|
|
39
|
+
* @param {string} opts.id Id of the options wrapper
|
|
40
|
+
* @param {object} [opts.state]
|
|
41
|
+
* @param {object} [opts.nodes]
|
|
42
|
+
* @param {object} [opts.options]
|
|
43
|
+
*/
|
|
44
|
+
export function initCombobox({ id, state = {}, nodes = {}, options = {} }) {
|
|
45
|
+
const combobox = getCombobox(id)
|
|
46
|
+
if (!combobox) {
|
|
47
|
+
comboboxes.push({
|
|
48
|
+
id,
|
|
49
|
+
state,
|
|
50
|
+
nodes: omitEmpty(nodes, { shallow: true }),
|
|
51
|
+
options,
|
|
52
|
+
})
|
|
53
|
+
return getCombobox(id)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
combobox.state = { ...combobox.state, ...omitEmpty(state) }
|
|
57
|
+
combobox.nodes = { ...combobox.nodes, ...omitEmpty(nodes, { shallow: true }) }
|
|
58
|
+
combobox.options = { ...combobox.options, ...omitEmpty(options) }
|
|
59
|
+
return combobox
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ========================================
|
|
63
|
+
// Actions
|
|
64
|
+
// ========================================
|
|
65
|
+
/**
|
|
66
|
+
* comboboxInput
|
|
67
|
+
* Lets an input open, move through and select the options it targets.
|
|
68
|
+
*
|
|
69
|
+
* @param {HTMLInputElement} node
|
|
70
|
+
* @param {object} [props]
|
|
71
|
+
* @param {object} [props.options]
|
|
72
|
+
* @param {boolean} [props.options.loop] Wrap past either end
|
|
73
|
+
* @param {(item: any) => void} [props.options.onselect] Receives the selected item
|
|
74
|
+
*/
|
|
75
|
+
export function comboboxInput(node, props = {}) {
|
|
76
|
+
const id = node.getAttribute('target')
|
|
77
|
+
const combobox = initCombobox({
|
|
78
|
+
id,
|
|
79
|
+
nodes: { input: node },
|
|
80
|
+
options: { loop: true, ...omitEmpty(props.options) },
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
setInitialExpanded(combobox)
|
|
84
|
+
|
|
85
|
+
const listeners = [
|
|
86
|
+
{ event: 'keydown', node, handler: onKeydown },
|
|
87
|
+
{ event: 'focusout', node, handler: onFocusout },
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
addListeners(listeners)
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
update(nextProps = {}) {
|
|
94
|
+
Object.assign(combobox.options, omitEmpty(nextProps.options))
|
|
95
|
+
},
|
|
96
|
+
destroy() {
|
|
97
|
+
removeListeners(listeners)
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function onKeydown(event) {
|
|
102
|
+
const { listbox } = combobox.nodes
|
|
103
|
+
if (!listbox) return
|
|
104
|
+
|
|
105
|
+
const { key } = event
|
|
106
|
+
const isOpen = isComboboxOpen(combobox)
|
|
107
|
+
|
|
108
|
+
if (key === 'ArrowDown' || key === 'ArrowUp') {
|
|
109
|
+
event.preventDefault()
|
|
110
|
+
|
|
111
|
+
if (!isOpen) {
|
|
112
|
+
if (getVisibleOptions(listbox).length) openCombobox(combobox)
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const option = getAdjacentOption({
|
|
117
|
+
listbox,
|
|
118
|
+
direction: key === 'ArrowDown' ? 'next' : 'prev',
|
|
119
|
+
loop: combobox.options.loop,
|
|
120
|
+
})
|
|
121
|
+
if (option) highlight(combobox, option)
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (key === 'Escape' && isOpen) {
|
|
126
|
+
event.preventDefault()
|
|
127
|
+
closeCombobox(combobox)
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const highlightedOption = getHighlightedOption(listbox)
|
|
132
|
+
if (!isOpen || !highlightedOption) return
|
|
133
|
+
|
|
134
|
+
if (key === 'Enter') {
|
|
135
|
+
event.preventDefault()
|
|
136
|
+
commitSelection(combobox, highlightedOption)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (isTab(event)) commitSelection(combobox, highlightedOption)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function onFocusout() {
|
|
143
|
+
if (combobox.nodes.listbox) closeCombobox(combobox)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* comboboxOptions
|
|
149
|
+
* Registers the options a combobox input targets.
|
|
150
|
+
*
|
|
151
|
+
* @param {HTMLElement} node Wrapper holding the listbox
|
|
152
|
+
* @param {object} [props]
|
|
153
|
+
* @param {boolean} [props.multiple] Let more than one option be selected
|
|
154
|
+
*/
|
|
155
|
+
export function comboboxOptions(node, props = {}) {
|
|
156
|
+
const listbox = node.querySelector('[role="listbox"]')
|
|
157
|
+
prepareOptions({ listbox, multiple: props.multiple })
|
|
158
|
+
|
|
159
|
+
const combobox = initCombobox({
|
|
160
|
+
id: node.id,
|
|
161
|
+
nodes: { listbox, popover: node.hasAttribute('popover') ? node : null },
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
setInitialExpanded(combobox)
|
|
165
|
+
|
|
166
|
+
const listeners = [
|
|
167
|
+
{ event: 'pointerdown', node, handler: keepInputFocus },
|
|
168
|
+
{ event: 'pointerover', node: listbox, handler: highlightHoveredOption },
|
|
169
|
+
{ event: 'click', node: listbox, handler: selectClickedOption },
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
addListeners(listeners)
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
destroy() {
|
|
176
|
+
removeListeners(listeners)
|
|
177
|
+
combobox.floating?.cleanup()
|
|
178
|
+
},
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Stops a press inside the list from blurring the input
|
|
182
|
+
function keepInputFocus(event) {
|
|
183
|
+
event.preventDefault()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function highlightHoveredOption(event) {
|
|
187
|
+
const option = event.target.closest('[role="option"]')
|
|
188
|
+
if (option) highlight(combobox, option, { shouldScroll: false })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function selectClickedOption(event) {
|
|
192
|
+
const option = event.target.closest('[role="option"]')
|
|
193
|
+
if (option) commitSelection(combobox, option)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ========================================
|
|
198
|
+
// Methods
|
|
199
|
+
// ========================================
|
|
200
|
+
/**
|
|
201
|
+
* openCombobox
|
|
202
|
+
* Shows the options under the input.
|
|
203
|
+
*
|
|
204
|
+
* Inline options are always shown, so this only changes popover options.
|
|
205
|
+
*
|
|
206
|
+
* @param {object} combobox
|
|
207
|
+
* @returns {void}
|
|
208
|
+
*/
|
|
209
|
+
export function openCombobox(combobox) {
|
|
210
|
+
if (isComboboxOpen(combobox)) return
|
|
211
|
+
|
|
212
|
+
const { input, popover } = combobox.nodes
|
|
213
|
+
input.setAttribute('aria-expanded', 'true')
|
|
214
|
+
|
|
215
|
+
popover.style.width = `${input.getBoundingClientRect().width}px`
|
|
216
|
+
popover.showPopover()
|
|
217
|
+
|
|
218
|
+
combobox.floating ??= initFloating({
|
|
219
|
+
referenceNode: input,
|
|
220
|
+
floatingNode: popover,
|
|
221
|
+
placement: 'bottom-start',
|
|
222
|
+
offset: 0,
|
|
223
|
+
})
|
|
224
|
+
combobox.floating.update()
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* closeCombobox
|
|
229
|
+
* Hides popover options and clears the highlight.
|
|
230
|
+
*
|
|
231
|
+
* @param {object} combobox
|
|
232
|
+
* @returns {void}
|
|
233
|
+
*/
|
|
234
|
+
export function closeCombobox(combobox) {
|
|
235
|
+
const { input, listbox, popover } = combobox.nodes
|
|
236
|
+
|
|
237
|
+
clearHighlight(listbox)
|
|
238
|
+
input.removeAttribute('aria-activedescendant')
|
|
239
|
+
|
|
240
|
+
if (!popover || !isComboboxOpen(combobox)) return
|
|
241
|
+
|
|
242
|
+
input.setAttribute('aria-expanded', 'false')
|
|
243
|
+
popover.hidePopover()
|
|
244
|
+
combobox.floating.cleanup()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function isComboboxOpen(combobox) {
|
|
248
|
+
return combobox.nodes.input.getAttribute('aria-expanded') === 'true'
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Inline options are always shown, so the input starts expanded
|
|
252
|
+
function setInitialExpanded(combobox) {
|
|
253
|
+
const { input, listbox, popover } = combobox.nodes
|
|
254
|
+
if (!input || !listbox) return
|
|
255
|
+
input.setAttribute('aria-expanded', String(!popover))
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function highlight(combobox, option, { shouldScroll = true } = {}) {
|
|
259
|
+
highlightOption({ listbox: combobox.nodes.listbox, option, shouldScroll })
|
|
260
|
+
combobox.nodes.input.setAttribute('aria-activedescendant', option.id)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Marks the option chosen, then dispatches `input` so `bind:value` and filtering see the new text
|
|
264
|
+
function commitSelection(combobox, option) {
|
|
265
|
+
const { input, listbox } = combobox.nodes
|
|
266
|
+
const { text } = option.dataset
|
|
267
|
+
|
|
268
|
+
selectOption({ listbox, option })
|
|
269
|
+
combobox.state.selectedText = text
|
|
270
|
+
input.value = text
|
|
271
|
+
input.dispatchEvent(new Event('input', { bubbles: true }))
|
|
272
|
+
closeCombobox(combobox)
|
|
273
|
+
|
|
274
|
+
// A layer above owns the items, so it gets the element and maps it back.
|
|
275
|
+
combobox.options.onselect?.(option)
|
|
276
|
+
}
|
|
@@ -3,12 +3,9 @@ import AutocompleteOptions from './AutocompleteOptions.svelte'
|
|
|
3
3
|
import TimepickerInput from './TimepickerInput.svelte'
|
|
4
4
|
import TimepickerOptions from './TimepickerOptions.svelte'
|
|
5
5
|
|
|
6
|
-
// Combobox has been renamed to Autocomplete
|
|
7
6
|
export {
|
|
8
7
|
AutocompleteInput,
|
|
9
8
|
AutocompleteOptions,
|
|
10
|
-
AutocompleteInput as ComboboxInput,
|
|
11
|
-
AutocompleteOptions as ComboboxOptions,
|
|
12
9
|
TimepickerInput,
|
|
13
10
|
TimepickerOptions,
|
|
14
11
|
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file DOM helpers for the options inside a listbox.
|
|
3
|
+
*
|
|
4
|
+
* Two states sit on an option and they are not the same thing. `data-highlighted` is the cursor — where the keyboard or pointer is, one option at a time. `aria-selected` is the selection — what the person chose, which can be more than one.
|
|
5
|
+
*/
|
|
6
|
+
import { randomString } from '@splendidlabz/utils/dom'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* prepareOptions
|
|
10
|
+
* Gives options written as plain `li` their role, id and text.
|
|
11
|
+
*
|
|
12
|
+
* Options rendered by Svelte already carry these in their markup, so only `li` without a role are touched.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} opts
|
|
15
|
+
* @param {HTMLElement} opts.listbox
|
|
16
|
+
* @param {boolean} [opts.multiple] Let more than one option be selected
|
|
17
|
+
* @returns {void}
|
|
18
|
+
*/
|
|
19
|
+
export function prepareOptions({ listbox, multiple = false }) {
|
|
20
|
+
if (multiple) listbox.setAttribute('aria-multiselectable', 'true')
|
|
21
|
+
|
|
22
|
+
for (const option of listbox.querySelectorAll('li:not([role])')) {
|
|
23
|
+
option.setAttribute('role', 'option')
|
|
24
|
+
option.id ||= randomString(8)
|
|
25
|
+
option.dataset.text = option.textContent.trim()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// A multiselectable listbox states every option's selected state, not only the chosen ones.
|
|
29
|
+
if (!multiple) return
|
|
30
|
+
for (const option of getAllOptions(listbox)) {
|
|
31
|
+
if (!option.hasAttribute('aria-selected')) {
|
|
32
|
+
option.setAttribute('aria-selected', 'false')
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* getAllOptions
|
|
39
|
+
* Gets every option, including the hidden ones.
|
|
40
|
+
*
|
|
41
|
+
* @param {HTMLElement} listbox
|
|
42
|
+
* @returns {HTMLElement[]}
|
|
43
|
+
*/
|
|
44
|
+
export function getAllOptions(listbox) {
|
|
45
|
+
return Array.from(listbox.querySelectorAll('[role="option"]'))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* getVisibleOptions
|
|
50
|
+
* Gets the options that aren't filtered out, in the order they appear on screen.
|
|
51
|
+
*
|
|
52
|
+
* Filtering ranks matches with the CSS `order` property, so screen order and DOM order differ. Arrow keys follow this list, so it has to be the one the eye follows.
|
|
53
|
+
*
|
|
54
|
+
* @param {HTMLElement} listbox
|
|
55
|
+
* @returns {HTMLElement[]}
|
|
56
|
+
*/
|
|
57
|
+
export function getVisibleOptions(listbox) {
|
|
58
|
+
const options = Array.from(
|
|
59
|
+
listbox.querySelectorAll('[role="option"]:not([hidden])'),
|
|
60
|
+
)
|
|
61
|
+
return options.sort((a, b) => getOrder(a) - getOrder(b))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* getHighlightedOption
|
|
66
|
+
* Gets the option the cursor is on.
|
|
67
|
+
*
|
|
68
|
+
* @param {HTMLElement} listbox
|
|
69
|
+
* @returns {HTMLElement | null}
|
|
70
|
+
*/
|
|
71
|
+
export function getHighlightedOption(listbox) {
|
|
72
|
+
return listbox.querySelector('[role="option"][data-highlighted]')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* highlightOption
|
|
77
|
+
* Moves the cursor onto one option.
|
|
78
|
+
*
|
|
79
|
+
* @param {object} opts
|
|
80
|
+
* @param {HTMLElement} opts.listbox
|
|
81
|
+
* @param {HTMLElement} opts.option
|
|
82
|
+
* @param {boolean} [opts.shouldScroll] Scroll option into view
|
|
83
|
+
* @returns {void}
|
|
84
|
+
*/
|
|
85
|
+
export function highlightOption({ listbox, option, shouldScroll = true }) {
|
|
86
|
+
clearHighlight(listbox)
|
|
87
|
+
option.dataset.highlighted = ''
|
|
88
|
+
if (shouldScroll) option.scrollIntoView({ block: 'nearest' })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* clearHighlight
|
|
93
|
+
* Takes the cursor off every option.
|
|
94
|
+
*
|
|
95
|
+
* @param {HTMLElement} listbox
|
|
96
|
+
* @returns {void}
|
|
97
|
+
*/
|
|
98
|
+
export function clearHighlight(listbox) {
|
|
99
|
+
getHighlightedOption(listbox)?.removeAttribute('data-highlighted')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* getSelectedOptions
|
|
104
|
+
* Gets the options the person chose.
|
|
105
|
+
*
|
|
106
|
+
* @param {HTMLElement} listbox
|
|
107
|
+
* @returns {HTMLElement[]}
|
|
108
|
+
*/
|
|
109
|
+
export function getSelectedOptions(listbox) {
|
|
110
|
+
return Array.from(
|
|
111
|
+
listbox.querySelectorAll('[role="option"][aria-selected="true"]'),
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* selectOption
|
|
117
|
+
* Single-select replaces the selection. Multi-select toggles this option.
|
|
118
|
+
*
|
|
119
|
+
* @param {object} opts
|
|
120
|
+
* @param {HTMLElement} opts.listbox
|
|
121
|
+
* @param {HTMLElement} opts.option
|
|
122
|
+
* @returns {void}
|
|
123
|
+
*/
|
|
124
|
+
export function selectOption({ listbox, option }) {
|
|
125
|
+
const multiselectable =
|
|
126
|
+
listbox.getAttribute('aria-multiselectable') === 'true'
|
|
127
|
+
|
|
128
|
+
if (!multiselectable) {
|
|
129
|
+
for (const selected of getSelectedOptions(listbox)) {
|
|
130
|
+
selected.removeAttribute('aria-selected')
|
|
131
|
+
}
|
|
132
|
+
option.setAttribute('aria-selected', 'true')
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
option.setAttribute(
|
|
137
|
+
'aria-selected',
|
|
138
|
+
String(option.getAttribute('aria-selected') !== 'true'),
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* getAdjacentOption
|
|
144
|
+
* Gets the visible option before or after the highlighted one.
|
|
145
|
+
*
|
|
146
|
+
* With nothing highlighted, `next` gives the first option and `prev` the last.
|
|
147
|
+
*
|
|
148
|
+
* @param {object} opts
|
|
149
|
+
* @param {HTMLElement} opts.listbox
|
|
150
|
+
* @param {'next' | 'prev'} opts.direction
|
|
151
|
+
* @param {boolean} opts.loop Wrap past either end
|
|
152
|
+
* @returns {HTMLElement | null}
|
|
153
|
+
*/
|
|
154
|
+
export function getAdjacentOption({ listbox, direction, loop }) {
|
|
155
|
+
const options = getVisibleOptions(listbox)
|
|
156
|
+
if (!options.length) return null
|
|
157
|
+
|
|
158
|
+
const currentIndex = options.indexOf(getHighlightedOption(listbox))
|
|
159
|
+
const lastIndex = options.length - 1
|
|
160
|
+
|
|
161
|
+
if (currentIndex === -1)
|
|
162
|
+
return direction === 'next' ? options[0] : options[lastIndex]
|
|
163
|
+
|
|
164
|
+
let index = direction === 'next' ? currentIndex + 1 : currentIndex - 1
|
|
165
|
+
if (index > lastIndex) index = loop ? 0 : lastIndex
|
|
166
|
+
if (index < 0) index = loop ? lastIndex : 0
|
|
167
|
+
return options[index]
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Reads the rank filtering wrote. An unranked option sits at 0, the CSS default.
|
|
171
|
+
function getOrder(option) {
|
|
172
|
+
return Number(option.style.order) || 0
|
|
173
|
+
}
|
|
@@ -24,11 +24,9 @@
|
|
|
24
24
|
// If there is an icon set, we use Iconify.
|
|
25
25
|
// This stores the icon in localStorage after loading it for the first time. Subsequent loads will use the localStorage cached version.
|
|
26
26
|
|
|
27
|
-
//
|
|
28
|
-
iconData = getIcon(icon)
|
|
29
|
-
|
|
27
|
+
// Reads the cache after hydration so the first render matches the server
|
|
30
28
|
// If icon is not in cache, loads the icon from Iconify
|
|
31
|
-
|
|
29
|
+
onMount(async _ => (iconData = getIcon(icon) || (await loadIcon(icon))))
|
|
32
30
|
|
|
33
31
|
$: if (iconData) {
|
|
34
32
|
attributes.viewBox = `0 0 ${iconData.width} ${iconData.height}`
|