@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.
@@ -1,393 +0,0 @@
1
- import { debounce, omitEmpty } from '@splendidlabz/utils'
2
- import {
3
- addListeners,
4
- getSiblingElements,
5
- isArrowKey,
6
- isShiftTab,
7
- isTab,
8
- randomString,
9
- removeListeners,
10
- } from '@splendidlabz/utils/dom'
11
- import {
12
- close as closeDisclosure,
13
- open as openDisclosure,
14
- } from '../../DisclosureOld/disclosure.js'
15
- import {
16
- getMatches,
17
- getPropertyMatches,
18
- hasPerfectMatch,
19
- hasPerfectPropertyMatch,
20
- } from './autocomplete-matching.js'
21
- import { initCombobox, updateCombobox } from './combobox-store.js'
22
-
23
- import { getDisclosure } from '../../DisclosureOld/disclosure-store.js'
24
-
25
- // Shifted focusTriggerOnClose to disclosureContent
26
- export const DEFAULT_AUTOCOMPLETE_INPUT_OPTS = {
27
- disclosure: {
28
- showOnClick: false,
29
- focusTriggerOnClose: false,
30
- shiftTabBackToContent: false,
31
- },
32
- input: {
33
- loop: true,
34
- filterByProp: null, // 'dot.notation.string'
35
- onMatch: null,
36
- onSearch: null,
37
- searchDebounce: 250,
38
- cacheSearchResults: true,
39
- softResetDelay: 1000,
40
- },
41
- }
42
-
43
- export const AUTOCOMPLETE_INPUT_OPT_KEYS = [
44
- ...Object.keys(DEFAULT_AUTOCOMPLETE_INPUT_OPTS.input),
45
- 'showOnClick',
46
- ]
47
-
48
- export const DEFAULT_AUTOCOMPLETE_CONTENT_OPTS = {
49
- disclosure: {
50
- useAutoWidth: true,
51
- focusTarget: null,
52
- },
53
- floating: {
54
- placement: 'bottom-start',
55
- offset: 0,
56
- },
57
- options: {
58
- onSelect: null,
59
- },
60
- }
61
-
62
- export const AUTOCOMPLETE_CONTENT_KEYS = Object.keys(
63
- DEFAULT_AUTOCOMPLETE_CONTENT_OPTS.options,
64
- )
65
-
66
- export function autocompleteInput(node, props = {}) {
67
- const listboxId = node.getAttribute('aria-owns')
68
- const disclosureId = listboxId.split('-')[0]
69
-
70
- const comboboxInfo = {
71
- id: listboxId,
72
- state: {
73
- matches: [],
74
- haystack: [],
75
- searchState: 'idle',
76
- searchCache: new Map(),
77
- ...omitEmpty(props.state),
78
- },
79
- options: props.options,
80
- nodes: { input: node },
81
- stores: { disclosure: getDisclosure(disclosureId) },
82
- }
83
-
84
- const Box = initCombobox(comboboxInfo)
85
-
86
- // This will be where we set most aria attributes.
87
- // But right now there's only one ComboboxInput, so we added them directly in ComboboxInput.svelte.
88
- // If we need to use this action for other components in the future, we can shift those aria attributes here.
89
-
90
- const listeners = [
91
- {
92
- event: 'click',
93
- node,
94
- handler: handleClick,
95
- },
96
- {
97
- event: 'input',
98
- node,
99
- handler: handleInput,
100
- },
101
- {
102
- event: 'input',
103
- node,
104
- handler: debounce(handleSearch, 1000),
105
- },
106
- {
107
- event: 'keydown',
108
- node,
109
- handler: handleKeydown,
110
- },
111
- ]
112
-
113
- addListeners(listeners)
114
-
115
- function handleClick(event) {
116
- const { showOnClick } = Box.options
117
- if (showOnClick) resolveDisclosureState(Box)
118
- }
119
-
120
- function handleInput(event) {
121
- const text = event.target.value.trim()
122
- const { searchCache } = Box.state
123
- const { onSearch } = Box.options
124
-
125
- if (text === '') {
126
- Box.state.searchState = 'idle'
127
- return updateMatches(Box, [])
128
- }
129
-
130
- // Basic, standard, haystack matching.
131
- if (!onSearch) checkForMatches(Box, text)
132
- else {
133
- // Draw from cache if user already searched the terms before.
134
- if (searchCache.has(text)) {
135
- // This replaces haystack right now.
136
- // Questioning whether we even need to add to haystack.
137
- // If need, then build in the future. Just need to crete a new variable to store all the search results.
138
- const results = searchCache.get(text)
139
- updateMatches(Box, results)
140
- } else {
141
- // Show loading indicator here later because the search function is debounced
142
- Box.state.searchState = 'searching'
143
- updateCombobox(Box)
144
- resolveDisclosureState(Box)
145
- }
146
- }
147
- }
148
-
149
- async function handleSearch(event) {
150
- const text = event.target.value.trim()
151
- const { searchCache } = Box.state
152
- const { onSearch, softResetDelay } = Box.options
153
- if (!onSearch) return
154
-
155
- // Handled in handleInput
156
- if (searchCache.has(text)) return
157
-
158
- // Runs onSearch and caches the results
159
- const results = await onSearch(text)
160
- const cache = searchCache
161
- cache.set(text, results)
162
- Box.state.searchState = 'complete'
163
- Box.state.searchCache = cache
164
- updateMatches(Box, results)
165
- setTimeout(_ => softReset(Box), softResetDelay)
166
- }
167
-
168
- function handleKeydown(event) {
169
- const { key } = event
170
-
171
- if (isArrowKey(key)) {
172
- if (key === 'ArrowUp') highlightPrevOption(Box)
173
- if (key === 'ArrowDown') highlightNextOption(Box)
174
- }
175
-
176
- if (key === 'Enter') {
177
- event.preventDefault()
178
- selectCurrentOption(Box)
179
- }
180
-
181
- if (isTab(event)) selectCurrentOption(Box)
182
- if (isShiftTab(event)) closeDisclosure(Box.stores.disclosure)
183
- }
184
-
185
- return {
186
- destroy() {
187
- removeListeners(listeners)
188
- },
189
- }
190
- }
191
-
192
- // Most of the listbox stuff here can use:listbox.
193
- // I wanna refactor this to use the listbox action. Just wonder how to combine the custom match and select thingy through another action.
194
- // And on select, have to update match, probably through a custom select handler.
195
- export function autocompleteOptions(node, props = {}) {
196
- const id = node.getAttribute('id')
197
- const disclosureId = id.split('-')[0]
198
-
199
- const comboboxInfo = {
200
- id,
201
- state: omitEmpty(props.state),
202
- options: props.options,
203
- nodes: { options: node },
204
- stores: { disclosure: getDisclosure(disclosureId) },
205
- }
206
-
207
- const Box = initCombobox(comboboxInfo)
208
-
209
- // Initialize each option with appropriate attributes
210
- // Custom options require Svelte so there's no need to check for Astro slot nonsense
211
- const options = getOptions(Box)
212
- for (const option of options) {
213
- if (!option.id) option.id = randomString(8)
214
- option.setAttribute('role', 'option')
215
- // Need to set aria selected to false here?
216
- }
217
-
218
- const listeners = [
219
- {
220
- event: 'click',
221
- node,
222
- handler: handleClick,
223
- },
224
- {
225
- event: 'pointerover',
226
- node,
227
- handler: handlePointerOver,
228
- },
229
- ]
230
-
231
- addListeners(listeners)
232
-
233
- function handleClick(event) {
234
- const { target } = event
235
- if (target.getAttribute('role') !== 'option') return
236
- selectOption(Box, target)
237
- }
238
-
239
- function handlePointerOver(event) {
240
- const { target } = event
241
- if (target.getAttribute('role') === 'option') highlightOption(Box, target)
242
- }
243
-
244
- return {
245
- destroy() {
246
- removeListeners(listeners)
247
- },
248
- }
249
- }
250
-
251
- // Matching methods
252
- // ------------------------
253
- function checkForMatches(Box, text) {
254
- let matches = []
255
- text = text.trim()
256
-
257
- const { filterByProp } = Box.options
258
- const matchType = filterByProp ? 'object' : 'string'
259
-
260
- const { haystack } = Box.state
261
- if (!haystack.length) return
262
-
263
- // Handle string matching
264
- if (matchType === 'string') {
265
- const perfectMatch = hasPerfectMatch(Box.state.haystack, text)
266
- if (!perfectMatch) matches = getMatches(Box.state.haystack, text)
267
- }
268
-
269
- // Handle object prop matching
270
- if (matchType === 'object') {
271
- const perfectMatch = hasPerfectPropertyMatch({
272
- items: Box.state.haystack,
273
- prop: Box.options.filterByProp,
274
- query: text,
275
- })
276
-
277
- if (!perfectMatch)
278
- matches = getPropertyMatches({
279
- items: Box.state.haystack,
280
- prop: Box.options.filterByProp,
281
- query: text,
282
- })
283
- }
284
- updateMatches(Box, matches)
285
- }
286
-
287
- function updateMatches(Box, matches) {
288
- Box.state.matches = matches
289
- updateCombobox(Box)
290
-
291
- // Custom matches handler
292
- if (Box.options.onMatch) Box.options.onMatch(matches)
293
- resolveDisclosureState(Box)
294
- }
295
-
296
- // Show or hide disclosure when appropriate
297
- function resolveDisclosureState(Box) {
298
- const { matches, searchState } = Box.state
299
- const { disclosure } = Box.stores
300
-
301
- if (matches.length) return openDisclosure(disclosure)
302
- if (searchState === 'searching' || searchState === 'complete')
303
- return openDisclosure(disclosure)
304
-
305
- closeDisclosure(disclosure)
306
- }
307
-
308
- // Resets to initial state while keeping any caches and haystack changes
309
- function softReset(Box) {
310
- if (Box.state.searchState !== 'complete') return
311
- Box.state.searchState = 'idle'
312
- updateCombobox(Box)
313
- resolveDisclosureState(Box)
314
- }
315
-
316
- // Highlighting methods
317
- // ------------------------
318
- function getOptions(Box) {
319
- return Array.from(Box.nodes.options.children)
320
- }
321
-
322
- function highlightOption(Box, option) {
323
- const optionId = option.getAttribute('id')
324
- const siblings = getSiblingElements(option)
325
-
326
- option.setAttribute('aria-selected', true)
327
- for (const sibling of siblings) {
328
- sibling.setAttribute('aria-selected', false)
329
- }
330
-
331
- Box.state.highlightedOptionId = optionId
332
- updateCombobox(Box)
333
- }
334
-
335
- function getHighlightedOptionIndex(Box) {
336
- const options = getOptions(Box)
337
- return options.findIndex(
338
- option => option.id === Box.state.highlightedOptionId,
339
- )
340
- }
341
-
342
- function highlightFirstOption(Box) {
343
- const options = getOptions(Box)
344
- const firstOption = options[0]
345
- highlightOption(Box, firstOption)
346
- }
347
-
348
- function highlightLastOption(Box) {
349
- const options = getOptions(Box)
350
- const lastOption = options[options.length - 1]
351
- highlightOption(Box, lastOption)
352
- }
353
-
354
- function highlightNextOption(Box) {
355
- const options = getOptions(Box)
356
- const index = getHighlightedOptionIndex(Box)
357
-
358
- const nextOption = options[index + 1]
359
- if (nextOption) highlightOption(Box, nextOption)
360
- else if (Box.options.loop) highlightFirstOption(Box)
361
- }
362
-
363
- function highlightPrevOption(Box) {
364
- const options = getOptions(Box)
365
- const index = getHighlightedOptionIndex(Box)
366
-
367
- const prevOption = options[index - 1]
368
- if (prevOption) highlightOption(Box, prevOption)
369
- else if (Box.options.loop) highlightLastOption(Box)
370
- }
371
-
372
- // Selecting methods
373
- // ------------------------
374
- function selectOption(Box, option) {
375
- const text = option.textContent.trim()
376
- const { onSelect } = Box.options
377
-
378
- // Custom select handler
379
- if (onSelect) onSelect(option)
380
- else {
381
- // Fill input with option text if standard select handler
382
- Box.nodes.input.value = text
383
- }
384
-
385
- updateMatches(Box, [])
386
- }
387
-
388
- function selectCurrentOption(Box) {
389
- const options = getOptions(Box)
390
- const index = getHighlightedOptionIndex(Box)
391
- const option = options[index]
392
- selectOption(Box, option)
393
- }
@@ -1,65 +0,0 @@
1
- import { get, writable } from 'svelte/store'
2
-
3
- import { omitEmpty } from '@splendidlabz/utils'
4
-
5
- export const comboboxes = writable([])
6
- const { update } = comboboxes
7
-
8
- export function getComboboxes() {
9
- return get(comboboxes)
10
- }
11
-
12
- export function getCombobox(id) {
13
- return get(comboboxes).find(combobox => combobox.id === id)
14
- }
15
-
16
- export function addCombobox({
17
- id,
18
- state = {},
19
- nodes = {},
20
- options = {},
21
- stores = [],
22
- }) {
23
- update(boxes => {
24
- const box = { id, state, options, nodes, stores }
25
- return [...boxes, box]
26
- })
27
- }
28
-
29
- export function updateCombobox({
30
- id,
31
- state = {},
32
- options = {},
33
- nodes = {},
34
- stores = [],
35
- }) {
36
- update(boxes => {
37
- const index = boxes.findIndex(d => d.id === id)
38
- const box = boxes[index]
39
-
40
- box.state = { ...box.state, ...omitEmpty(state) }
41
- box.options = { ...box.options, ...omitEmpty(options) }
42
- box.nodes = { ...box.nodes, ...omitEmpty(nodes, { shallow: true }) }
43
- box.stores = { ...box.stores, ...omitEmpty(stores, { shallow: true }) }
44
-
45
- return boxes.with(index, box)
46
- })
47
- }
48
-
49
- export function initCombobox({
50
- id,
51
- options = {},
52
- state = {},
53
- nodes = {},
54
- stores = {},
55
- }) {
56
- const d = getCombobox(id)
57
-
58
- if (!d) {
59
- addCombobox({ id, options, state, nodes, stores })
60
- } else {
61
- updateCombobox({ id, options, state, nodes, stores })
62
- }
63
-
64
- return getCombobox(id)
65
- }