@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,360 +0,0 @@
1
- import {
2
- addListeners,
3
- getSiblingElements,
4
- isArrowKey,
5
- isShiftTab,
6
- isTab,
7
- randomString,
8
- removeListeners,
9
- } from '@splendidlabz/utils/dom'
10
- import {
11
- close as closeDisclosure,
12
- open as openDisclosure,
13
- } from '../../DisclosureOld/disclosure.js'
14
- import { initCombobox, updateCombobox } from './combobox-store.js'
15
-
16
- import { omitEmpty } from '@splendidlabz/utils'
17
- import { getDisclosure } from '../../DisclosureOld/disclosure-store.js'
18
-
19
- // Shifted focusTriggerOnClose to disclosureContent
20
- export const DEFAULT_COMBOBOX_INPUT_OPTIONS = {
21
- disclosure: {
22
- showOnClick: false,
23
- focusTriggerOnClose: false,
24
- shiftTabBackToContent: false,
25
- },
26
- input: {
27
- showDisclosureOnClick: true,
28
- loop: true,
29
- },
30
- }
31
-
32
- export const COMBOBOX_INPUT_OPTION_KEYS = Object.keys(
33
- DEFAULT_COMBOBOX_INPUT_OPTIONS.input,
34
- )
35
-
36
- export const DEFAULT_LISTBOX_OPTIONS = {
37
- disclosure: {
38
- focusTarget: null,
39
- },
40
- floating: {
41
- placement: 'bottom-start',
42
- offset: '0.75rem',
43
- },
44
- options: {
45
- onSelect: null,
46
- preserveSelectedIndex: true,
47
- },
48
- }
49
-
50
- export const LISTBOX_OPTION_KEYS = Object.keys(DEFAULT_LISTBOX_OPTIONS.options)
51
-
52
- // Allows tampering of submitted value to the database.
53
- export function comboboxAssociatedInput(node, props = {}) {
54
- const listboxId = node.getAttribute('listbox-id')
55
-
56
- const comboboxInfo = {
57
- id: listboxId,
58
- nodes: { toSubmit: node },
59
- }
60
-
61
- initCombobox(comboboxInfo)
62
- }
63
-
64
- /**
65
- * Initializes Combobox Input
66
- * @param {HTMLElement} node - The DOM node to which the combobox input behavior will be attached.
67
- * @param {Object} [props={}] - An optional object containing properties to configure the combobox input.
68
- * @returns {Object} An object with a `destroy` method to clean up the combobox input behavior.
69
- */
70
- export function comboboxInput(node, props = {}) {
71
- const listboxId = node.getAttribute('listbox-id')
72
- const disclosureId = listboxId.split('-')[0]
73
-
74
- const comboboxInfo = {
75
- id: listboxId,
76
- state: {
77
- ...omitEmpty(props.state),
78
- selectedOptionId: null,
79
- },
80
- options: props.options,
81
- nodes: { input: node },
82
- stores: { disclosure: getDisclosure(disclosureId) },
83
- }
84
-
85
- const Box = initCombobox(comboboxInfo)
86
-
87
- node.setAttribute('role', 'combobox')
88
- node.setAttribute('aria-controls', listboxId)
89
- node.setAttribute('aria-owns', listboxId)
90
- // aria-expanded is handled by disclosure.
91
-
92
- // I can probably set activedescendant here directly too... Shall explore later.
93
- // Aria haspopup is implied and defaults to listbox. No need to set unless there is dialog... but popover IS dialog? So have to set on Popover too...
94
- // node.setAttribute('aria-haspopup', 'listbox')
95
-
96
- const listeners = [
97
- {
98
- event: 'click',
99
- node,
100
- handler: handleClick,
101
- },
102
- {
103
- event: 'keydown',
104
- node,
105
- handler: handleKeydown,
106
- },
107
- ]
108
-
109
- addListeners(listeners)
110
-
111
- function handleClick(event) {
112
- const { showDisclosureOnClick } = Box.options
113
- if (showDisclosureOnClick) openDisclosure(Box.stores.disclosure)
114
- }
115
-
116
- function handleKeydown(event) {
117
- const { key } = event
118
-
119
- if (isArrowKey(key)) {
120
- if (key === 'ArrowUp') highlightPrevOption(Box)
121
- if (key === 'ArrowDown') highlightNextOption(Box)
122
- }
123
-
124
- if (key === 'Enter') {
125
- event.preventDefault()
126
- selectCurrentOption(Box)
127
- }
128
-
129
- if (isTab(event)) selectCurrentOption(Box)
130
- if (isShiftTab(event)) closeDisclosure(Box.stores.disclosure)
131
- }
132
-
133
- return {
134
- destroy() {
135
- removeListeners(listeners)
136
- },
137
- }
138
- }
139
-
140
- export function listboxTrigger() {}
141
-
142
- /**
143
- * Initializes a listbox component on the given DOM node with the specified properties.
144
- * @param {HTMLElement} node - The DOM node to which the listbox behavior will be attached.
145
- * @param {Object} [passedProps={}] - An optional object containing properties to configure the listbox.
146
- * @param {boolean} [passedProps.multiple=false] - If true, allows multiple selections; otherwise, only one item can be selected.
147
- * @param {Function} [passedProps.onSelect] - A callback function that is called when an item is selected.
148
- */
149
- export function listbox(node, props = {}) {
150
- const id = node.getAttribute('id')
151
- const disclosureId = id.split('-')[0]
152
-
153
- const comboboxInfo = {
154
- id,
155
- state: omitEmpty(props.state),
156
- options: omitEmpty(props.options),
157
- nodes: { listbox: node },
158
- stores: { disclosure: getDisclosure(disclosureId) },
159
- }
160
-
161
- const Box = initCombobox(comboboxInfo)
162
-
163
- // Initialize with appropriate aria attributes for listbox node and options
164
- node.setAttribute('role', 'listbox')
165
- node.setAttribute('tabindex', '0')
166
-
167
- const options = getOptions(Box)
168
- for (const option of options) {
169
- if (!option.id) option.id = randomString(8)
170
- option.setAttribute('role', 'option')
171
- option.setAttribute('aria-selected', 'false')
172
- }
173
-
174
- // Highlighted selected option (if any)
175
- // We need to use selectedIndex because id is regenerated on each render.
176
- // This cannot be changed unless Svelte key works with Astro slots (they don't at 2024/07/25).
177
- setTimeout(_ => {
178
- console.log(Box)
179
- const { selectedOptionIndex } = Box.state
180
- console.log(selectedOptionIndex)
181
- const option = getOptions(Box)[selectedOptionIndex]
182
- console.log(option)
183
- if (option) highlightOption(Box, option)
184
- }, 0)
185
-
186
- const listeners = [
187
- {
188
- event: 'click',
189
- node,
190
- handler: handleClick,
191
- },
192
- {
193
- event: 'pointerover',
194
- node,
195
- handler: handlePointerOver,
196
- },
197
- {
198
- event: 'pointerout',
199
- node,
200
- handler: handlePointerOut,
201
- },
202
- {
203
- event: 'keydown',
204
- node,
205
- handler: handleKeydown,
206
- },
207
- ]
208
-
209
- addListeners(listeners)
210
-
211
- function handleClick(event) {
212
- const { target } = event
213
- if (target.getAttribute('role') !== 'option') return
214
- selectOption(Box, target)
215
- }
216
-
217
- function handlePointerOver(event) {
218
- const { target } = event
219
-
220
- // Do not disturb pointer interactions with automatic scrolling.
221
- if (target.getAttribute('role') === 'option')
222
- highlightOption(Box, target, { scroll: false })
223
- }
224
-
225
- function handlePointerOut(event) {
226
- removeAllHighlightedOptions(Box)
227
- }
228
-
229
- return {
230
- destroy() {
231
- removeListeners(listeners)
232
- },
233
- }
234
-
235
- // But this should NOT be triggered if there it is in popover + Combobox mode... have to check
236
- // This will only activate if the listbox is focused.
237
- function handleKeydown(event) {
238
- const { key } = event
239
-
240
- if (isArrowKey(key)) {
241
- if (key === 'ArrowUp') highlightPrevOption(Box)
242
- if (key === 'ArrowDown') highlightNextOption(Box)
243
- }
244
-
245
- if (key === 'Enter') {
246
- event.preventDefault()
247
- selectCurrentOption(Box)
248
- }
249
-
250
- if (isTab(event)) selectCurrentOption(Box)
251
- if (isShiftTab(event)) closeDisclosure(Box.stores.disclosure)
252
- }
253
- }
254
-
255
- // Highlighting methods
256
- // ------------------------
257
- function getOptions(Box) {
258
- return Array.from(Box.nodes.listbox.children)
259
- }
260
-
261
- function highlightOption(Box, option, { scroll = true } = {}) {
262
- const optionId = option.getAttribute('id')
263
- const siblings = getSiblingElements(option)
264
-
265
- option.setAttribute('aria-selected', true)
266
- for (const sibling of siblings) {
267
- sibling.setAttribute('aria-selected', false)
268
- }
269
-
270
- // Adjust scroll so highlighted option remains visible
271
- if (scroll) option.scrollIntoView({ block: 'nearest', behavior: 'instant' })
272
-
273
- Box.state.highlightedOptionId = optionId
274
-
275
- updateCombobox(Box)
276
- }
277
-
278
- function getHighlightedOptionIndex(Box) {
279
- const options = getOptions(Box)
280
- return options.findIndex(
281
- option => option.id === Box.state.highlightedOptionId,
282
- )
283
- }
284
-
285
- function removeAllHighlightedOptions(Box) {
286
- const options = getOptions(Box)
287
- for (const option of options) {
288
- option.setAttribute('aria-selected', false)
289
- }
290
-
291
- Box.state.highlightedOptionId = null
292
- updateCombobox(Box)
293
- }
294
-
295
- function highlightFirstOption(Box) {
296
- const options = getOptions(Box)
297
- const firstOption = options[0]
298
- highlightOption(Box, firstOption)
299
- }
300
-
301
- function highlightLastOption(Box) {
302
- const options = getOptions(Box)
303
- const lastOption = options[options.length - 1]
304
- highlightOption(Box, lastOption)
305
- }
306
-
307
- function highlightNextOption(Box) {
308
- const options = getOptions(Box)
309
- const index = getHighlightedOptionIndex(Box)
310
-
311
- const nextOption = options[index + 1]
312
- if (nextOption) highlightOption(Box, nextOption)
313
- else if (Box.options.loop) highlightFirstOption(Box)
314
- }
315
-
316
- function highlightPrevOption(Box) {
317
- const options = getOptions(Box)
318
- const index = getHighlightedOptionIndex(Box)
319
-
320
- const prevOption = options[index - 1]
321
- if (prevOption) highlightOption(Box, prevOption)
322
- else if (Box.options.loop) highlightLastOption(Box)
323
- }
324
-
325
- // Selecting methods
326
- // ------------------------
327
- function selectOption(Box, option) {
328
- const { onSelect, preserveSelectedIndex } = Box.options
329
- if (onSelect)
330
- onSelect(option) // Custom select handler
331
- else {
332
- // Default select handler
333
- // Cannot use 'value' directly value is 0 when used on non-input elements. So we use 'data-value' instead.
334
- const text = option.textContent.trim()
335
- const value = option.dataset.value
336
-
337
- // Fill input with option text if standard select handler
338
- Box.nodes.input.value = text
339
-
340
- // Fill associated input with option value
341
- Box.nodes.toSubmit.value = value
342
- }
343
-
344
- if (preserveSelectedIndex) {
345
- const index = getHighlightedOptionIndex(Box)
346
- Box.state.selectedOptionIndex = index
347
- }
348
-
349
- Box.state.highlightedOptionId = null
350
-
351
- updateCombobox(Box)
352
- closeDisclosure(Box.stores.disclosure)
353
- }
354
-
355
- function selectCurrentOption(Box) {
356
- const options = getOptions(Box)
357
- const index = getHighlightedOptionIndex(Box)
358
- const option = options[index]
359
- selectOption(Box, option)
360
- }