@splendidlabz/svelte 4.16.0 → 4.17.1
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 +90 -19
- package/components/Form/Combobox/Chips.svelte +58 -0
- package/components/Form/Combobox/ComboboxOptions.svelte +45 -1
- package/components/Form/Combobox/Select.svelte +195 -0
- package/components/Form/Combobox/SelectOptions.svelte +57 -0
- package/components/Form/Combobox/autocomplete.svelte.js +439 -106
- package/components/Form/Combobox/combobox.svelte.js +304 -59
- package/components/Form/Combobox/index.js +6 -2
- package/components/Form/Combobox/listbox.js +105 -17
- package/components/Form/Combobox/match.js +17 -4
- 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/{Select.svelte → NativeSelect.svelte} +20 -15
- package/components/Form/TextInput.svelte +11 -22
- package/components/Form/Textarea.svelte +22 -34
- package/components/Form/index.js +1 -1
- package/components/PerspectiveHover/perspective-hover.js +0 -1
- package/components/Spotlight/PointerPosition.js +0 -1
- package/package.json +3 -3
- package/components/Form/Combobox/AutocompleteInput.svelte +0 -40
- package/components/Form/Combobox/ComboboxInput.svelte +0 -50
- package/components/Form/Combobox/autocomplete-matching.js +0 -83
|
@@ -4,27 +4,49 @@
|
|
|
4
4
|
* Two states sit on an option and they are not the same thing. `data-highlighted` is the highlighted option — 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
5
|
*/
|
|
6
6
|
import { randomString } from '@splendidlabz/utils/dom'
|
|
7
|
+
import { removeAccents } from './match.js'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* prepareOptions
|
|
10
|
-
* Gives options written as plain `li` their role, id and
|
|
11
|
+
* Gives options written as plain `li` their role, id, label and value, and gives every option its check.
|
|
11
12
|
*
|
|
12
|
-
* Options rendered by Svelte already carry
|
|
13
|
+
* Options rendered by Svelte already carry their role, id, label and value in their markup, so only `li` without a role are touched for those.
|
|
14
|
+
*
|
|
15
|
+
* The check is copied in here rather than rendered by Svelte because slot options are markup the person wrote, and Svelte can't render a snippet inside it. Copying it into every option keeps slot options, item options and created options the same.
|
|
13
16
|
*
|
|
14
17
|
* @param {object} opts
|
|
15
18
|
* @param {HTMLElement} opts.listbox
|
|
16
19
|
* @param {boolean} [opts.multiple] Let more than one option be selected
|
|
20
|
+
* @param {HTMLElement | null} [opts.check] Check the options copy
|
|
17
21
|
* @returns {void}
|
|
18
22
|
*/
|
|
19
|
-
export function prepareOptions({ listbox, multiple = false }) {
|
|
20
|
-
|
|
23
|
+
export function prepareOptions({ listbox, multiple = false, check }) {
|
|
24
|
+
listbox.setAttribute('aria-multiselectable', String(multiple))
|
|
21
25
|
|
|
22
26
|
/** @type {NodeListOf<HTMLLIElement>} */
|
|
23
27
|
const plainOptions = listbox.querySelectorAll('li:not([role])')
|
|
24
28
|
for (const option of plainOptions) {
|
|
25
29
|
option.setAttribute('role', 'option')
|
|
26
30
|
option.id ||= randomString(8)
|
|
27
|
-
option.dataset.
|
|
31
|
+
option.dataset.label = option.textContent.trim()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// An option submits its value, and the label is the value when the markup names no other.
|
|
35
|
+
for (const option of getAllOptions(listbox)) {
|
|
36
|
+
option.dataset.value ??= option.dataset.label
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The order the markup wrote, so filtering can put the list back after ranking it
|
|
40
|
+
getAllOptions(listbox).forEach((option, index) => {
|
|
41
|
+
option.dataset.position ??= String(index)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// Comes after the label is read, so the check's own markup never becomes part of it
|
|
45
|
+
if (check) {
|
|
46
|
+
for (const option of getAllOptions(listbox)) {
|
|
47
|
+
if (!option.querySelector('.option-check'))
|
|
48
|
+
option.append(check.cloneNode(true))
|
|
49
|
+
}
|
|
28
50
|
}
|
|
29
51
|
|
|
30
52
|
// A multiselectable listbox states every option's selected state, not only the chosen ones.
|
|
@@ -36,6 +58,55 @@ export function prepareOptions({ listbox, multiple = false }) {
|
|
|
36
58
|
}
|
|
37
59
|
}
|
|
38
60
|
|
|
61
|
+
/**
|
|
62
|
+
* showOptionCount
|
|
63
|
+
* Caps the listbox at the height of its first `size` options, so that many show before it scrolls.
|
|
64
|
+
*
|
|
65
|
+
* The heights are summed rather than multiplied, so an option that wraps onto a second line still counts as one option.
|
|
66
|
+
*
|
|
67
|
+
* @param {object} opts
|
|
68
|
+
* @param {HTMLElement} opts.listbox
|
|
69
|
+
* @param {number} [opts.size] How many options to show
|
|
70
|
+
* @returns {void}
|
|
71
|
+
*/
|
|
72
|
+
export function showOptionCount({ listbox, size }) {
|
|
73
|
+
if (!size) return
|
|
74
|
+
|
|
75
|
+
const options = getVisibleOptions(listbox).slice(0, size)
|
|
76
|
+
if (!options.length) return
|
|
77
|
+
|
|
78
|
+
const height = options.reduce(
|
|
79
|
+
(total, option) => total + option.getBoundingClientRect().height,
|
|
80
|
+
0,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
// Options measure 0 inside a closed popover, and a cap of 0 would hide every one
|
|
84
|
+
if (!height) return
|
|
85
|
+
listbox.style.setProperty('--options-max-height', `${height}px`)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* sortOptions
|
|
90
|
+
* Puts the options into the DOM in the order they read, because a screen reader reads the DOM.
|
|
91
|
+
*
|
|
92
|
+
* Svelte renders a created value and the create option from their own blocks below the list, and each block inserts against an anchor node. An option moved past one of those anchors sends the block's next render above the list, so everything stays ahead of the first anchor.
|
|
93
|
+
*
|
|
94
|
+
* @param {object} opts
|
|
95
|
+
* @param {HTMLElement} opts.listbox
|
|
96
|
+
* @param {HTMLElement[]} opts.options Options in the order they read
|
|
97
|
+
* @returns {void}
|
|
98
|
+
*/
|
|
99
|
+
export function sortOptions({ listbox, options }) {
|
|
100
|
+
if (!options.length) return
|
|
101
|
+
|
|
102
|
+
const group = new Set(options)
|
|
103
|
+
const anchor = /** @type {HTMLElement} */ (
|
|
104
|
+
getAllOptions(listbox).findLast(option => group.has(option))
|
|
105
|
+
).nextSibling
|
|
106
|
+
|
|
107
|
+
for (const option of options) listbox.insertBefore(option, anchor)
|
|
108
|
+
}
|
|
109
|
+
|
|
39
110
|
/**
|
|
40
111
|
* getAllOptions
|
|
41
112
|
* Gets every option, including the hidden ones.
|
|
@@ -51,15 +122,11 @@ export function getAllOptions(listbox) {
|
|
|
51
122
|
* getVisibleOptions
|
|
52
123
|
* Gets the options that aren't filtered out, in the order they appear on screen.
|
|
53
124
|
*
|
|
54
|
-
* 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.
|
|
55
|
-
*
|
|
56
125
|
* @param {HTMLElement} listbox
|
|
57
126
|
* @returns {HTMLElement[]}
|
|
58
127
|
*/
|
|
59
128
|
export function getVisibleOptions(listbox) {
|
|
60
|
-
return getAllOptions(listbox)
|
|
61
|
-
.filter(option => !option.hidden)
|
|
62
|
-
.sort((a, b) => getOrder(a) - getOrder(b))
|
|
129
|
+
return getAllOptions(listbox).filter(option => !option.hidden)
|
|
63
130
|
}
|
|
64
131
|
|
|
65
132
|
/**
|
|
@@ -81,12 +148,17 @@ export function getHighlightedOption(listbox) {
|
|
|
81
148
|
* @param {HTMLElement} opts.listbox
|
|
82
149
|
* @param {HTMLElement} opts.option
|
|
83
150
|
* @param {boolean} [opts.shouldScroll] Scroll option into view
|
|
151
|
+
* @param {ScrollBehavior} [opts.behaviour] How the scroll runs
|
|
84
152
|
* @returns {void}
|
|
85
153
|
*/
|
|
86
|
-
export function highlightOption(
|
|
154
|
+
export function highlightOption(opts) {
|
|
155
|
+
const { listbox, option, shouldScroll = true, behaviour } = opts
|
|
156
|
+
|
|
87
157
|
clearHighlight(listbox)
|
|
88
158
|
option.dataset.highlighted = ''
|
|
89
|
-
if (shouldScroll)
|
|
159
|
+
if (shouldScroll) {
|
|
160
|
+
option.scrollIntoView({ block: 'nearest', behavior: behaviour })
|
|
161
|
+
}
|
|
90
162
|
}
|
|
91
163
|
|
|
92
164
|
/**
|
|
@@ -172,11 +244,27 @@ export function getAdjacentOption({ listbox, direction, loop }) {
|
|
|
172
244
|
}
|
|
173
245
|
|
|
174
246
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
247
|
+
* getOptionStartingWith
|
|
248
|
+
* Gets the first visible option whose label starts with typed text, searching from an option and wrapping past the end.
|
|
177
249
|
*
|
|
178
|
-
*
|
|
250
|
+
* Case and accents are ignored, so "e" reaches "Éire".
|
|
251
|
+
*
|
|
252
|
+
* @param {object} opts
|
|
253
|
+
* @param {HTMLElement} opts.listbox
|
|
254
|
+
* @param {string} opts.text Typed text
|
|
255
|
+
* @param {HTMLElement | null} [opts.from] Option the search starts at
|
|
256
|
+
* @returns {HTMLElement | null}
|
|
179
257
|
*/
|
|
180
|
-
function
|
|
181
|
-
|
|
258
|
+
export function getOptionStartingWith({ listbox, text, from }) {
|
|
259
|
+
const options = getVisibleOptions(listbox)
|
|
260
|
+
const start = from ? options.indexOf(from) : 0
|
|
261
|
+
const typed = removeAccents(text).toLowerCase()
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
[...options.slice(start), ...options.slice(0, start)].find(option =>
|
|
265
|
+
removeAccents(/** @type {string} */ (option.dataset.label))
|
|
266
|
+
.toLowerCase()
|
|
267
|
+
.startsWith(typed),
|
|
268
|
+
) ?? null
|
|
269
|
+
)
|
|
182
270
|
}
|
|
@@ -12,9 +12,10 @@
|
|
|
12
12
|
* @param {string} opts.text Text to search
|
|
13
13
|
* @param {string} opts.query Typed query
|
|
14
14
|
* @param {boolean} opts.insideWords Match inside words too
|
|
15
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
15
16
|
* @returns {[number, number][]} Sorted ranges, or empty when any word misses.
|
|
16
17
|
*/
|
|
17
|
-
export function match({ text, query, insideWords }) {
|
|
18
|
+
export function match({ text, query, insideWords, caseSensitive = false }) {
|
|
18
19
|
const removeAccents = AccentRemover()
|
|
19
20
|
const characters = Array.from(text).map(removeAccents)
|
|
20
21
|
let remainingText = characters.join('')
|
|
@@ -28,9 +29,10 @@ export function match({ text, query, insideWords }) {
|
|
|
28
29
|
for (const word of words) {
|
|
29
30
|
const boundary = !insideWords && /[a-z0-9_]/i.test(word[0]) ? '\\b' : ''
|
|
30
31
|
const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
31
|
-
const occurrence = new RegExp(
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const occurrence = new RegExp(
|
|
33
|
+
boundary + escapedWord,
|
|
34
|
+
caseSensitive ? '' : 'i',
|
|
35
|
+
).exec(remainingText)
|
|
34
36
|
if (!occurrence) return []
|
|
35
37
|
|
|
36
38
|
// Maps the index back to the original text, where "Æ" became "AE"
|
|
@@ -52,6 +54,17 @@ export function match({ text, query, insideWords }) {
|
|
|
52
54
|
return ranges.sort((a, b) => a[0] - b[0])
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
/**
|
|
58
|
+
* removeAccents
|
|
59
|
+
* Writes a string with plain letters, so "Café" reads as "Cafe".
|
|
60
|
+
*
|
|
61
|
+
* @param {string} string
|
|
62
|
+
* @returns {string}
|
|
63
|
+
*/
|
|
64
|
+
export function removeAccents(string) {
|
|
65
|
+
return Array.from(string).map(AccentRemover()).join('')
|
|
66
|
+
}
|
|
67
|
+
|
|
55
68
|
/**
|
|
56
69
|
* AccentRemover
|
|
57
70
|
* Replaces accented characters with their plain letters.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Matches option texts against a typed query.
|
|
3
|
+
*
|
|
4
|
+
* match strips diacritics before matching, so "cafe" matches "Café". Case is ignored the same way, until `caseSensitive` says otherwise.
|
|
5
|
+
*/
|
|
6
|
+
import { match } from './match.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* getMatchedIndexes
|
|
10
|
+
* Finds which options a query matches, best first. Each option has one text per key it matches by.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} opts
|
|
13
|
+
* @param {string[][]} opts.texts Each option's texts
|
|
14
|
+
* @param {string} opts.query Typed query
|
|
15
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
16
|
+
* @returns {number[]} Word-start matches on any text first, then inside-word matches.
|
|
17
|
+
*/
|
|
18
|
+
export function getMatchedIndexes({ texts, query, caseSensitive }) {
|
|
19
|
+
const wordStartIndexes = getMatchingIndexes({
|
|
20
|
+
texts,
|
|
21
|
+
query,
|
|
22
|
+
insideWords: false,
|
|
23
|
+
caseSensitive,
|
|
24
|
+
})
|
|
25
|
+
const insideWordIndexes = getMatchingIndexes({
|
|
26
|
+
texts,
|
|
27
|
+
query,
|
|
28
|
+
insideWords: true,
|
|
29
|
+
caseSensitive,
|
|
30
|
+
})
|
|
31
|
+
return Array.from(new Set([...wordStartIndexes, ...insideWordIndexes]))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* hasPerfectMatch
|
|
36
|
+
* Checks whether the query equals one of the texts, ignoring diacritics.
|
|
37
|
+
*
|
|
38
|
+
* @param {object} opts
|
|
39
|
+
* @param {string[]} opts.texts Texts to search
|
|
40
|
+
* @param {string} opts.query Typed query
|
|
41
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
42
|
+
* @returns {boolean}
|
|
43
|
+
*/
|
|
44
|
+
export function hasPerfectMatch({ texts, query, caseSensitive }) {
|
|
45
|
+
return texts.some(text => isPerfectMatch({ text, query, caseSensitive }))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* getMatchingIndexes
|
|
50
|
+
* Gets the indexes of the options where any text matches the query.
|
|
51
|
+
*
|
|
52
|
+
* @param {object} opts
|
|
53
|
+
* @param {string[][]} opts.texts Each option's texts
|
|
54
|
+
* @param {string} opts.query Typed query
|
|
55
|
+
* @param {boolean} opts.insideWords Match inside words too
|
|
56
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
57
|
+
* @returns {number[]}
|
|
58
|
+
*/
|
|
59
|
+
function getMatchingIndexes({ texts, query, insideWords, caseSensitive }) {
|
|
60
|
+
return texts.flatMap((optionTexts, index) => {
|
|
61
|
+
const isMatch = optionTexts.some(
|
|
62
|
+
text => match({ text, query, insideWords, caseSensitive }).length,
|
|
63
|
+
)
|
|
64
|
+
return isMatch ? [index] : []
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* isPerfectMatch
|
|
70
|
+
* Checks whether the query covers the whole text.
|
|
71
|
+
*
|
|
72
|
+
* @param {object} opts
|
|
73
|
+
* @param {string} opts.text Text to compare
|
|
74
|
+
* @param {string} opts.query Typed query
|
|
75
|
+
* @param {boolean} [opts.caseSensitive] Take case into account
|
|
76
|
+
*/
|
|
77
|
+
function isPerfectMatch({ text, query, caseSensitive }) {
|
|
78
|
+
if (text.length !== query.length) return false
|
|
79
|
+
|
|
80
|
+
const ranges = match({ text, query, insideWords: true, caseSensitive })
|
|
81
|
+
if (!ranges.length) return false
|
|
82
|
+
return ranges[0][0] === 0 && ranges[ranges.length - 1][1] === text.length
|
|
83
|
+
}
|