@splendidlabz/svelte 4.15.2 → 4.16.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.
@@ -0,0 +1,182 @@
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 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
+ */
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
+ /** @type {NodeListOf<HTMLLIElement>} */
23
+ const plainOptions = listbox.querySelectorAll('li:not([role])')
24
+ for (const option of plainOptions) {
25
+ option.setAttribute('role', 'option')
26
+ option.id ||= randomString(8)
27
+ option.dataset.text = option.textContent.trim()
28
+ }
29
+
30
+ // A multiselectable listbox states every option's selected state, not only the chosen ones.
31
+ if (!multiple) return
32
+ for (const option of getAllOptions(listbox)) {
33
+ if (!option.hasAttribute('aria-selected')) {
34
+ option.setAttribute('aria-selected', 'false')
35
+ }
36
+ }
37
+ }
38
+
39
+ /**
40
+ * getAllOptions
41
+ * Gets every option, including the hidden ones.
42
+ *
43
+ * @param {HTMLElement} listbox
44
+ * @returns {HTMLElement[]}
45
+ */
46
+ export function getAllOptions(listbox) {
47
+ return Array.from(listbox.querySelectorAll('[role="option"]'))
48
+ }
49
+
50
+ /**
51
+ * getVisibleOptions
52
+ * Gets the options that aren't filtered out, in the order they appear on screen.
53
+ *
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
+ * @param {HTMLElement} listbox
57
+ * @returns {HTMLElement[]}
58
+ */
59
+ export function getVisibleOptions(listbox) {
60
+ return getAllOptions(listbox)
61
+ .filter(option => !option.hidden)
62
+ .sort((a, b) => getOrder(a) - getOrder(b))
63
+ }
64
+
65
+ /**
66
+ * getHighlightedOption
67
+ * Gets the highlighted option.
68
+ *
69
+ * @param {HTMLElement} listbox
70
+ * @returns {HTMLElement | null}
71
+ */
72
+ export function getHighlightedOption(listbox) {
73
+ return listbox.querySelector('[role="option"][data-highlighted]')
74
+ }
75
+
76
+ /**
77
+ * highlightOption
78
+ * Highlights one option.
79
+ *
80
+ * @param {object} opts
81
+ * @param {HTMLElement} opts.listbox
82
+ * @param {HTMLElement} opts.option
83
+ * @param {boolean} [opts.shouldScroll] Scroll option into view
84
+ * @returns {void}
85
+ */
86
+ export function highlightOption({ listbox, option, shouldScroll = true }) {
87
+ clearHighlight(listbox)
88
+ option.dataset.highlighted = ''
89
+ if (shouldScroll) option.scrollIntoView({ block: 'nearest' })
90
+ }
91
+
92
+ /**
93
+ * clearHighlight
94
+ * Removes the highlight from every option.
95
+ *
96
+ * @param {HTMLElement} listbox
97
+ * @returns {void}
98
+ */
99
+ export function clearHighlight(listbox) {
100
+ getHighlightedOption(listbox)?.removeAttribute('data-highlighted')
101
+ }
102
+
103
+ /**
104
+ * getSelectedOptions
105
+ * Gets the options the person chose.
106
+ *
107
+ * @param {HTMLElement} listbox
108
+ * @returns {HTMLElement[]}
109
+ */
110
+ export function getSelectedOptions(listbox) {
111
+ return Array.from(
112
+ listbox.querySelectorAll('[role="option"][aria-selected="true"]'),
113
+ )
114
+ }
115
+
116
+ /**
117
+ * selectOption
118
+ * Single-select replaces the selection. Multi-select toggles this option.
119
+ *
120
+ * @param {object} opts
121
+ * @param {HTMLElement} opts.listbox
122
+ * @param {HTMLElement} opts.option
123
+ * @returns {void}
124
+ */
125
+ export function selectOption({ listbox, option }) {
126
+ const multiselectable =
127
+ listbox.getAttribute('aria-multiselectable') === 'true'
128
+
129
+ if (!multiselectable) {
130
+ for (const selected of getSelectedOptions(listbox)) {
131
+ selected.removeAttribute('aria-selected')
132
+ }
133
+ option.setAttribute('aria-selected', 'true')
134
+ return
135
+ }
136
+
137
+ option.setAttribute(
138
+ 'aria-selected',
139
+ String(option.getAttribute('aria-selected') !== 'true'),
140
+ )
141
+ }
142
+
143
+ /**
144
+ * getAdjacentOption
145
+ * Gets the visible option before or after the highlighted one.
146
+ *
147
+ * With nothing highlighted, `next` gives the first option and `prev` the last.
148
+ *
149
+ * @param {object} opts
150
+ * @param {HTMLElement} opts.listbox
151
+ * @param {'next' | 'prev'} opts.direction
152
+ * @param {boolean} opts.loop Wrap past either end
153
+ * @returns {HTMLElement | null}
154
+ */
155
+ export function getAdjacentOption({ listbox, direction, loop }) {
156
+ const options = getVisibleOptions(listbox)
157
+ if (!options.length) return null
158
+
159
+ const highlightedOption = getHighlightedOption(listbox)
160
+ const currentIndex = highlightedOption
161
+ ? options.indexOf(highlightedOption)
162
+ : -1
163
+ const lastIndex = options.length - 1
164
+
165
+ if (currentIndex === -1)
166
+ return direction === 'next' ? options[0] : options[lastIndex]
167
+
168
+ let index = direction === 'next' ? currentIndex + 1 : currentIndex - 1
169
+ if (index > lastIndex) index = loop ? 0 : lastIndex
170
+ if (index < 0) index = loop ? lastIndex : 0
171
+ return options[index]
172
+ }
173
+
174
+ /**
175
+ * getOrder
176
+ * Reads the rank filtering wrote. An unranked option sits at 0, the CSS default.
177
+ *
178
+ * @param {HTMLElement} option
179
+ */
180
+ function getOrder(option) {
181
+ return Number(option.style.order) || 0
182
+ }
@@ -0,0 +1,392 @@
1
+ /**
2
+ * @file Matches query words inside text, ignoring accents.
3
+ *
4
+ * Copied from autosuggest-highlight 3.3.4 (match) and its bundled remove-accents, both MIT. autosuggest-highlight ships CommonJS only, so importing it from this package broke hydration in `astro dev`.
5
+ */
6
+
7
+ /**
8
+ * match
9
+ * Finds where every word of a query matches inside a text.
10
+ *
11
+ * @param {object} opts
12
+ * @param {string} opts.text Text to search
13
+ * @param {string} opts.query Typed query
14
+ * @param {boolean} opts.insideWords Match inside words too
15
+ * @returns {[number, number][]} Sorted ranges, or empty when any word misses.
16
+ */
17
+ export function match({ text, query, insideWords }) {
18
+ const removeAccents = AccentRemover()
19
+ const characters = Array.from(text).map(removeAccents)
20
+ let remainingText = characters.join('')
21
+ const words = removeAccents(query)
22
+ .trim()
23
+ .split(/\s+/)
24
+ .filter(word => word.length > 0)
25
+
26
+ /** @type {[number, number][]} */
27
+ const ranges = []
28
+ for (const word of words) {
29
+ const boundary = !insideWords && /[a-z0-9_]/i.test(word[0]) ? '\\b' : ''
30
+ const escapedWord = word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
31
+ const occurrence = new RegExp(boundary + escapedWord, 'i').exec(
32
+ remainingText,
33
+ )
34
+ if (!occurrence) return []
35
+
36
+ // Maps the index back to the original text, where "Æ" became "AE"
37
+ const { index } = occurrence
38
+ const startOffset = index - characters.slice(0, index).join('').length
39
+ const endOffset =
40
+ word.length - characters.slice(index, index + word.length).join('').length
41
+ const start = index + startOffset
42
+ const end = index + word.length + startOffset + endOffset
43
+ if (start !== end) ranges.push([start, end])
44
+
45
+ // Blanks the matched part so later words skip it
46
+ remainingText =
47
+ remainingText.slice(0, index) +
48
+ ' '.repeat(word.length) +
49
+ remainingText.slice(index + word.length)
50
+ }
51
+
52
+ return ranges.sort((a, b) => a[0] - b[0])
53
+ }
54
+
55
+ /**
56
+ * AccentRemover
57
+ * Replaces accented characters with their plain letters.
58
+ *
59
+ * @returns {(string: string) => string}
60
+ */
61
+ function AccentRemover() {
62
+ /** @type {Record<string, string>} */
63
+ const accentMap = {
64
+ À: 'A',
65
+ Á: 'A',
66
+ Â: 'A',
67
+ Ã: 'A',
68
+ Ä: 'A',
69
+ Å: 'A',
70
+ Ấ: 'A',
71
+ Ắ: 'A',
72
+ Ẳ: 'A',
73
+ Ẵ: 'A',
74
+ Ặ: 'A',
75
+ Æ: 'AE',
76
+ Ầ: 'A',
77
+ Ằ: 'A',
78
+ Ȃ: 'A',
79
+ Ç: 'C',
80
+ Ḉ: 'C',
81
+ È: 'E',
82
+ É: 'E',
83
+ Ê: 'E',
84
+ Ë: 'E',
85
+ Ế: 'E',
86
+ Ḗ: 'E',
87
+ Ề: 'E',
88
+ Ḕ: 'E',
89
+ Ḝ: 'E',
90
+ Ȇ: 'E',
91
+ Ì: 'I',
92
+ Í: 'I',
93
+ Î: 'I',
94
+ Ï: 'I',
95
+ Ḯ: 'I',
96
+ Ȋ: 'I',
97
+ Ð: 'D',
98
+ Ñ: 'N',
99
+ Ò: 'O',
100
+ Ó: 'O',
101
+ Ô: 'O',
102
+ Õ: 'O',
103
+ Ö: 'O',
104
+ Ø: 'O',
105
+ Ố: 'O',
106
+ Ṍ: 'O',
107
+ Ṓ: 'O',
108
+ Ȏ: 'O',
109
+ Ù: 'U',
110
+ Ú: 'U',
111
+ Û: 'U',
112
+ Ü: 'U',
113
+ Ý: 'Y',
114
+ à: 'a',
115
+ á: 'a',
116
+ â: 'a',
117
+ ã: 'a',
118
+ ä: 'a',
119
+ å: 'a',
120
+ ấ: 'a',
121
+ ắ: 'a',
122
+ ẳ: 'a',
123
+ ẵ: 'a',
124
+ ặ: 'a',
125
+ æ: 'ae',
126
+ ầ: 'a',
127
+ ằ: 'a',
128
+ ȃ: 'a',
129
+ ç: 'c',
130
+ ḉ: 'c',
131
+ è: 'e',
132
+ é: 'e',
133
+ ê: 'e',
134
+ ë: 'e',
135
+ ế: 'e',
136
+ ḗ: 'e',
137
+ ề: 'e',
138
+ ḕ: 'e',
139
+ ḝ: 'e',
140
+ ȇ: 'e',
141
+ ì: 'i',
142
+ í: 'i',
143
+ î: 'i',
144
+ ï: 'i',
145
+ ḯ: 'i',
146
+ ȋ: 'i',
147
+ ð: 'd',
148
+ ñ: 'n',
149
+ ò: 'o',
150
+ ó: 'o',
151
+ ô: 'o',
152
+ õ: 'o',
153
+ ö: 'o',
154
+ ø: 'o',
155
+ ố: 'o',
156
+ ṍ: 'o',
157
+ ṓ: 'o',
158
+ ȏ: 'o',
159
+ ù: 'u',
160
+ ú: 'u',
161
+ û: 'u',
162
+ ü: 'u',
163
+ ý: 'y',
164
+ ÿ: 'y',
165
+ Ā: 'A',
166
+ ā: 'a',
167
+ Ă: 'A',
168
+ ă: 'a',
169
+ Ą: 'A',
170
+ ą: 'a',
171
+ Ć: 'C',
172
+ ć: 'c',
173
+ Ĉ: 'C',
174
+ ĉ: 'c',
175
+ Ċ: 'C',
176
+ ċ: 'c',
177
+ Č: 'C',
178
+ č: 'c',
179
+ Ď: 'D',
180
+ ď: 'd',
181
+ Đ: 'D',
182
+ đ: 'd',
183
+ Ē: 'E',
184
+ ē: 'e',
185
+ Ĕ: 'E',
186
+ ĕ: 'e',
187
+ Ė: 'E',
188
+ ė: 'e',
189
+ Ę: 'E',
190
+ ę: 'e',
191
+ Ě: 'E',
192
+ ě: 'e',
193
+ Ĝ: 'G',
194
+ Ǵ: 'G',
195
+ ĝ: 'g',
196
+ ǵ: 'g',
197
+ Ğ: 'G',
198
+ ğ: 'g',
199
+ Ġ: 'G',
200
+ ġ: 'g',
201
+ Ģ: 'G',
202
+ ģ: 'g',
203
+ Ĥ: 'H',
204
+ ĥ: 'h',
205
+ Ħ: 'H',
206
+ ħ: 'h',
207
+ Ḫ: 'H',
208
+ ḫ: 'h',
209
+ Ĩ: 'I',
210
+ ĩ: 'i',
211
+ Ī: 'I',
212
+ ī: 'i',
213
+ Ĭ: 'I',
214
+ ĭ: 'i',
215
+ Į: 'I',
216
+ į: 'i',
217
+ İ: 'I',
218
+ ı: 'i',
219
+ IJ: 'IJ',
220
+ ij: 'ij',
221
+ Ĵ: 'J',
222
+ ĵ: 'j',
223
+ Ķ: 'K',
224
+ ķ: 'k',
225
+ Ḱ: 'K',
226
+ ḱ: 'k',
227
+ Ĺ: 'L',
228
+ ĺ: 'l',
229
+ Ļ: 'L',
230
+ ļ: 'l',
231
+ Ľ: 'L',
232
+ ľ: 'l',
233
+ Ŀ: 'L',
234
+ ŀ: 'l',
235
+ Ł: 'l',
236
+ ł: 'l',
237
+ Ḿ: 'M',
238
+ ḿ: 'm',
239
+ Ń: 'N',
240
+ ń: 'n',
241
+ Ņ: 'N',
242
+ ņ: 'n',
243
+ Ň: 'N',
244
+ ň: 'n',
245
+ ʼn: 'n',
246
+ Ō: 'O',
247
+ ō: 'o',
248
+ Ŏ: 'O',
249
+ ŏ: 'o',
250
+ Ő: 'O',
251
+ ő: 'o',
252
+ Œ: 'OE',
253
+ œ: 'oe',
254
+ Ŕ: 'R',
255
+ ŕ: 'r',
256
+ Ŗ: 'R',
257
+ ŗ: 'r',
258
+ Ř: 'R',
259
+ ř: 'r',
260
+ Ȓ: 'R',
261
+ ȓ: 'r',
262
+ Ś: 'S',
263
+ ś: 's',
264
+ Ŝ: 'S',
265
+ ŝ: 's',
266
+ Ş: 'S',
267
+ Ș: 'S',
268
+ ș: 's',
269
+ ş: 's',
270
+ Š: 'S',
271
+ š: 's',
272
+ Ţ: 'T',
273
+ ţ: 't',
274
+ ț: 't',
275
+ Ț: 'T',
276
+ Ť: 'T',
277
+ ť: 't',
278
+ Ŧ: 'T',
279
+ ŧ: 't',
280
+ Ũ: 'U',
281
+ ũ: 'u',
282
+ Ū: 'U',
283
+ ū: 'u',
284
+ Ŭ: 'U',
285
+ ŭ: 'u',
286
+ Ů: 'U',
287
+ ů: 'u',
288
+ Ű: 'U',
289
+ ű: 'u',
290
+ Ų: 'U',
291
+ ų: 'u',
292
+ Ȗ: 'U',
293
+ ȗ: 'u',
294
+ Ŵ: 'W',
295
+ ŵ: 'w',
296
+ Ẃ: 'W',
297
+ ẃ: 'w',
298
+ Ŷ: 'Y',
299
+ ŷ: 'y',
300
+ Ÿ: 'Y',
301
+ Ź: 'Z',
302
+ ź: 'z',
303
+ Ż: 'Z',
304
+ ż: 'z',
305
+ Ž: 'Z',
306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
+ Ǩ: 'K',
379
+ ǩ: 'k',
380
+ Ṧ: 'S',
381
+ ṧ: 's',
382
+ Ḑ: 'D',
383
+ ḑ: 'd',
384
+ Ȩ: 'E',
385
+ ȩ: 'e',
386
+ Ḩ: 'H',
387
+ ḩ: 'h',
388
+ }
389
+
390
+ return string =>
391
+ Array.from(string, character => accentMap[character] ?? character).join('')
392
+ }
@@ -9,11 +9,13 @@
9
9
  } = $props()
10
10
  </script>
11
11
 
12
- <!-- `autocomplete="off"` keeps the browser from filling a field named after a real one, which would reject a real person as a bot. -->
12
+ <!-- `autocomplete="off"` keeps the browser from filling a field named after a real one, which would reject a real person as a bot. `tabindex` and `aria-hidden` keep keyboard and screen reader users out of the field. -->
13
13
  <TextInput
14
14
  {...rest}
15
15
  {name}
16
16
  class="form-field-helper {klass}"
17
17
  autocomplete="off"
18
+ tabindex="-1"
19
+ aria-hidden="true"
18
20
  bind:value
19
21
  />
@@ -1,5 +1,11 @@
1
1
  <script>
2
2
  import TextInput from './TextInput.svelte'
3
+
4
+ /**
5
+ * @import { ComponentProps } from 'svelte'
6
+ */
7
+
8
+ /** @type {ComponentProps<typeof TextInput>} */
3
9
  const { ...rest } = $props()
4
10
  </script>
5
11
 
@@ -4,6 +4,37 @@
4
4
  import { init } from '../../utils'
5
5
  import Markdown from '../Markdown/Markdown.svelte'
6
6
 
7
+ /**
8
+ * @import { Snippet } from 'svelte'
9
+ */
10
+
11
+ /**
12
+ * @typedef {object} TextInputProps
13
+ * @property {string} name
14
+ * @property {string} [value]
15
+ * @property {HTMLInputElement | null} [ref]
16
+ * @property {string} [class]
17
+ * @property {string} [style]
18
+ * @property {string} [labelClass]
19
+ * @property {string} [hintClass]
20
+ * @property {string} [errorClass]
21
+ * @property {string} [inputGroupClass]
22
+ * @property {string} [fieldClass]
23
+ * @property {Snippet} [before] Content before the input
24
+ * @property {Snippet} [after] Content after the input
25
+ * @property {Snippet} [children]
26
+ * @property {string} [id]
27
+ * @property {'text' | 'email' | 'password' | 'search' | 'url' | 'tel'} [type]
28
+ * @property {string} [label]
29
+ * @property {boolean} [labelVisible]
30
+ * @property {string | null} [hint]
31
+ * @property {string | null} [error]
32
+ * @property {boolean | null} [disabled]
33
+ * @property {boolean | null} [readonly]
34
+ * @property {{ fn: Function, props?: any }[]} [actions] Actions run on the input
35
+ */
36
+
37
+ /** @type {TextInputProps & Record<string, any>} */
7
38
  let {
8
39
  value = $bindable(''),
9
40
  ref = $bindable(null),
@@ -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
- // Populates icon data if the icon is in cache
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
- if (!iconData) onMount(async _ => (iconData = await loadIcon(icon)))
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}`
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splendidlabz/svelte",
3
3
  "type": "module",
4
- "version": "4.15.2",
4
+ "version": "4.16.0",
5
5
  "license": "Elastic-2.0",
6
6
  "sideEffects": false,
7
7
  "homepage": "https://splendidlabz.com/docs/svelte",
@@ -34,8 +34,7 @@
34
34
  "dependencies": {
35
35
  "@floating-ui/dom": "^1.7.6",
36
36
  "@iconify/svelte": "^5.2.1",
37
- "@splendidlabz/utils": "*",
38
- "autosuggest-highlight": "^3.3.4"
37
+ "@splendidlabz/utils": "*"
39
38
  },
40
39
  "devDependencies": {
41
40
  "@splendidlabz/eslint-config": "2.2.0",