@splendidlabz/svelte 0.4.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.
Files changed (153) hide show
  1. package/.eslintrc.cjs +3 -0
  2. package/CHANGELOG.md +91 -0
  3. package/actions/breakpoint-observer.js +71 -0
  4. package/actions/index.js +6 -0
  5. package/actions/portal.js +20 -0
  6. package/actions/resize-observer.js +43 -0
  7. package/actions/scroll-observer.js +47 -0
  8. package/common.js +21 -0
  9. package/components/Accordion/Accordion.svelte +108 -0
  10. package/components/Accordion/index.js +3 -0
  11. package/components/Breadcrumbs/Breadcrumbs.svelte +69 -0
  12. package/components/Breadcrumbs/Crumb.svelte +37 -0
  13. package/components/Breadcrumbs/CrumbCustom.svelte +14 -0
  14. package/components/Breadcrumbs/index.js +5 -0
  15. package/components/BrowserFrame.svelte +37 -0
  16. package/components/BrowserWithTabs.svelte +28 -0
  17. package/components/CQChecker/CQChecker.astro +20 -0
  18. package/components/CQChecker/CQChecker.svelte +89 -0
  19. package/components/Callout/Callout.svelte +17 -0
  20. package/components/Callout/index.js +3 -0
  21. package/components/Codepen/Codepen.astro +29 -0
  22. package/components/Codepen/Codepen.svelte +24 -0
  23. package/components/Codepen/index.js +3 -0
  24. package/components/ContentList/ContentList-old.svelte +117 -0
  25. package/components/ContentList/ContentList.svelte +68 -0
  26. package/components/ContentList/index.js +3 -0
  27. package/components/Disclosure/Drawer/Drawer.svelte +35 -0
  28. package/components/Disclosure/Drawer/DrawerCloseButton.svelte +31 -0
  29. package/components/Disclosure/Drawer/DrawerContent-wrecked.svelte +198 -0
  30. package/components/Disclosure/Drawer/DrawerContent.svelte +164 -0
  31. package/components/Disclosure/Drawer/DrawerTrigger.svelte +32 -0
  32. package/components/Disclosure/Drawer/drawer.js +112 -0
  33. package/components/Disclosure/Drawer/index.js +6 -0
  34. package/components/Disclosure/Dropdown/DropdownContent.svelte +15 -0
  35. package/components/Disclosure/Dropdown/DropdownTrigger.svelte +14 -0
  36. package/components/Disclosure/Dropdown/dropdown.js +4 -0
  37. package/components/Disclosure/Dropdown/index.js +4 -0
  38. package/components/Disclosure/Modal/ModalContent.svelte +3 -0
  39. package/components/Disclosure/Modal/ModalTrigger.svelte +33 -0
  40. package/components/Disclosure/Modal/exit-intent.js +17 -0
  41. package/components/Disclosure/Modal/modal.js +13 -0
  42. package/components/Disclosure/Popover/PopoverCloseButton.svelte +29 -0
  43. package/components/Disclosure/Popover/PopoverContent.svelte +107 -0
  44. package/components/Disclosure/Popover/PopoverTrigger.svelte +33 -0
  45. package/components/Disclosure/Popover/floating.js +171 -0
  46. package/components/Disclosure/Popover/index.js +5 -0
  47. package/components/Disclosure/Popover/popover.js +18 -0
  48. package/components/Disclosure/Popover/references/FollowTheMouse.svelte +45 -0
  49. package/components/Disclosure/Tooltip/Tooltip.svelte +14 -0
  50. package/components/Disclosure/Tooltip/TooltipContent.svelte +7 -0
  51. package/components/Disclosure/Tooltip/TooltipTrigger.svelte +33 -0
  52. package/components/Disclosure/Tooltip/index.js +5 -0
  53. package/components/Disclosure/Tooltip/tooltip.js +22 -0
  54. package/components/Disclosure/disclosure-store.js +48 -0
  55. package/components/Disclosure/disclosure.js +439 -0
  56. package/components/Disclosure/index.js +3 -0
  57. package/components/Disclosure/state.svelte.js +138 -0
  58. package/components/FancyList/FancyList.svelte +49 -0
  59. package/components/FancyList/index.js +3 -0
  60. package/components/Form/Button.svelte +24 -0
  61. package/components/Form/Checkbox.svelte +80 -0
  62. package/components/Form/Checkboxes.svelte +48 -0
  63. package/components/Form/Combobox/AutocompleteInput.svelte +63 -0
  64. package/components/Form/Combobox/AutocompleteOptions.svelte +74 -0
  65. package/components/Form/Combobox/TimepickerInput.svelte +81 -0
  66. package/components/Form/Combobox/TimepickerOptions.svelte +100 -0
  67. package/components/Form/Combobox/autocomplete-matching.js +114 -0
  68. package/components/Form/Combobox/autocomplete.js +393 -0
  69. package/components/Form/Combobox/combobox-store.js +65 -0
  70. package/components/Form/Combobox/combobox.js +359 -0
  71. package/components/Form/Combobox/index.js +14 -0
  72. package/components/Form/Combobox/timepicker.js +119 -0
  73. package/components/Form/Form.svelte +62 -0
  74. package/components/Form/Hidden.svelte +16 -0
  75. package/components/Form/Radio.svelte +69 -0
  76. package/components/Form/Radios.svelte +50 -0
  77. package/components/Form/Select.svelte +127 -0
  78. package/components/Form/Switch.svelte +50 -0
  79. package/components/Form/TextInput.svelte +87 -0
  80. package/components/Form/Textarea.svelte +103 -0
  81. package/components/Form/check.svg +3 -0
  82. package/components/Form/index.js +12 -0
  83. package/components/Highlight/Highlight.svelte +27 -0
  84. package/components/Highlight/_Highlight.scss +19 -0
  85. package/components/Highlight/index.js +2 -0
  86. package/components/Image.svelte +24 -0
  87. package/components/InteractiveRegion/InteractiveRegion.svelte +47 -0
  88. package/components/InteractiveRegion/index.js +3 -0
  89. package/components/List/Lia.svelte +12 -0
  90. package/components/List/NestedListItem.svelte +50 -0
  91. package/components/List/index.js +3 -0
  92. package/components/Markdown/Markdown.svelte +35 -0
  93. package/components/Masonry/Masonry.svelte +79 -0
  94. package/components/Masonry/index.js +3 -0
  95. package/components/PerspectiveHover/PerspectiveHover.svelte +10 -0
  96. package/components/PerspectiveHover/perspective-hover.js +46 -0
  97. package/components/Populate/ConditionalPopulate.svelte +33 -0
  98. package/components/Populate/Populate.svelte +45 -0
  99. package/components/Populate/actions.js +29 -0
  100. package/components/Populate/get-value.js +17 -0
  101. package/components/Populate/index.js +4 -0
  102. package/components/Portal.svelte +27 -0
  103. package/components/PushToLocalStorage.svelte +30 -0
  104. package/components/Resizer/Resizer.svelte +32 -0
  105. package/components/Resizer/draggable.js +23 -0
  106. package/components/Resizer/index.js +3 -0
  107. package/components/Resizer/resizer.js +133 -0
  108. package/components/SVG/SVG.svelte +120 -0
  109. package/components/Scrambler/Scrambler.svelte +27 -0
  110. package/components/Scrambler/index.js +3 -0
  111. package/components/Spotlight/PointerPosition.js +32 -0
  112. package/components/Spotlight/Spotlight.svelte +9 -0
  113. package/components/Status.svelte +27 -0
  114. package/components/Switcheroo/Switcheroo.svelte +71 -0
  115. package/components/Switcheroo/index.js +3 -0
  116. package/components/Table/RowGroup.svelte +8 -0
  117. package/components/Table/Table.svelte +23 -0
  118. package/components/Table/Td.svelte +20 -0
  119. package/components/Table/Th.svelte +27 -0
  120. package/components/Table/Thead.svelte +8 -0
  121. package/components/Table/Tr.svelte +8 -0
  122. package/components/Table/index.js +17 -0
  123. package/components/Table/table.js +35 -0
  124. package/components/Tabs/Tab.svelte +12 -0
  125. package/components/Tabs/Tablist.svelte +62 -0
  126. package/components/Tabs/Tabpanel.svelte +10 -0
  127. package/components/Tabs/Tabpanels.svelte +12 -0
  128. package/components/Tabs/index.js +6 -0
  129. package/components/Tabs/tab-store.js +60 -0
  130. package/components/Tabs/tabs.js +364 -0
  131. package/components/Tabs-Old/Tab.svelte +9 -0
  132. package/components/Tabs-Old/Tablist.svelte +13 -0
  133. package/components/Tabs-Old/Tabpanel.svelte +10 -0
  134. package/components/Tabs-Old/Tabpanels.svelte +14 -0
  135. package/components/Tabs-Old/Tabs.svelte +83 -0
  136. package/components/Tabs-Old/index.js +7 -0
  137. package/components/Tabs-Old/tabs.js +306 -0
  138. package/components/Textwall/Repeat.svelte +10 -0
  139. package/components/Textwall/Textwall.svelte +30 -0
  140. package/components/Textwall/index.js +4 -0
  141. package/components/Typewriter/Typewriter.svelte +70 -0
  142. package/components/Typewriter/index.js +3 -0
  143. package/components/Video.svelte +46 -0
  144. package/components/Vimeo.svelte +18 -0
  145. package/components/Wrapper.svelte +22 -0
  146. package/components/Youtube.svelte +15 -0
  147. package/components/_shared/accessibility.js +9 -0
  148. package/components/_shared/key.js +1 -0
  149. package/components/_shared/transitions.js +48 -0
  150. package/components/_shared/use-actions.js +31 -0
  151. package/index.js +35 -0
  152. package/jsconfig.json +5 -0
  153. package/package.json +39 -0
@@ -0,0 +1,359 @@
1
+ import { 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 { getDisclosure } from '../../Disclosure/disclosure-store.js'
12
+ import {
13
+ close as closeDisclosure,
14
+ open as openDisclosure,
15
+ } from '../../Disclosure/disclosure.js'
16
+ import { initCombobox, updateCombobox } from './combobox-store.js'
17
+
18
+ // Shifted focusTriggerOnClose to disclosureContent
19
+ export const DEFAULT_COMBOBOX_INPUT_OPTIONS = {
20
+ disclosure: {
21
+ showOnClick: false,
22
+ focusTriggerOnClose: false,
23
+ shiftTabBackToContent: false,
24
+ },
25
+ input: {
26
+ showDisclosureOnClick: true,
27
+ loop: true,
28
+ },
29
+ }
30
+
31
+ export const COMBOBOX_INPUT_OPTION_KEYS = Object.keys(
32
+ DEFAULT_COMBOBOX_INPUT_OPTIONS.input,
33
+ )
34
+
35
+ export const DEFAULT_LISTBOX_OPTIONS = {
36
+ disclosure: {
37
+ focusTarget: null,
38
+ },
39
+ floating: {
40
+ placement: 'bottom-start',
41
+ offset: '0.75rem',
42
+ },
43
+ options: {
44
+ onSelect: null,
45
+ preserveSelectedIndex: true,
46
+ },
47
+ }
48
+
49
+ export const LISTBOX_OPTION_KEYS = Object.keys(DEFAULT_LISTBOX_OPTIONS.options)
50
+
51
+ // Allows tampering of submitted value to the database.
52
+ export function comboboxAssociatedInput(node, props = {}) {
53
+ const listboxId = node.getAttribute('listbox-id')
54
+
55
+ const comboboxInfo = {
56
+ id: listboxId,
57
+ nodes: { toSubmit: node },
58
+ }
59
+
60
+ initCombobox(comboboxInfo)
61
+ }
62
+
63
+ /**
64
+ * Initializes Combobox Input
65
+ * @param {HTMLElement} node - The DOM node to which the combobox input behavior will be attached.
66
+ * @param {Object} [props={}] - An optional object containing properties to configure the combobox input.
67
+ * @returns {Object} An object with a `destroy` method to clean up the combobox input behavior.
68
+ */
69
+ export function comboboxInput(node, props = {}) {
70
+ const listboxId = node.getAttribute('listbox-id')
71
+ const disclosureId = listboxId.split('-')[0]
72
+
73
+ const comboboxInfo = {
74
+ id: listboxId,
75
+ state: {
76
+ ...omitEmpty(props.state),
77
+ selectedOptionId: null,
78
+ },
79
+ options: props.options,
80
+ nodes: { input: node },
81
+ stores: { disclosure: getDisclosure(disclosureId) },
82
+ }
83
+
84
+ const Box = initCombobox(comboboxInfo)
85
+
86
+ node.setAttribute('role', 'combobox')
87
+ node.setAttribute('aria-controls', listboxId)
88
+ node.setAttribute('aria-owns', listboxId)
89
+ // aria-expanded is handled by disclosure.
90
+
91
+ // I can probably set activedescendant here directly too... Shall explore later.
92
+ // 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...
93
+ // node.setAttribute('aria-haspopup', 'listbox')
94
+
95
+ const listeners = [
96
+ {
97
+ event: 'click',
98
+ node,
99
+ handler: handleClick,
100
+ },
101
+ {
102
+ event: 'keydown',
103
+ node,
104
+ handler: handleKeydown,
105
+ },
106
+ ]
107
+
108
+ addListeners(listeners)
109
+
110
+ function handleClick(event) {
111
+ const { showDisclosureOnClick } = Box.options
112
+ if (showDisclosureOnClick) openDisclosure(Box.stores.disclosure)
113
+ }
114
+
115
+ function handleKeydown(event) {
116
+ const { key } = event
117
+
118
+ if (isArrowKey(key)) {
119
+ if (key === 'ArrowUp') highlightPrevOption(Box)
120
+ if (key === 'ArrowDown') highlightNextOption(Box)
121
+ }
122
+
123
+ if (key === 'Enter') {
124
+ event.preventDefault()
125
+ selectCurrentOption(Box)
126
+ }
127
+
128
+ if (isTab(event)) selectCurrentOption(Box)
129
+ if (isShiftTab(event)) closeDisclosure(Box.stores.disclosure)
130
+ }
131
+
132
+ return {
133
+ destroy() {
134
+ removeListeners(listeners)
135
+ },
136
+ }
137
+ }
138
+
139
+ export function listboxTrigger() {}
140
+
141
+ /**
142
+ * Initializes a listbox component on the given DOM node with the specified properties.
143
+ * @param {HTMLElement} node - The DOM node to which the listbox behavior will be attached.
144
+ * @param {Object} [props={}] - An optional object containing properties to configure the listbox.
145
+ * @param {boolean} [props.multiple=false] - If true, allows multiple selections; otherwise, only one item can be selected.
146
+ * @param {Function} [props.onSelect] - A callback function that is called when an item is selected.
147
+ */
148
+ export function listbox(node, props = {}) {
149
+ const id = node.getAttribute('id')
150
+ const disclosureId = id.split('-')[0]
151
+
152
+ const comboboxInfo = {
153
+ id,
154
+ state: omitEmpty(props.state),
155
+ options: omitEmpty(props.options),
156
+ nodes: { listbox: node },
157
+ stores: { disclosure: getDisclosure(disclosureId) },
158
+ }
159
+
160
+ const Box = initCombobox(comboboxInfo)
161
+
162
+ // Initialize with appropriate aria attributes for listbox node and options
163
+ node.setAttribute('role', 'listbox')
164
+ node.setAttribute('tabindex', '0')
165
+
166
+ const options = getOptions(Box)
167
+ for (const option of options) {
168
+ if (!option.id) option.id = randomString(8)
169
+ option.setAttribute('role', 'option')
170
+ option.setAttribute('aria-selected', 'false')
171
+ }
172
+
173
+ // Highlighted selected option (if any)
174
+ // We need to use selectedIndex because id is regenerated on each render.
175
+ // This cannot be changed unless Svelte key works with Astro slots (they don't at 2024/07/25).
176
+ setTimeout(_ => {
177
+ console.log(Box)
178
+ const { selectedOptionIndex } = Box.state
179
+ console.log(selectedOptionIndex)
180
+ const option = getOptions(Box)[selectedOptionIndex]
181
+ console.log(option)
182
+ if (option) highlightOption(Box, option)
183
+ }, 0)
184
+
185
+ const listeners = [
186
+ {
187
+ event: 'click',
188
+ node,
189
+ handler: handleClick,
190
+ },
191
+ {
192
+ event: 'pointerover',
193
+ node,
194
+ handler: handlePointerOver,
195
+ },
196
+ {
197
+ event: 'pointerout',
198
+ node,
199
+ handler: handlePointerOut,
200
+ },
201
+ {
202
+ event: 'keydown',
203
+ node,
204
+ handler: handleKeydown,
205
+ },
206
+ ]
207
+
208
+ addListeners(listeners)
209
+
210
+ function handleClick(event) {
211
+ const { target } = event
212
+ if (target.getAttribute('role') !== 'option') return
213
+ selectOption(Box, target)
214
+ }
215
+
216
+ function handlePointerOver(event) {
217
+ const { target } = event
218
+
219
+ // Do not disturb pointer interactions with automatic scrolling.
220
+ if (target.getAttribute('role') === 'option')
221
+ highlightOption(Box, target, { scroll: false })
222
+ }
223
+
224
+ function handlePointerOut(event) {
225
+ removeAllHighlightedOptions(Box)
226
+ }
227
+
228
+ return {
229
+ destroy() {
230
+ removeListeners(listeners)
231
+ },
232
+ }
233
+
234
+ // But this should NOT be triggered if there it is in popover + Combobox mode... have to check
235
+ // This will only activate if the listbox is focused.
236
+ function handleKeydown(event) {
237
+ const { key } = event
238
+
239
+ if (isArrowKey(key)) {
240
+ if (key === 'ArrowUp') highlightPrevOption(Box)
241
+ if (key === 'ArrowDown') highlightNextOption(Box)
242
+ }
243
+
244
+ if (key === 'Enter') {
245
+ event.preventDefault()
246
+ selectCurrentOption(Box)
247
+ }
248
+
249
+ if (isTab(event)) selectCurrentOption(Box)
250
+ if (isShiftTab(event)) closeDisclosure(Box.stores.disclosure)
251
+ }
252
+ }
253
+
254
+ // Highlighting methods
255
+ // ------------------------
256
+ function getOptions(Box) {
257
+ return Array.from(Box.nodes.listbox.children)
258
+ }
259
+
260
+ function highlightOption(Box, option, { scroll = true } = {}) {
261
+ const optionId = option.getAttribute('id')
262
+ const siblings = getSiblingElements(option)
263
+
264
+ option.setAttribute('aria-selected', true)
265
+ for (const sibling of siblings) {
266
+ sibling.setAttribute('aria-selected', false)
267
+ }
268
+
269
+ // Adjust scroll so highlighted option remains visible
270
+ if (scroll) option.scrollIntoView({ block: 'nearest', behavior: 'instant' })
271
+
272
+ Box.state.highlightedOptionId = optionId
273
+
274
+ updateCombobox(Box)
275
+ }
276
+
277
+ function getHighlightedOptionIndex(Box) {
278
+ const options = getOptions(Box)
279
+ return options.findIndex(
280
+ option => option.id === Box.state.highlightedOptionId,
281
+ )
282
+ }
283
+
284
+ function removeAllHighlightedOptions(Box) {
285
+ const options = getOptions(Box)
286
+ for (const option of options) {
287
+ option.setAttribute('aria-selected', false)
288
+ }
289
+
290
+ Box.state.highlightedOptionId = null
291
+ updateCombobox(Box)
292
+ }
293
+
294
+ function highlightFirstOption(Box) {
295
+ const options = getOptions(Box)
296
+ const firstOption = options[0]
297
+ highlightOption(Box, firstOption)
298
+ }
299
+
300
+ function highlightLastOption(Box) {
301
+ const options = getOptions(Box)
302
+ const lastOption = options[options.length - 1]
303
+ highlightOption(Box, lastOption)
304
+ }
305
+
306
+ function highlightNextOption(Box) {
307
+ const options = getOptions(Box)
308
+ const index = getHighlightedOptionIndex(Box)
309
+
310
+ const nextOption = options[index + 1]
311
+ if (nextOption) highlightOption(Box, nextOption)
312
+ else if (Box.options.loop) highlightFirstOption(Box)
313
+ }
314
+
315
+ function highlightPrevOption(Box) {
316
+ const options = getOptions(Box)
317
+ const index = getHighlightedOptionIndex(Box)
318
+
319
+ const prevOption = options[index - 1]
320
+ if (prevOption) highlightOption(Box, prevOption)
321
+ else if (Box.options.loop) highlightLastOption(Box)
322
+ }
323
+
324
+ // Selecting methods
325
+ // ------------------------
326
+ function selectOption(Box, option) {
327
+ const { onSelect, preserveSelectedIndex } = Box.options
328
+ if (onSelect)
329
+ onSelect(option) // Custom select handler
330
+ else {
331
+ // Default select handler
332
+ // Cannot use 'value' directly value is 0 when used on non-input elements. So we use 'data-value' instead.
333
+ const text = option.textContent.trim()
334
+ const value = option.dataset.value
335
+
336
+ // Fill input with option text if standard select handler
337
+ Box.nodes.input.value = text
338
+
339
+ // Fill associated input with option value
340
+ Box.nodes.toSubmit.value = value
341
+ }
342
+
343
+ if (preserveSelectedIndex) {
344
+ const index = getHighlightedOptionIndex(Box)
345
+ Box.state.selectedOptionIndex = index
346
+ }
347
+
348
+ Box.state.highlightedOptionId = null
349
+
350
+ updateCombobox(Box)
351
+ closeDisclosure(Box.stores.disclosure)
352
+ }
353
+
354
+ function selectCurrentOption(Box) {
355
+ const options = getOptions(Box)
356
+ const index = getHighlightedOptionIndex(Box)
357
+ const option = options[index]
358
+ selectOption(Box, option)
359
+ }
@@ -0,0 +1,14 @@
1
+ import AutocompleteInput from './AutocompleteInput.svelte'
2
+ import AutocompleteOptions from './AutocompleteOptions.svelte'
3
+ import TimepickerInput from './TimepickerInput.svelte'
4
+ import TimepickerOptions from './TimepickerOptions.svelte'
5
+
6
+ // Combobox has been renamed to Autocomplete
7
+ export {
8
+ AutocompleteInput,
9
+ AutocompleteOptions,
10
+ AutocompleteInput as ComboboxInput,
11
+ AutocompleteOptions as ComboboxOptions,
12
+ TimepickerInput,
13
+ TimepickerOptions,
14
+ }
@@ -0,0 +1,119 @@
1
+ export const DEFAULT_TIMEPICKER_OPTIONS = {
2
+ mode: 'listbox', // list, listbox, detailed
3
+ selection: 'single', // single/multiple
4
+ start: '00:00',
5
+ end: '24:00',
6
+ interval: '30m', // m or h
7
+ format: 'hh:mm', // HH == 24h clock hh == 12h clock mm = minutes ss = seconds
8
+ blockedTimes: [], // Example blocked interval
9
+ showBlockedTimes: true,
10
+ }
11
+
12
+ export const DEFAULT_TIMEPICKER_DISCLOSURE_OPTS = {
13
+ showOnClick: true,
14
+ focusTriggerOnClose: false,
15
+ shiftTabBackToContent: false,
16
+ }
17
+
18
+ export function detailedTimepicker(node, options = {}) {}
19
+
20
+ export function generateTimings(options = {}) {
21
+ options = Object.assign({}, DEFAULT_TIMEPICKER_OPTIONS, options)
22
+ const { start, end, interval, format, blockedTimes, showBlockedTimes } =
23
+ options
24
+
25
+ // Initialize timings array
26
+ const timings = []
27
+ const intervalInMinutes =
28
+ parseInt(interval, 10) * (interval.includes('h') ? 60 : 1)
29
+ const blockedMinutes = blockedTimes.map(([start, end]) => {
30
+ return [timeStringToMinutes(start), timeStringToMinutes(end)]
31
+ })
32
+
33
+ for (
34
+ let minutes = timeStringToMinutes(start);
35
+ minutes <= timeStringToMinutes(end);
36
+ minutes += intervalInMinutes
37
+ ) {
38
+ const time = {
39
+ minutes,
40
+ timeString: minutesToTimeString(minutes, format),
41
+ timeString24H: minutesToTimeString(minutes, 'HH:mm'),
42
+ }
43
+ const blocked = isBlockedMinutes(minutes, blockedMinutes)
44
+ if (showBlockedTimes) timings.push({ ...time, blocked })
45
+ else {
46
+ if (!blocked) {
47
+ timings.push({ ...time, blocked: false })
48
+ }
49
+ }
50
+ }
51
+
52
+ return timings
53
+ }
54
+
55
+ // Helper function to convert time string to minutes
56
+ function timeStringToMinutes(time) {
57
+ const [hours, minutes] = time.split(':').map(Number)
58
+ return hours * 60 + minutes
59
+ }
60
+
61
+ // Helper function to convert minutes back to time string
62
+ function minutesToTimeString(minutes, format) {
63
+ let hours = Math.floor(minutes / 60)
64
+ const mins = minutes % 60
65
+ const seconds = 0 // Assuming seconds are always 0 as input is in minutes
66
+ const timestringParts = []
67
+
68
+ // Adjust for the 24:00 case to correctly represent it as 00:00
69
+ if (hours === 24) hours = 0
70
+
71
+ // Determine if AM/PM is needed
72
+ const isAmPm = format.includes('h') || format.includes('hh')
73
+ const ampm = hours >= 12 ? 'PM' : 'AM'
74
+
75
+ // Define time components based on the format
76
+ const timeComponents = {
77
+ HH: hours.toString().padStart(2, '0'),
78
+ H: hours.toString(),
79
+ hh: (hours % 12 || 12).toString().padStart(2, '0'),
80
+ h: (hours % 12 || 12).toString(),
81
+ mm: mins.toString().padStart(2, '0'),
82
+ m: mins.toString(),
83
+ ss: seconds.toString().padStart(2, '0'),
84
+ s: seconds.toString(),
85
+ }
86
+
87
+ if (format.includes('H')) {
88
+ if (format.includes('HH')) timestringParts.push(timeComponents.HH)
89
+ else timestringParts.push(timeComponents.H)
90
+ }
91
+
92
+ if (format.includes('h')) {
93
+ if (format.includes('hh')) timestringParts.push(timeComponents.hh)
94
+ else timestringParts.push(timeComponents.h)
95
+ }
96
+
97
+ if (format.includes('m')) {
98
+ if (format.includes('mm')) timestringParts.push(timeComponents.mm)
99
+ else timestringParts.push(timeComponents.m)
100
+ }
101
+
102
+ if (format.includes('s')) {
103
+ if (format.includes('ss')) timestringParts.push(timeComponents.ss)
104
+ else timestringParts.push(timeComponents.s)
105
+ }
106
+
107
+ // Join the parts with colon separator and append AM/PM if needed
108
+ let timeString = timestringParts.join(':')
109
+ if (isAmPm) timeString += ` ${ampm}`
110
+
111
+ return timeString
112
+ }
113
+
114
+ // Check if a time is within any blocked intervals
115
+ function isBlockedMinutes(minutes, blockedIntervals) {
116
+ return blockedIntervals.some(([startMinutes, endMinutes]) => {
117
+ return minutes >= startMinutes && minutes <= endMinutes
118
+ })
119
+ }
@@ -0,0 +1,62 @@
1
+ <script>
2
+ import {
3
+ flattenArrayFields,
4
+ formDataToObject,
5
+ sanitizeObject,
6
+ } from '@splendidlabz/utils'
7
+ import HiddenInput from './Hidden.svelte'
8
+
9
+ let klass = ''
10
+ export { klass as class }
11
+ export let method = 'post'
12
+ export let action = null
13
+ export let options = {}
14
+
15
+ options = {
16
+ sanitize: true,
17
+ preventSpam: true,
18
+ ...options,
19
+ }
20
+
21
+ // Only expose submit event handler.
22
+ // This is due to a limitation on Svelte 4.
23
+ // Solvable in Svelte 5 with bubbling events
24
+ // See
25
+ // https://github.com/sveltejs/svelte/issues/2837.
26
+ // https://svelte-5-preview.vercel.app/docs/event-handlers#bubbling-events
27
+ export let onSubmit = null
28
+ let spam = ''
29
+
30
+ function submit(event) {
31
+ // Submits the form without JS if no onSubmit callback is provided
32
+ const form = event.target
33
+
34
+ // Using a callback pattern here because Svelte 5 uses a callback pattern anyway.
35
+ if (!onSubmit) return form.submit()
36
+
37
+ event.preventDefault()
38
+
39
+ let data = formDataToObject(new FormData(form))
40
+ if (options.sanitize) data = sanitizeObject(data)
41
+ data = flattenArrayFields(data)
42
+
43
+ if (data.spam) return
44
+ delete data.spam
45
+
46
+ onSubmit(data)
47
+ }
48
+ </script>
49
+
50
+ <form
51
+ on:submit={submit}
52
+ on:change
53
+ class="Form {klass}"
54
+ {action}
55
+ {method}
56
+ {...$$restProps}
57
+ >
58
+ {#if options.preventSpam}
59
+ <HiddenInput name="spam" bind:value={spam} />
60
+ {/if}
61
+ <slot />
62
+ </form>
@@ -0,0 +1,16 @@
1
+ <script>
2
+ import { useActions } from '../_shared/use-actions'
3
+ export let name
4
+ export let ref = null
5
+ export let value = ''
6
+ export let actions = []
7
+ </script>
8
+
9
+ <input
10
+ type="hidden"
11
+ {name}
12
+ bind:this={ref}
13
+ bind:value
14
+ use:useActions={actions}
15
+ {...$$restProps}
16
+ />
@@ -0,0 +1,69 @@
1
+ <script>
2
+ import { toTitle } from '@splendidlabz/utils'
3
+ import { randomString } from '@splendidlabz/utils/dom'
4
+ import SVG from '../SVG/SVG.svelte'
5
+ import TextInput from './TextInput.svelte'
6
+
7
+ let klass = ''
8
+ export { klass as class }
9
+
10
+ export let labelClass = ''
11
+ export let fieldClass = ''
12
+ export let icon = null
13
+ export let iconClass = ''
14
+
15
+ export let id = null
16
+ export let name
17
+ export let label = null
18
+ export let value = null
19
+ export let checked = false
20
+ export let readonly = null
21
+ export let disabled = null
22
+ export let ref = null
23
+ export let group = null
24
+ export let hasInput = false
25
+
26
+ id = id || randomString(8)
27
+
28
+ if (!name) throw new Error('Radio requires a name')
29
+ if (!value) throw new Error('Radio requires a value')
30
+
31
+ // Create label if not provided
32
+ if (!label && label !== '') label = toTitle(value)
33
+ </script>
34
+
35
+ <div class="Radio {klass}" class:checked class:disabled class:readonly>
36
+ <div class="Radiomark">
37
+ <input
38
+ class="Field {fieldClass}"
39
+ type="radio"
40
+ {id}
41
+ {name}
42
+ {checked}
43
+ disabled={disabled || readonly}
44
+ {value}
45
+ bind:group
46
+ bind:this={ref}
47
+ on:change
48
+ {...$$restProps}
49
+ />
50
+
51
+ {#if readonly && checked}
52
+ <input type="hidden" {name} {value} {checked} />
53
+ {/if}
54
+
55
+ {#if icon}
56
+ <SVG class={iconClass} {icon} />
57
+ {:else}
58
+ <div class="radio"></div>
59
+ {/if}
60
+ </div>
61
+
62
+ {#if label}
63
+ <label class="Label {labelClass}" for={id}> {label} </label>
64
+ {/if}
65
+
66
+ {#if hasInput}
67
+ <TextInput name="{name}-input" />
68
+ {/if}
69
+ </div>
@@ -0,0 +1,50 @@
1
+ <script>
2
+ import Radio from './Radio.svelte'
3
+
4
+ let klass = ''
5
+ export { klass as class }
6
+ export let legendClass = ''
7
+ export let radiosClass = ''
8
+ export let icon = null
9
+ export let iconClass = ''
10
+
11
+ export let legend = ''
12
+ export let name // Name of the radio group
13
+ export let radios // Array of objects that contain name and label
14
+ export let checked = null
15
+ export let disabled = null
16
+ export let readonly = null
17
+ export let ref = null
18
+
19
+ const props = {
20
+ icon,
21
+ iconClass,
22
+ disabled,
23
+ readonly,
24
+ }
25
+
26
+ radios = radios.map(radio => {
27
+ if (typeof radio === 'string') radio = { value: radio }
28
+ return { ref: null, ...radio }
29
+ })
30
+
31
+ checked = radios.find(radio => radio.checked)?.value
32
+ </script>
33
+
34
+ <fieldset class="Fieldset {klass}" bind:this={ref} {...$$restProps}>
35
+ {#if legend}
36
+ <legend class="Legend {legendClass}">{legend}</legend>
37
+ {/if}
38
+
39
+ <div class="Radios {radiosClass}">
40
+ {#each radios as radio}
41
+ <Radio
42
+ {...props}
43
+ {...radio}
44
+ {name}
45
+ bind:ref={radio.ref}
46
+ bind:group={checked}
47
+ />
48
+ {/each}
49
+ </div>
50
+ </fieldset>