@splendidlabz/svelte 4.11.0 → 4.12.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.
Files changed (48) hide show
  1. package/components/SVG/Noise.svelte +57 -0
  2. package/components/SVG/index.js +2 -1
  3. package/package.json +11 -8
  4. package/CHANGELOG.md +0 -624
  5. package/common.js +0 -21
  6. package/components/Accordion/accordion-controlled.spec.js +0 -14
  7. package/components/DisclosureOld/Drawer/Drawer.svelte +0 -31
  8. package/components/DisclosureOld/Drawer/DrawerCloseButton.svelte +0 -37
  9. package/components/DisclosureOld/Drawer/DrawerContent.svelte +0 -105
  10. package/components/DisclosureOld/Drawer/DrawerTrigger.svelte +0 -11
  11. package/components/DisclosureOld/Drawer/drawer.js +0 -18
  12. package/components/DisclosureOld/Drawer/index.js +0 -6
  13. package/components/DisclosureOld/Dropdown/DropdownContent.svelte +0 -15
  14. package/components/DisclosureOld/Dropdown/DropdownTrigger.svelte +0 -14
  15. package/components/DisclosureOld/Dropdown/dropdown.js +0 -4
  16. package/components/DisclosureOld/Dropdown/index.js +0 -4
  17. package/components/DisclosureOld/Modal/Modal.svelte +0 -30
  18. package/components/DisclosureOld/Modal/ModalCloseButton.svelte +0 -7
  19. package/components/DisclosureOld/Modal/ModalContent.svelte +0 -50
  20. package/components/DisclosureOld/Modal/ModalTrigger.svelte +0 -11
  21. package/components/DisclosureOld/Modal/exit-intent.js +0 -17
  22. package/components/DisclosureOld/Modal/index.js +0 -5
  23. package/components/DisclosureOld/Modal/modal.js +0 -4
  24. package/components/DisclosureOld/Old/DisclosureContentOld.svelte +0 -109
  25. package/components/DisclosureOld/Old/DisclosureTriggerOld.svelte +0 -62
  26. package/components/DisclosureOld/Popover/PopoverCloseButton.svelte +0 -32
  27. package/components/DisclosureOld/Popover/PopoverContent.svelte +0 -107
  28. package/components/DisclosureOld/Popover/PopoverTrigger.svelte +0 -33
  29. package/components/DisclosureOld/Popover/floating.js +0 -171
  30. package/components/DisclosureOld/Popover/index.js +0 -5
  31. package/components/DisclosureOld/Popover/popover.js +0 -18
  32. package/components/DisclosureOld/Popover/references/FollowTheMouse.svelte +0 -45
  33. package/components/DisclosureOld/Tooltip/Tooltip.svelte +0 -14
  34. package/components/DisclosureOld/Tooltip/TooltipContent.svelte +0 -7
  35. package/components/DisclosureOld/Tooltip/TooltipTrigger.svelte +0 -33
  36. package/components/DisclosureOld/Tooltip/index.js +0 -5
  37. package/components/DisclosureOld/Tooltip/tooltip.js +0 -22
  38. package/components/DisclosureOld/disclosure-store.js +0 -48
  39. package/components/DisclosureOld/disclosure.js +0 -443
  40. package/components/DisclosureOld/disclosure.svelte.js +0 -432
  41. package/components/Markdown/Markdown.spec.js +0 -48
  42. package/components/Tabs/tabs-css.spec.js +0 -43
  43. package/components/Tabs/tabs.spec.js +0 -553
  44. package/eslint.config.js +0 -2
  45. package/jsconfig.json +0 -16
  46. package/svelte.config.js +0 -13
  47. package/test-utils.js +0 -44
  48. package/vitest.config.js +0 -11
@@ -1,443 +0,0 @@
1
- import { delay, omitEmpty } from '@splendidlabz/utils'
2
- import {
3
- Focusable,
4
- addListeners,
5
- getChildrenElements,
6
- isAncestor,
7
- isTab,
8
- manageFocus,
9
- removeListeners,
10
- trapFocus,
11
- } from '@splendidlabz/utils/dom'
12
- import {
13
- getDisclosure as getD,
14
- getDisclosures,
15
- initDisclosure,
16
- updateDisclosure,
17
- } from './disclosure-store.js'
18
-
19
- /**
20
- * @typedef {Object} DisclosureTriggerOpts
21
- * @property {boolean} showOnHover - Show disclosure content on hover.
22
- * @property {boolean} showOnFocus - Show disclosure content on focus.
23
- * @property {number} hoverCloseDelay - Delay in milliseconds before closing disclosure content on mouse leave.
24
- * @property {boolean} closeOthers - Close other disclosures when opening this disclosure.
25
- */
26
- export const DEFAULT_DISCLOSURE_TRIGGER_OPTS = {
27
- showOnClick: true,
28
- showOnHover: false,
29
- showOnFocus: false,
30
- hoverCloseDelay: 120,
31
- closeOthers: true,
32
- // shiftTabBackToContent: true,
33
- }
34
-
35
- export const DISCLOSURE_TRIGGER_OPT_KEYS = Object.keys(
36
- DEFAULT_DISCLOSURE_TRIGGER_OPTS,
37
- )
38
-
39
- /**
40
- * @typedef {Object} DisclosureContentOpts
41
- * @property {boolean} closeOnOutsideClick - Close disclosure content when clicking outside of contentNode and triggerNode.
42
- * @property {boolean} closeOnEscapeButton - Close disclosure content when pressing the escape key.
43
- * @property {boolean} preventBodyScroll - Prevent body from scrolling when disclosure content is open.
44
- * @property {boolean} trapFocus - Trap focus within disclosure content.
45
- * @property {(string|HTMLElement)} focusTargetWhenOpening - Focus target when disclosure content is opened. Can be 'first', 'content', or an HTMLElement.
46
- */
47
- export const DEFAULT_DISCLOSURE_CONTENT_OPTS = {
48
- lightDismiss: true,
49
- focusManagement: 'manage', // false, `manage`, `trap`
50
- focusTargetWhenOpening: 'content', // || false, 'first', 'content'.
51
- focusTriggerOnClose: true,
52
- inertOthers: false,
53
- preventBodyScroll: false,
54
- useAutoWidth: false,
55
- onclick: null,
56
- onkeydown: null,
57
- ontoggle: null,
58
- onbeforetoggle: null,
59
- onopen: null,
60
- onbeforeopen: null,
61
- onclose: null,
62
- onbeforeclose: null,
63
- }
64
-
65
- export const DISCLOSURE_CONTENT_OPT_KEYS = Object.keys(
66
- DEFAULT_DISCLOSURE_CONTENT_OPTS,
67
- )
68
-
69
- // Potentially switch to the Popover API in the future if the change warrants it.
70
- /**
71
- * Disclosure Trigger
72
- * @prop {HTMLElement} node - The trigger element.
73
- * @prop {DisclosureTriggerOpts} props - The trigger options.
74
- */
75
- export function disclosureTrigger(node, props = {}) {
76
- console.log('dt running')
77
- const targetId = node.getAttribute('target')
78
-
79
- const disclosureInfo = {
80
- id: targetId,
81
- state: { show: false, ...omitEmpty(props.state) },
82
- options: {
83
- ...DEFAULT_DISCLOSURE_TRIGGER_OPTS,
84
- ...props.options,
85
- },
86
- nodes: { trigger: node },
87
- }
88
-
89
- const d = initDisclosure(disclosureInfo)
90
- node.setAttribute('aria-expanded', d.state.show)
91
- node.setAttribute('data-show', d.state.show)
92
-
93
- const listeners = [
94
- {
95
- event: 'click',
96
- node,
97
- handler: handleClick,
98
- },
99
- {
100
- event: 'keydown',
101
- node,
102
- handler: closeOnEscapeButton,
103
- },
104
- {
105
- event: 'keydown',
106
- node,
107
- handler: tabOntoContent,
108
- },
109
- {
110
- event: 'pointerenter',
111
- node,
112
- handler: handlePointerEnter,
113
- },
114
- {
115
- event: 'pointerleave',
116
- node,
117
- handler: handlePointerLeave,
118
- },
119
- {
120
- event: 'focusin',
121
- node,
122
- handler: handleFocusIn,
123
- },
124
- {
125
- event: 'focusout',
126
- node,
127
- handler: handleFocusOut,
128
- },
129
- ]
130
-
131
- function handleClick(event) {
132
- if (d.options.showOnClick) {
133
- const { show } = d.state
134
- if (show === false) return open(d)
135
- if (show === true) return close(d)
136
- }
137
- }
138
-
139
- function closeOnEscapeButton(event) {
140
- const { lightDismiss } = d.options
141
- if (event.key !== 'Escape') return
142
- if (lightDismiss) return close(d)
143
- }
144
-
145
- function tabOntoContent(event) {
146
- if (d.state.show === false) return
147
- if (isTab(event)) {
148
- event.preventDefault()
149
- d.nodes.contentFocusables.first?.focus()
150
- }
151
- }
152
-
153
- function handlePointerEnter(event) {
154
- if (d.options.showOnHover) {
155
- d.state.pointerLocation = 'trigger'
156
- open(d)
157
- }
158
- }
159
-
160
- async function handlePointerLeave(event) {
161
- if (d.options.showOnHover) {
162
- d.state.pointerLocation = null
163
-
164
- await delay(d.options.hoverCloseDelay)
165
- if (d.state.pointerLocation === null) {
166
- d.state.isMouseOut = true
167
- close(d)
168
- d.state.isMouseOut = false
169
- }
170
- }
171
- }
172
-
173
- function handleFocusIn(event) {
174
- if (d.options.showOnFocus) open(d)
175
- }
176
-
177
- function handleFocusOut() {
178
- if (d.options.showOnFocus) close(d)
179
- }
180
-
181
- addListeners(listeners)
182
-
183
- return {
184
- destroy() {
185
- removeListeners(listeners)
186
- },
187
- }
188
- }
189
-
190
- /**
191
- * Disclosure Content action.
192
- * Disclosure content will only activate when d.state.show is updated to true
193
- * @prop {HTMLElement} node - The trigger element.
194
- * @prop {DisclosureContentOpts} props - The trigger options.
195
- */
196
- export function disclosureContent(node, props = {}) {
197
- console.log('dc running')
198
-
199
- const disclosureContentID = node.getAttribute('id')
200
- const contentFocusables = Focusable(node)
201
-
202
- const disclosureInfo = {
203
- id: disclosureContentID,
204
- state: omitEmpty(props.state),
205
- options: {
206
- ...DEFAULT_DISCLOSURE_CONTENT_OPTS,
207
- ...omitEmpty(props.options),
208
- },
209
- nodes: { content: node, contentFocusables },
210
- }
211
-
212
- if (disclosureInfo.options.inertOthers) {
213
- disclosureInfo.options.preventBodyScroll = true
214
- }
215
-
216
- const d = initDisclosure(disclosureInfo)
217
-
218
- // Opening actions
219
- node.setAttribute('tabindex', '-1')
220
-
221
- const { focusTargetWhenOpening, useAutoWidth, onopen, ontoggle } = d.options
222
-
223
- if (d.options.inertOthers) makeOthersInert(d)
224
- if (d.options.preventBodyScroll) preventBodyScroll()
225
-
226
- if (useAutoWidth) {
227
- const { width: triggerWidth } = d.nodes.trigger.getBoundingClientRect()
228
- node.style.width = triggerWidth + 'px'
229
- }
230
-
231
- // Setting focus requires timeout. Otherwise, it won't work.
232
- setTimeout(_ => {
233
- if (focusTargetWhenOpening) {
234
- const target = focusTargetWhenOpening
235
- if (target === 'content') node.focus()
236
- if (target === 'first') contentFocusables.first?.focus()
237
- if (target === 'second') contentFocusables[1]?.focus()
238
- if (target instanceof HTMLElement) target.focus()
239
- }
240
-
241
- // Run opening functions
242
- if (typeof onopen === 'function') onopen(d)
243
- if (typeof ontoggle === 'function') ontoggle(d)
244
- }, 0)
245
-
246
- // Add event listeners
247
- const listeners = [
248
- {
249
- event: 'click',
250
- node: document,
251
- handler: handleOutsideClick,
252
- },
253
- {
254
- event: 'keydown',
255
- node,
256
- handler: handleKeydown,
257
- },
258
- {
259
- event: 'mouseenter',
260
- node,
261
- handler: handleMouseEnter,
262
- },
263
- {
264
- event: 'mouseleave',
265
- node,
266
- handler: handleMouseLeave,
267
- },
268
- ]
269
-
270
- addListeners(listeners)
271
-
272
- return {
273
- destroy() {
274
- removeListeners(listeners)
275
- },
276
- }
277
-
278
- // ========================
279
- // Event Listeners
280
- // ========================
281
- // Close if click is outside of contentNode and triggerNode
282
- function handleOutsideClick(event) {
283
- if (d.options.lightDismiss === false) return
284
- if (d.nodes.content.contains(event.target)) return
285
- if (d.nodes.trigger.contains(event.target)) return
286
- // Only close if state is currently set to show
287
- if (d.state.show) {
288
- if (d.options.closeOthers) close(d)
289
- else if (!clickedOnOtherDisclosureNodes(event.target, d.id)) close(d)
290
- }
291
- }
292
-
293
- // function closeAllOnEscape(event) {
294
- // if (d.options.closeOnEscapeButton && event.key === 'Escape') {
295
- // const disclosures = getDisclosures()
296
- // disclosures.forEach(d => close(d, { shouldFocusTrigger: true }))
297
- // }
298
- // }
299
-
300
- function handleKeydown(event) {
301
- const { focusManagement } = d.options
302
-
303
- if (focusManagement === 'trap') {
304
- trapFocus({ event, focusables: d.nodes.contentFocusables })
305
- }
306
-
307
- if (focusManagement === 'manage') {
308
- manageFocus({
309
- event,
310
- triggerNode: d.nodes.trigger,
311
- contentNode: d.nodes.content,
312
- focusables: d.nodes.contentFocusables,
313
- shouldFocusTriggerWhenTabbingOut: d.options.focusTriggerWhenTabbingOut,
314
- onLeave(direction) {
315
- if (direction === 'forward') close(d)
316
- },
317
- })
318
- }
319
-
320
- if (d.options.lightDismiss) closeOnEscape(event)
321
- }
322
-
323
- function closeOnEscape(event) {
324
- if (event.key === 'Escape') close(d, { shouldFocusTrigger: true })
325
- }
326
-
327
- function handleMouseEnter() {
328
- if (!d.options.showOnHover) return
329
- d.state.pointerLocation = 'content'
330
- }
331
-
332
- function handleMouseLeave() {
333
- if (!d.options.showOnHover) return
334
- d.state.pointerLocation = null
335
- close(d)
336
- }
337
- }
338
-
339
- // ========================
340
- // Methods
341
- // ========================
342
- export function open(d) {
343
- const { onbeforeopen, onbeforetoggle } = d.options
344
- if (typeof onbeforeopen === 'function') onbeforeopen(d)
345
- if (typeof onbeforetoggle === 'function') onbeforetoggle(d)
346
- if (d.state.show === true) return
347
- d.state.show = true
348
- const { trigger } = d.nodes
349
- trigger.setAttribute('aria-expanded', d.state.show)
350
- trigger.setAttribute('data-show', d.state.show)
351
-
352
- if (d.options.closeOthers) closeOthersExcept(d.id)
353
-
354
- updateDisclosure(d)
355
-
356
- // This run disclosureContent because d.state.show will be updated into stores
357
- // All other opening activites are conducted within `disclosureContent` because we have to wait for content node to be ready. So @see disclosureContent for details.
358
- }
359
-
360
- export function close(d, { shouldFocusTrigger = false } = {}) {
361
- const { onbeforeclose, onbeforetoggle, onclose, ontoggle } = d.options
362
- if (typeof onbeforeclose === 'function') onbeforeclose(d)
363
- if (typeof onbeforetoggle === 'function') onbeforetoggle(d)
364
- if (d.state.show === false) return
365
- d.state.show = false
366
-
367
- const { trigger } = d.nodes
368
- trigger.setAttribute('aria-expanded', d.state.show)
369
- trigger.setAttribute('data-show', d.state.show)
370
- revertInert(d)
371
-
372
- updateDisclosure(d)
373
-
374
- // Dispatch a close event
375
- // If check because we can close disclosure content so quickly in some implementations it hasn't been opened yet. Example: Combobox.
376
- if (typeof onclose === 'function') onclose(d)
377
- if (typeof ontoggle === 'function') ontoggle(d)
378
- // if (content) content.dispatchEvent(new CustomEvent('close'))
379
-
380
- // If it's mouse out, we no need to change focus back to trigger. Only bring focus back to trigger when using keyboard events, and only when necessary
381
-
382
- if (
383
- !d.state.isMouseOut &&
384
- d.options.focusTriggerOnClose &&
385
- shouldFocusTrigger
386
- ) {
387
- trigger.focus()
388
- }
389
- if (d.options.preventBodyScroll) revertPreventBodyScroll()
390
- }
391
-
392
- // For manual closing. Need to get disclosure before we can use `close`.
393
- export function getDisclosure(id) {
394
- return getD(id)
395
- }
396
-
397
- function closeOthersExcept(id) {
398
- const disclosures = getDisclosures()
399
- disclosures.forEach(d => {
400
- if (d.id !== id) close(d)
401
- })
402
- }
403
-
404
- function clickedOnOtherDisclosureNodes(target, id) {
405
- const disclosures = getDisclosures()
406
- return disclosures.some(d => {
407
- if (d.id === id) return false
408
- if (d.nodes.trigger?.contains(target)) return true
409
- if (d.nodes.content?.contains(target)) return true
410
- return false
411
- })
412
- }
413
-
414
- function preventBodyScroll() {
415
- const top = document.documentElement.scrollTop
416
- document.body.style.top = -1 * top + 'px'
417
- document.body.style.overflow = 'hidden'
418
- }
419
-
420
- function revertPreventBodyScroll() {
421
- const top = parseFloat(document.body.style.top) * -1
422
- document.body.style.top = null
423
- document.body.style.overflow = null
424
- document.documentElement.scrollTop = top
425
- }
426
-
427
- function makeOthersInert(d) {
428
- const bodyChildren = getChildrenElements(document.body)
429
- // Disclosures are often placed inside Portal divs — not directly on the body element — so we need to account for portals.
430
- const elementsToInert = bodyChildren.filter(
431
- child => !isAncestor(child, d.nodes.content),
432
- )
433
- elementsToInert.forEach(el => el.setAttribute('inert', ''))
434
- d.state.inertElements = elementsToInert
435
- // Apparently, no need to updateDisclosure before reverting Inert later.
436
- // (Probably cos close() updates disclosure first).
437
- }
438
-
439
- function revertInert(d) {
440
- d.state.inertElements?.forEach(el => {
441
- if (el.removeAttribute) el.removeAttribute('inert', '')
442
- })
443
- }