@splendidlabz/svelte 4.15.3 → 4.17.0
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/README.md +44 -0
- package/components/Form/Combobox/Autocomplete.svelte +208 -0
- package/components/Form/Combobox/AutocompleteOptions.svelte +99 -14
- package/components/Form/Combobox/Chips.svelte +58 -0
- package/components/Form/Combobox/ComboboxOptions.svelte +44 -1
- package/components/Form/Combobox/Select.svelte +195 -0
- package/components/Form/Combobox/SelectOptions.svelte +57 -0
- package/components/Form/Combobox/autocomplete.svelte.js +517 -96
- package/components/Form/Combobox/combobox.svelte.js +395 -49
- package/components/Form/Combobox/index.js +6 -2
- package/components/Form/Combobox/listbox.js +119 -22
- package/components/Form/Combobox/match.js +405 -0
- package/components/Form/Combobox/matching.js +83 -0
- package/components/Form/Combobox/select.svelte.js +533 -0
- package/components/Form/FieldMeta.svelte +50 -0
- package/components/Form/Honeypot.svelte +3 -1
- package/components/Form/{Select.svelte → NativeSelect.svelte} +20 -15
- package/components/Form/SearchInput.svelte +6 -0
- package/components/Form/TextInput.svelte +42 -22
- package/components/Form/Textarea.svelte +22 -34
- package/components/Form/index.js +1 -1
- package/package.json +3 -4
- package/components/Form/Combobox/AutocompleteInput.svelte +0 -26
- package/components/Form/Combobox/ComboboxInput.svelte +0 -37
- package/components/Form/Combobox/autocomplete-matching.js +0 -69
|
@@ -14,18 +14,33 @@ import {
|
|
|
14
14
|
highlightOption,
|
|
15
15
|
prepareOptions,
|
|
16
16
|
selectOption,
|
|
17
|
+
showOptionCount,
|
|
17
18
|
} from './listbox.js'
|
|
19
|
+
import { canSelectOption, deselect } from './select.svelte.js'
|
|
18
20
|
|
|
19
21
|
// ========================================
|
|
20
22
|
// Shared state
|
|
21
23
|
// ========================================
|
|
24
|
+
/**
|
|
25
|
+
* @type {Combobox[]}
|
|
26
|
+
*/
|
|
22
27
|
const comboboxes = $state([])
|
|
23
28
|
|
|
24
29
|
/**
|
|
25
30
|
* getCombobox
|
|
26
31
|
* Gets the combobox registered under an id.
|
|
27
32
|
*
|
|
33
|
+
* @typedef {object} Combobox
|
|
34
|
+
* @property {string} id Id of the options wrapper
|
|
35
|
+
* @property {Record<string, any>} state Holds `phase`: closed, open or closing
|
|
36
|
+
* @property {Record<string, any>} nodes Each part registers its own nodes
|
|
37
|
+
* @property {Record<string, any>} options
|
|
38
|
+
* @property {ReturnType<typeof initFloating>} [floating]
|
|
39
|
+
* @property {ResizeObserver} [resizeObserver] Keeps open options as wide as the box
|
|
40
|
+
* @property {() => void} [stopClosing] Ends the fade, leaving the options shown
|
|
41
|
+
*
|
|
28
42
|
* @param {string} id
|
|
43
|
+
* @returns {Combobox | undefined}
|
|
29
44
|
*/
|
|
30
45
|
export function getCombobox(id) {
|
|
31
46
|
return comboboxes.find(combobox => combobox.id === id)
|
|
@@ -40,6 +55,7 @@ export function getCombobox(id) {
|
|
|
40
55
|
* @param {object} [opts.state]
|
|
41
56
|
* @param {object} [opts.nodes]
|
|
42
57
|
* @param {object} [opts.options]
|
|
58
|
+
* @returns {Combobox}
|
|
43
59
|
*/
|
|
44
60
|
export function initCombobox({ id, state = {}, nodes = {}, options = {} }) {
|
|
45
61
|
const combobox = getCombobox(id)
|
|
@@ -50,7 +66,8 @@ export function initCombobox({ id, state = {}, nodes = {}, options = {} }) {
|
|
|
50
66
|
nodes: omitEmpty(nodes, { shallow: true }),
|
|
51
67
|
options,
|
|
52
68
|
})
|
|
53
|
-
|
|
69
|
+
// The store wraps what was pushed in a proxy, so the entry is read back
|
|
70
|
+
return /** @type {Combobox} */ (getCombobox(id))
|
|
54
71
|
}
|
|
55
72
|
|
|
56
73
|
combobox.state = { ...combobox.state, ...omitEmpty(state) }
|
|
@@ -64,43 +81,59 @@ export function initCombobox({ id, state = {}, nodes = {}, options = {} }) {
|
|
|
64
81
|
// ========================================
|
|
65
82
|
/**
|
|
66
83
|
* comboboxInput
|
|
67
|
-
* Lets an input open, move through and select the options it targets.
|
|
84
|
+
* Lets an input or a button open, move through and select the options it targets.
|
|
68
85
|
*
|
|
69
|
-
* @param {HTMLInputElement} node
|
|
86
|
+
* @param {HTMLInputElement | HTMLButtonElement} node
|
|
70
87
|
* @param {object} [props]
|
|
71
88
|
* @param {object} [props.options]
|
|
72
89
|
* @param {boolean} [props.options.loop] Wrap past either end
|
|
73
|
-
* @param {(
|
|
90
|
+
* @param {(option: HTMLElement) => void} [props.options.onselect] Receives the selected option
|
|
91
|
+
* @param {(selected: any) => void} [props.options.onselectedchange] Receives the selection after each change
|
|
74
92
|
*/
|
|
75
93
|
export function comboboxInput(node, props = {}) {
|
|
76
|
-
const id = node.getAttribute('target')
|
|
94
|
+
const id = /** @type {string} */ (node.getAttribute('target'))
|
|
77
95
|
const combobox = initCombobox({
|
|
78
96
|
id,
|
|
79
97
|
nodes: { input: node },
|
|
80
|
-
options: { loop:
|
|
98
|
+
options: { loop: false, ...omitEmpty(props.options) },
|
|
81
99
|
})
|
|
82
100
|
|
|
83
|
-
|
|
101
|
+
setInitialPhase(combobox)
|
|
84
102
|
|
|
85
103
|
const listeners = [
|
|
86
104
|
{ event: 'keydown', node, handler: onKeydown },
|
|
87
105
|
{ event: 'focusout', node, handler: onFocusout },
|
|
106
|
+
{ event: 'chipremove', node, handler: onChipremove },
|
|
88
107
|
]
|
|
89
108
|
|
|
90
109
|
addListeners(listeners)
|
|
91
110
|
|
|
92
111
|
return {
|
|
112
|
+
/**
|
|
113
|
+
* update
|
|
114
|
+
* Takes the input's new options.
|
|
115
|
+
*
|
|
116
|
+
* @param {typeof props} [nextProps]
|
|
117
|
+
*/
|
|
93
118
|
update(nextProps = {}) {
|
|
94
119
|
Object.assign(combobox.options, omitEmpty(nextProps.options))
|
|
95
120
|
},
|
|
96
121
|
destroy() {
|
|
97
122
|
removeListeners(listeners)
|
|
123
|
+
removeComboboxNodes({ combobox, names: ['input'] })
|
|
98
124
|
},
|
|
99
125
|
}
|
|
100
126
|
|
|
127
|
+
/**
|
|
128
|
+
* onKeydown
|
|
129
|
+
* Opens, moves through, selects and closes the options.
|
|
130
|
+
*
|
|
131
|
+
* @param {KeyboardEvent} event
|
|
132
|
+
*/
|
|
101
133
|
function onKeydown(event) {
|
|
102
134
|
const { listbox } = combobox.nodes
|
|
103
135
|
if (!listbox) return
|
|
136
|
+
if (!canChangeSelection(combobox)) return
|
|
104
137
|
|
|
105
138
|
const { key } = event
|
|
106
139
|
const isOpen = isComboboxOpen(combobox)
|
|
@@ -108,6 +141,9 @@ export function comboboxInput(node, props = {}) {
|
|
|
108
141
|
if (key === 'ArrowDown' || key === 'ArrowUp') {
|
|
109
142
|
event.preventDefault()
|
|
110
143
|
|
|
144
|
+
// An arrow means the options are wanted, so it also ends a close still fading
|
|
145
|
+
stopClosingCombobox(combobox)
|
|
146
|
+
|
|
111
147
|
if (!isOpen) {
|
|
112
148
|
if (getVisibleOptions(listbox).length) openCombobox(combobox)
|
|
113
149
|
return
|
|
@@ -118,7 +154,7 @@ export function comboboxInput(node, props = {}) {
|
|
|
118
154
|
direction: key === 'ArrowDown' ? 'next' : 'prev',
|
|
119
155
|
loop: combobox.options.loop,
|
|
120
156
|
})
|
|
121
|
-
if (option) highlight(combobox, option)
|
|
157
|
+
if (option) highlight(combobox, option, { behaviour: 'instant' })
|
|
122
158
|
return
|
|
123
159
|
}
|
|
124
160
|
|
|
@@ -128,20 +164,68 @@ export function comboboxInput(node, props = {}) {
|
|
|
128
164
|
return
|
|
129
165
|
}
|
|
130
166
|
|
|
167
|
+
// Backspace on empty text takes the last chip off
|
|
168
|
+
if (key === 'Backspace' && combobox.options.multiple && !node.value) {
|
|
169
|
+
const lastSelected = combobox.state.selected.at(-1)
|
|
170
|
+
if (lastSelected) deselect({ combobox, value: lastSelected.value })
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
131
174
|
const highlightedOption = getHighlightedOption(listbox)
|
|
132
|
-
if (!isOpen || !highlightedOption) return
|
|
133
175
|
|
|
176
|
+
// An exact match closes a single select, and Enter still picks it
|
|
134
177
|
if (key === 'Enter') {
|
|
135
|
-
|
|
136
|
-
|
|
178
|
+
// Create sorts last, so it heads the list only when nothing matched — and that is the only time Enter takes it
|
|
179
|
+
const [firstOption] = getVisibleOptions(listbox)
|
|
180
|
+
const createOption =
|
|
181
|
+
firstOption?.dataset.create === undefined ? null : firstOption
|
|
182
|
+
|
|
183
|
+
const option =
|
|
184
|
+
highlightedOption ?? combobox.options.getTypedOption?.() ?? createOption
|
|
185
|
+
// Enter submits only from a closed list that picks nothing
|
|
186
|
+
if (isOpen || option) event.preventDefault()
|
|
187
|
+
|
|
188
|
+
if (option) commitSelection(combobox, option)
|
|
189
|
+
// An option already says this text, so the field is cleared for the next one
|
|
190
|
+
else if (combobox.options.multiple && combobox.state.hasExactMatch)
|
|
191
|
+
writeInputText(combobox, '')
|
|
192
|
+
|
|
193
|
+
return
|
|
137
194
|
}
|
|
138
195
|
|
|
139
|
-
if (
|
|
196
|
+
if (!isOpen) return
|
|
197
|
+
|
|
198
|
+
if (isTab(event) && highlightedOption) {
|
|
199
|
+
commitSelection(combobox, highlightedOption)
|
|
200
|
+
}
|
|
140
201
|
}
|
|
141
202
|
|
|
203
|
+
/**
|
|
204
|
+
* onFocusout
|
|
205
|
+
* Puts the selected label back and closes the options.
|
|
206
|
+
*
|
|
207
|
+
* Text matching no option never stands as the answer.
|
|
208
|
+
*/
|
|
142
209
|
function onFocusout() {
|
|
210
|
+
if (node instanceof HTMLInputElement) {
|
|
211
|
+
const text = combobox.options.multiple
|
|
212
|
+
? ''
|
|
213
|
+
: (combobox.state.selected[0]?.label ?? '')
|
|
214
|
+
writeInputText(combobox, text)
|
|
215
|
+
}
|
|
143
216
|
if (combobox.nodes.listbox) closeCombobox(combobox)
|
|
144
217
|
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* onChipremove
|
|
221
|
+
* Takes the removed chip off the selection, and puts focus back here.
|
|
222
|
+
*
|
|
223
|
+
* @param {CustomEvent<{ value: string }>} event
|
|
224
|
+
*/
|
|
225
|
+
function onChipremove(event) {
|
|
226
|
+
deselect({ combobox, value: event.detail.value })
|
|
227
|
+
node.focus()
|
|
228
|
+
}
|
|
145
229
|
}
|
|
146
230
|
|
|
147
231
|
/**
|
|
@@ -151,45 +235,110 @@ export function comboboxInput(node, props = {}) {
|
|
|
151
235
|
* @param {HTMLElement} node Wrapper holding the listbox
|
|
152
236
|
* @param {object} [props]
|
|
153
237
|
* @param {boolean} [props.multiple] Let more than one option be selected
|
|
238
|
+
* @param {number} [props.size] How many options to show before scrolling
|
|
154
239
|
*/
|
|
155
240
|
export function comboboxOptions(node, props = {}) {
|
|
156
|
-
const listbox =
|
|
157
|
-
|
|
241
|
+
const listbox = /** @type {HTMLElement} */ (
|
|
242
|
+
node.querySelector('[role="listbox"]')
|
|
243
|
+
)
|
|
244
|
+
/** @type {HTMLElement | null} */
|
|
245
|
+
const check = node.querySelector('.option-check-source > .option-check')
|
|
246
|
+
let multiple = props.multiple
|
|
247
|
+
let size = props.size
|
|
248
|
+
prepareOptions({ listbox, multiple, check })
|
|
249
|
+
showOptionCount({ listbox, size })
|
|
250
|
+
|
|
251
|
+
// Search results and created values are added after this runs, so they're prepared as they arrive
|
|
252
|
+
const optionsObserver = new MutationObserver(() => {
|
|
253
|
+
prepareOptions({ listbox, multiple, check })
|
|
254
|
+
showOptionCount({ listbox, size })
|
|
255
|
+
})
|
|
256
|
+
optionsObserver.observe(listbox, { childList: true })
|
|
158
257
|
|
|
159
258
|
const combobox = initCombobox({
|
|
160
259
|
id: node.id,
|
|
161
260
|
nodes: { listbox, popover: node.hasAttribute('popover') ? node : null },
|
|
261
|
+
options: { size },
|
|
162
262
|
})
|
|
163
263
|
|
|
164
|
-
|
|
264
|
+
setInitialPhase(combobox)
|
|
265
|
+
|
|
266
|
+
/** @type {number | null} */
|
|
267
|
+
let lastPointerX = null
|
|
268
|
+
/** @type {number | null} */
|
|
269
|
+
let lastPointerY = null
|
|
165
270
|
|
|
166
271
|
const listeners = [
|
|
167
272
|
{ event: 'pointerdown', node, handler: keepInputFocus },
|
|
168
|
-
{ event: '
|
|
169
|
-
{ event: '
|
|
273
|
+
{ event: 'pointermove', node: listbox, handler: highlightHoveredOption },
|
|
274
|
+
{ event: 'pointerup', node: listbox, handler: selectReleasedOption },
|
|
170
275
|
]
|
|
171
276
|
|
|
172
277
|
addListeners(listeners)
|
|
173
278
|
|
|
174
279
|
return {
|
|
280
|
+
/**
|
|
281
|
+
* update
|
|
282
|
+
* Prepares options Svelte added, and takes `multiple` once the input registers it.
|
|
283
|
+
*
|
|
284
|
+
* @param {typeof props} [nextProps]
|
|
285
|
+
*/
|
|
286
|
+
update(nextProps = {}) {
|
|
287
|
+
multiple = nextProps.multiple
|
|
288
|
+
size = nextProps.size
|
|
289
|
+
prepareOptions({ listbox, multiple, check })
|
|
290
|
+
showOptionCount({ listbox, size })
|
|
291
|
+
},
|
|
175
292
|
destroy() {
|
|
176
293
|
removeListeners(listeners)
|
|
177
|
-
|
|
294
|
+
optionsObserver.disconnect()
|
|
295
|
+
removeComboboxNodes({ combobox, names: ['listbox', 'popover'] })
|
|
178
296
|
},
|
|
179
297
|
}
|
|
180
298
|
|
|
181
|
-
|
|
299
|
+
/**
|
|
300
|
+
* keepInputFocus
|
|
301
|
+
* Stops a press inside the list from blurring the input.
|
|
302
|
+
*
|
|
303
|
+
* @param {PointerEvent} event
|
|
304
|
+
*/
|
|
182
305
|
function keepInputFocus(event) {
|
|
183
306
|
event.preventDefault()
|
|
184
307
|
}
|
|
185
308
|
|
|
309
|
+
/**
|
|
310
|
+
* highlightHoveredOption
|
|
311
|
+
* Highlights the option under the pointer.
|
|
312
|
+
*
|
|
313
|
+
* @param {PointerEvent} event
|
|
314
|
+
*/
|
|
186
315
|
function highlightHoveredOption(event) {
|
|
187
|
-
|
|
316
|
+
if (!canChangeSelection(combobox)) return
|
|
317
|
+
|
|
318
|
+
// Scrolling fires pointermove at the resting coordinates, so an option arriving under a still pointer doesn't take the keyboard's place
|
|
319
|
+
const hasMoved =
|
|
320
|
+
event.clientX !== lastPointerX || event.clientY !== lastPointerY
|
|
321
|
+
lastPointerX = event.clientX
|
|
322
|
+
lastPointerY = event.clientY
|
|
323
|
+
if (!hasMoved) return
|
|
324
|
+
|
|
325
|
+
const option = getEventOption(event)
|
|
188
326
|
if (option) highlight(combobox, option, { shouldScroll: false })
|
|
189
327
|
}
|
|
190
328
|
|
|
191
|
-
|
|
192
|
-
|
|
329
|
+
/**
|
|
330
|
+
* selectReleasedOption
|
|
331
|
+
* Selects the option the pointer lifts over, so a press on the trigger can drag into the list and release on a choice.
|
|
332
|
+
*
|
|
333
|
+
* @param {PointerEvent} event
|
|
334
|
+
*/
|
|
335
|
+
function selectReleasedOption(event) {
|
|
336
|
+
if (!canChangeSelection(combobox)) return
|
|
337
|
+
|
|
338
|
+
// A list still fading is on its way out, so a release that lands on it picks nothing
|
|
339
|
+
if (combobox.state.phase === 'closing') return
|
|
340
|
+
|
|
341
|
+
const option = getEventOption(event)
|
|
193
342
|
if (option) commitSelection(combobox, option)
|
|
194
343
|
}
|
|
195
344
|
}
|
|
@@ -199,36 +348,59 @@ export function comboboxOptions(node, props = {}) {
|
|
|
199
348
|
// ========================================
|
|
200
349
|
/**
|
|
201
350
|
* openCombobox
|
|
202
|
-
* Shows the options under the input.
|
|
351
|
+
* Shows the options under the input's box, as wide as the box.
|
|
203
352
|
*
|
|
204
|
-
* Inline options are always shown, so this only changes popover options.
|
|
353
|
+
* Inline options are always shown, so this only changes popover options. Chips share the box with the input, so the box can change size while the options are open.
|
|
205
354
|
*
|
|
206
|
-
* @param {
|
|
355
|
+
* @param {Combobox} combobox
|
|
207
356
|
* @returns {void}
|
|
208
357
|
*/
|
|
209
358
|
export function openCombobox(combobox) {
|
|
210
|
-
if (isComboboxOpen(combobox)) return
|
|
211
|
-
|
|
212
359
|
const { input, popover } = combobox.nodes
|
|
360
|
+
|
|
361
|
+
stopClosingCombobox(combobox)
|
|
362
|
+
if (combobox.state.phase === 'open') return
|
|
363
|
+
|
|
364
|
+
combobox.state.phase = 'open'
|
|
213
365
|
input.setAttribute('aria-expanded', 'true')
|
|
214
366
|
|
|
215
|
-
|
|
367
|
+
const box = input.closest('.input-group') ?? input
|
|
368
|
+
function matchBoxWidth() {
|
|
369
|
+
popover.style.width = `${box.getBoundingClientRect().width}px`
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Hidden until the first position lands, because floating-ui computes it a tick after the popover is shown
|
|
373
|
+
matchBoxWidth()
|
|
374
|
+
popover.style.visibility = 'hidden'
|
|
216
375
|
popover.showPopover()
|
|
217
376
|
|
|
377
|
+
combobox.resizeObserver = new ResizeObserver(matchBoxWidth)
|
|
378
|
+
combobox.resizeObserver.observe(box)
|
|
379
|
+
|
|
218
380
|
combobox.floating ??= initFloating({
|
|
219
|
-
referenceNode:
|
|
381
|
+
referenceNode: box,
|
|
220
382
|
floatingNode: popover,
|
|
221
383
|
placement: 'bottom-start',
|
|
222
384
|
offset: 0,
|
|
223
385
|
})
|
|
224
386
|
combobox.floating.update()
|
|
387
|
+
|
|
388
|
+
// Options measure 0 while the popover is closed, so the cap is set here, once they have a height
|
|
389
|
+
showOptionCount({
|
|
390
|
+
listbox: combobox.nodes.listbox,
|
|
391
|
+
size: combobox.options.size,
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
requestAnimationFrame(() => popover.style.removeProperty('visibility'))
|
|
225
395
|
}
|
|
226
396
|
|
|
227
397
|
/**
|
|
228
398
|
* closeCombobox
|
|
229
399
|
* Hides popover options and clears the highlight.
|
|
230
400
|
*
|
|
231
|
-
*
|
|
401
|
+
* The options stay on screen while they fade, so the combobox still reads as open until the fade ends. A click on the input during the fade closes rather than reopens, and a reopen picks the options up where they are.
|
|
402
|
+
*
|
|
403
|
+
* @param {Combobox} combobox
|
|
232
404
|
* @returns {void}
|
|
233
405
|
*/
|
|
234
406
|
export function closeCombobox(combobox) {
|
|
@@ -237,40 +409,214 @@ export function closeCombobox(combobox) {
|
|
|
237
409
|
clearHighlight(listbox)
|
|
238
410
|
input.removeAttribute('aria-activedescendant')
|
|
239
411
|
|
|
240
|
-
|
|
412
|
+
// Nothing to close, or a close already running that's left to finish
|
|
413
|
+
if (!popover || combobox.state.phase !== 'open') return
|
|
414
|
+
|
|
415
|
+
function onFadeEnd(/** @type {TransitionEvent} */ event) {
|
|
416
|
+
if (event.target !== popover) return
|
|
417
|
+
if (event.propertyName !== 'opacity') return
|
|
418
|
+
hideOptions()
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Takes the options off screen once they have faded
|
|
422
|
+
function hideOptions() {
|
|
423
|
+
combobox.stopClosing?.()
|
|
424
|
+
combobox.state.phase = 'closed'
|
|
425
|
+
input.setAttribute('aria-expanded', 'false')
|
|
426
|
+
popover.hidePopover()
|
|
427
|
+
combobox.floating?.cleanup()
|
|
428
|
+
combobox.resizeObserver?.disconnect()
|
|
429
|
+
}
|
|
241
430
|
|
|
242
|
-
|
|
243
|
-
popover
|
|
244
|
-
|
|
431
|
+
// Nothing to fade — reduced motion, a zero duration, or a test environment
|
|
432
|
+
const duration = fadeDuration(popover)
|
|
433
|
+
if (!duration) return hideOptions()
|
|
434
|
+
|
|
435
|
+
// Fades the options out here rather than leaving it to the browser. Safari runs no transition when it tears a popover down, so the list would vanish instead of fading.
|
|
436
|
+
combobox.state.phase = 'closing'
|
|
437
|
+
popover.style.opacity = '0'
|
|
438
|
+
popover.addEventListener('transitionend', onFadeEnd)
|
|
439
|
+
|
|
440
|
+
// Covers a fade the engine skips
|
|
441
|
+
const timer = setTimeout(hideOptions, duration + 50)
|
|
442
|
+
|
|
443
|
+
combobox.stopClosing = function stopClosing() {
|
|
444
|
+
combobox.stopClosing = undefined
|
|
445
|
+
combobox.state.phase = 'open'
|
|
446
|
+
popover.removeEventListener('transitionend', onFadeEnd)
|
|
447
|
+
clearTimeout(timer)
|
|
448
|
+
popover.style.removeProperty('opacity')
|
|
449
|
+
}
|
|
245
450
|
}
|
|
246
451
|
|
|
247
|
-
|
|
248
|
-
|
|
452
|
+
/**
|
|
453
|
+
* stopClosingCombobox
|
|
454
|
+
* Ends a close still fading, so the options stay on screen at full strength.
|
|
455
|
+
*
|
|
456
|
+
* @param {Combobox} combobox
|
|
457
|
+
* @returns {void}
|
|
458
|
+
*/
|
|
459
|
+
export function stopClosingCombobox(combobox) {
|
|
460
|
+
combobox.stopClosing?.()
|
|
249
461
|
}
|
|
250
462
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
463
|
+
/**
|
|
464
|
+
* fadeDuration
|
|
465
|
+
* Reads how long the options take to fade, in milliseconds.
|
|
466
|
+
*
|
|
467
|
+
* @param {HTMLElement} popover
|
|
468
|
+
* @returns {number}
|
|
469
|
+
*/
|
|
470
|
+
function fadeDuration(popover) {
|
|
471
|
+
const { transitionDuration, transitionProperty } = getComputedStyle(popover)
|
|
472
|
+
const properties = (transitionProperty ?? '')
|
|
473
|
+
.split(',')
|
|
474
|
+
.map(one => one.trim())
|
|
475
|
+
const durations = (transitionDuration ?? '').split(',').map(one => one.trim())
|
|
476
|
+
const index = properties.indexOf('opacity')
|
|
477
|
+
const duration = durations[index] || durations[0] || '0s'
|
|
478
|
+
|
|
479
|
+
const milliseconds = duration.endsWith('ms')
|
|
480
|
+
? parseFloat(duration)
|
|
481
|
+
: parseFloat(duration) * 1000
|
|
482
|
+
|
|
483
|
+
// A test environment reports no transition at all, and NaN would make the timer fire on the next tick
|
|
484
|
+
return Number.isFinite(milliseconds) ? milliseconds : 0
|
|
256
485
|
}
|
|
257
486
|
|
|
258
|
-
|
|
259
|
-
|
|
487
|
+
/**
|
|
488
|
+
* isComboboxOpen
|
|
489
|
+
* Reads whether the options are on screen, counting a close still fading.
|
|
490
|
+
*
|
|
491
|
+
* @param {Combobox} combobox
|
|
492
|
+
*/
|
|
493
|
+
export function isComboboxOpen(combobox) {
|
|
494
|
+
return combobox.state.phase !== 'closed'
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* highlight
|
|
499
|
+
* Highlights an option and points the input at it.
|
|
500
|
+
*
|
|
501
|
+
* @param {Combobox} combobox
|
|
502
|
+
* @param {HTMLElement} option
|
|
503
|
+
* @param {object} [opts]
|
|
504
|
+
* @param {boolean} [opts.shouldScroll] Scroll option into view
|
|
505
|
+
* @param {ScrollBehavior} [opts.behaviour] How the scroll runs. Falls back to the CSS `scroll-behavior`.
|
|
506
|
+
*/
|
|
507
|
+
export function highlight(
|
|
508
|
+
combobox,
|
|
509
|
+
option,
|
|
510
|
+
{ shouldScroll = true, behaviour } = {},
|
|
511
|
+
) {
|
|
512
|
+
highlightOption({
|
|
513
|
+
listbox: combobox.nodes.listbox,
|
|
514
|
+
option,
|
|
515
|
+
shouldScroll,
|
|
516
|
+
behaviour,
|
|
517
|
+
})
|
|
260
518
|
combobox.nodes.input.setAttribute('aria-activedescendant', option.id)
|
|
261
519
|
}
|
|
262
520
|
|
|
263
|
-
|
|
264
|
-
|
|
521
|
+
/**
|
|
522
|
+
* commitSelection
|
|
523
|
+
* Marks the option chosen, then dispatches `input` so `bind:value` and filtering see the new text.
|
|
524
|
+
*
|
|
525
|
+
* @param {Combobox} combobox
|
|
526
|
+
* @param {HTMLElement} option
|
|
527
|
+
*/
|
|
528
|
+
export function commitSelection(combobox, option) {
|
|
265
529
|
const { input, listbox } = combobox.nodes
|
|
266
|
-
|
|
530
|
+
|
|
531
|
+
if (!canSelectOption(combobox, option)) return
|
|
267
532
|
|
|
268
533
|
selectOption({ listbox, option })
|
|
534
|
+
|
|
535
|
+
// A layer above owns the items, so it gets the element and maps it back.
|
|
536
|
+
combobox.options.onselect?.(option)
|
|
537
|
+
|
|
538
|
+
// A button shows its selection through Svelte, not through a value
|
|
539
|
+
if (input instanceof HTMLInputElement) {
|
|
540
|
+
// Selecting one of many leaves the text behind: the selection shows as a chip, and the next one is typed right after it.
|
|
541
|
+
const nextText = combobox.options.multiple
|
|
542
|
+
? ''
|
|
543
|
+
: /** @type {string} */ (option.dataset.label)
|
|
544
|
+
writeInputText(combobox, nextText)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (!combobox.options.multiple) closeCombobox(combobox)
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* canChangeSelection
|
|
552
|
+
* Reads whether the input is neither disabled nor readonly.
|
|
553
|
+
*
|
|
554
|
+
* Inline options stay clickable while the input can't change, so the options check this too.
|
|
555
|
+
*
|
|
556
|
+
* @param {Combobox} combobox
|
|
557
|
+
*/
|
|
558
|
+
function canChangeSelection(combobox) {
|
|
559
|
+
const { input } = combobox.nodes
|
|
560
|
+
if (!input) return true
|
|
561
|
+
return !input.disabled && !input.hasAttribute('readonly')
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* writeInputText
|
|
566
|
+
* Writes the input's text, then dispatches `input` so `bind:value` and filtering see it.
|
|
567
|
+
*
|
|
568
|
+
* @param {Combobox} combobox
|
|
569
|
+
* @param {string} text
|
|
570
|
+
*/
|
|
571
|
+
function writeInputText(combobox, text) {
|
|
572
|
+
const { input } = combobox.nodes
|
|
573
|
+
// Searching skips the text a selection wrote
|
|
269
574
|
combobox.state.selectedText = text
|
|
270
575
|
input.value = text
|
|
271
576
|
input.dispatchEvent(new Event('input', { bubbles: true }))
|
|
272
|
-
|
|
577
|
+
}
|
|
273
578
|
|
|
274
|
-
|
|
275
|
-
|
|
579
|
+
/**
|
|
580
|
+
* setInitialPhase
|
|
581
|
+
* Starts the combobox open when its options are inline, since those are always shown.
|
|
582
|
+
*
|
|
583
|
+
* @param {Combobox} combobox
|
|
584
|
+
*/
|
|
585
|
+
function setInitialPhase(combobox) {
|
|
586
|
+
const { input, listbox, popover } = combobox.nodes
|
|
587
|
+
if (!input || !listbox) return
|
|
588
|
+
|
|
589
|
+
combobox.state.phase = popover ? 'closed' : 'open'
|
|
590
|
+
input.setAttribute('aria-expanded', String(!popover))
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* removeComboboxNodes
|
|
595
|
+
* Drops a destroyed part's nodes, and the entry once no part is left.
|
|
596
|
+
*
|
|
597
|
+
* @param {object} opts
|
|
598
|
+
* @param {Combobox} opts.combobox
|
|
599
|
+
* @param {string[]} opts.names Node names the part registered
|
|
600
|
+
*/
|
|
601
|
+
function removeComboboxNodes({ combobox, names }) {
|
|
602
|
+
// Positioning points at the old nodes, so the next open rebuilds it
|
|
603
|
+
combobox.floating?.cleanup()
|
|
604
|
+
delete combobox.floating
|
|
605
|
+
combobox.resizeObserver?.disconnect()
|
|
606
|
+
|
|
607
|
+
for (const name of names) delete combobox.nodes[name]
|
|
608
|
+
if (combobox.nodes.input || combobox.nodes.listbox) return
|
|
609
|
+
|
|
610
|
+
comboboxes.splice(comboboxes.indexOf(combobox), 1)
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* getEventOption
|
|
615
|
+
* Gets the option an event happened on.
|
|
616
|
+
*
|
|
617
|
+
* @param {Event} event
|
|
618
|
+
* @returns {HTMLElement | null}
|
|
619
|
+
*/
|
|
620
|
+
function getEventOption(event) {
|
|
621
|
+
return /** @type {HTMLElement} */ (event.target).closest('[role="option"]')
|
|
276
622
|
}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Autocomplete from './Autocomplete.svelte'
|
|
2
2
|
import AutocompleteOptions from './AutocompleteOptions.svelte'
|
|
3
|
+
import Select from './Select.svelte'
|
|
4
|
+
import SelectOptions from './SelectOptions.svelte'
|
|
3
5
|
import TimepickerInput from './TimepickerInput.svelte'
|
|
4
6
|
import TimepickerOptions from './TimepickerOptions.svelte'
|
|
5
7
|
|
|
6
8
|
export {
|
|
7
|
-
|
|
9
|
+
Autocomplete,
|
|
8
10
|
AutocompleteOptions,
|
|
11
|
+
Select,
|
|
12
|
+
SelectOptions,
|
|
9
13
|
TimepickerInput,
|
|
10
14
|
TimepickerOptions,
|
|
11
15
|
}
|