@splendidlabz/svelte 4.12.0 → 4.13.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 (48) hide show
  1. package/components/SVG/SVG.svelte +18 -11
  2. package/components/SVG/parser.js +4 -4
  3. package/package.json +11 -8
  4. package/CHANGELOG.md +0 -636
  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,432 +0,0 @@
1
- import {
2
- Focusable,
3
- addListeners,
4
- isTab,
5
- manageFocus,
6
- removeListeners,
7
- trapFocus,
8
- } from '@splendidlabz/utils/dom'
9
- import { delay, omitEmpty } from '@splendidlabz/utils'
10
-
11
- // ========================================
12
- // Default Options
13
- // ========================================
14
- /**
15
- * @typedef {Object} DisclosureTriggerOpts
16
- * @property {boolean} showOnHover - Show disclosure content on hover.
17
- * @property {boolean} showOnFocus - Show disclosure content on focus.
18
- * @property {number} hoverCloseDelay - Delay in milliseconds before closing disclosure content on mouse leave.
19
- * @property {boolean} closeOthers - Close other disclosures when opening this disclosure.
20
- */
21
- export const DEFAULT_DISCLOSURE_TRIGGER_OPTS = {
22
- showOnClick: true,
23
- showOnHover: false,
24
- showOnFocus: false,
25
- hoverCloseDelay: 120,
26
- closeOthers: true,
27
- // shiftTabBackToContent: true,
28
- }
29
-
30
- export const DISCLOSURE_TRIGGER_OPT_KEYS = Object.keys(
31
- DEFAULT_DISCLOSURE_TRIGGER_OPTS,
32
- )
33
-
34
- /**
35
- * @typedef {Object} DisclosureContentOpts
36
- * @property {boolean} closeOnOutsideClick - Close disclosure content when clicking outside of contentNode and triggerNode.
37
- * @property {boolean} closeOnEscapeButton - Close disclosure content when pressing the escape key.
38
- * @property {boolean} preventBodyScroll - Prevent body from scrolling when disclosure content is open.
39
- * @property {boolean} trapFocus - Trap focus within disclosure content.
40
- * @property {(string|HTMLElement)} focusTargetWhenOpening - Focus target when disclosure content is opened. Can be 'first', 'content', or an HTMLElement.
41
- */
42
- export const DEFAULT_DISCLOSURE_CONTENT_OPTS = {
43
- closeOnOutsideClick: true,
44
- closeOnEscapeButton: true,
45
- focusManagement: 'manage', // false, `manage`, `trap`
46
- focusWhenOpen: 'content', // || false, 'first', 'content'. TODO: HTMLElement. (No need, HTMLElement. Just use autofocus and set focusWhenOpen to false)
47
- focusWhenClose: 'trigger', // false
48
- usePortal: true,
49
- useModal: false,
50
- useTriggerWidth: false,
51
- inertOthers: false,
52
- // preventBodyScroll: false,
53
- }
54
-
55
- export const DISCLOSURE_CONTENT_OPT_KEYS = Object.keys(
56
- DEFAULT_DISCLOSURE_CONTENT_OPTS,
57
- )
58
-
59
- // ========================================
60
- // Shared state
61
- // ========================================
62
- const disclosures = $state([])
63
-
64
- export function getDisclosure(id) {
65
- return disclosures.find(d => d.id === id)
66
- }
67
-
68
- export function initDisclosure(part, disclosureInfo) {
69
- const { id } = disclosureInfo
70
- let d = getDisclosure(id)
71
- if (!d) {
72
- disclosures.push(disclosureInfo)
73
- return getDisclosure(id)
74
- }
75
-
76
- // If trigger or content is already initialized, don't re-init
77
- if (part === 'trigger' && d.nodes.trigger) return d
78
- if (part === 'content' && d.nodes.content) return d
79
-
80
- // Initialize new info from trigger or content
81
- d.state = { ...d.state, ...omitEmpty(disclosureInfo.state) }
82
- d.options = { ...d.options, ...omitEmpty(disclosureInfo.options) }
83
- d.nodes = {
84
- ...d.nodes,
85
- ...omitEmpty(disclosureInfo.nodes, { shallow: true }),
86
- }
87
-
88
- return d
89
- }
90
-
91
- // ========================================
92
- // Actions
93
- // ========================================
94
- /**
95
- * Disclosure Trigger
96
- * @prop {HTMLElement} node - The trigger element.
97
- * @prop {DisclosureTriggerOpts} props - The trigger options.
98
- */
99
- export function disclosureTrigger(node, props = {}) {
100
- const id = node.getAttribute('target')
101
- const disclosureInfo = {
102
- id,
103
- state: { show: false, ...omitEmpty(props.state) },
104
- nodes: { trigger: node },
105
- options: {
106
- ...DEFAULT_DISCLOSURE_TRIGGER_OPTS,
107
- ...props.options,
108
- },
109
- }
110
-
111
- let d = initDisclosure('trigger', disclosureInfo)
112
- node.setAttribute('aria-expanded', d.state.show)
113
- node.setAttribute('aria-haspopup', d.options['aria-haspopup'])
114
-
115
- const listeners = [
116
- {
117
- event: 'click',
118
- node,
119
- handler: handleClick,
120
- },
121
- {
122
- event: 'keydown',
123
- node,
124
- handler: closeOnEscapeButton,
125
- },
126
- {
127
- event: 'keydown',
128
- node,
129
- handler: tabOntoContent,
130
- },
131
- {
132
- event: 'pointerenter',
133
- node,
134
- handler: handlePointerEnter,
135
- },
136
- {
137
- event: 'pointerleave',
138
- node,
139
- handler: handlePointerLeave,
140
- },
141
- {
142
- event: 'focusin',
143
- node,
144
- handler: handleFocusIn,
145
- },
146
- {
147
- event: 'focusout',
148
- node,
149
- handler: handleFocusOut,
150
- },
151
- ]
152
-
153
- function handleClick(event) {
154
- if (d.options.showOnClick) {
155
- if (d.state.show === false) return open(d)
156
- if (d.state.show === true) return close(d)
157
- }
158
- }
159
-
160
- function closeOnEscapeButton(event) {
161
- if (d.options.closeOnEscapeButton) {
162
- if (event.key === 'Escape') close(d)
163
- }
164
- }
165
-
166
- function tabOntoContent(event) {
167
- if (d.state.show === false) return
168
- if (isTab(event)) {
169
- event.preventDefault()
170
- d.nodes.contentFocusables.first?.focus()
171
- }
172
- }
173
-
174
- // function backToDisclosureContent(event) {
175
- // if (d.options.shiftTabBackToContent === false) return
176
- // if (isShiftTab(event) && d.state.show === true) {
177
- // setTimeout(_ => {
178
- // d.nodes.content.focus({ preventScroll: true })
179
- // }, 0)
180
- // }
181
- // }
182
-
183
- function handlePointerEnter(event) {
184
- if (d.options.showOnHover) {
185
- d.state.pointerLocation = 'trigger'
186
- open(d)
187
- }
188
- }
189
-
190
- async function handlePointerLeave(event) {
191
- if (d.options.showOnHover) {
192
- d.state.pointerLocation = null
193
-
194
- await delay(d.options.hoverCloseDelay)
195
- if (d.state.pointerLocation === null) {
196
- d.state.isMouseOut = true
197
- close(d)
198
- d.state.isMouseOut = false
199
- }
200
- }
201
- }
202
-
203
- function handleFocusIn(event) {
204
- if (d.options.showOnFocus) open(d)
205
- }
206
-
207
- function handleFocusOut() {
208
- if (d.options.showOnFocus) close(d)
209
- }
210
-
211
- addListeners(listeners)
212
-
213
- return {
214
- destroy() {
215
- removeListeners(listeners)
216
- },
217
- }
218
- }
219
-
220
- /**
221
- * Disclosure Content action.
222
- * Disclosure content will only activate when d.state.show is updated to true
223
- * @prop {HTMLElement} node - The trigger element.
224
- * @prop {DisclosureContentOpts} props - The trigger options.
225
- */
226
- export function disclosureContent(node, props = {}) {
227
- const id = node.getAttribute('id')
228
- const contentFocusables = Focusable(node)
229
-
230
- const disclosureInfo = {
231
- id,
232
- state: omitEmpty(props.state),
233
- nodes: { content: node, contentFocusables },
234
- options: {
235
- ...DEFAULT_DISCLOSURE_CONTENT_OPTS,
236
- ...omitEmpty(props.options),
237
- },
238
- }
239
-
240
- // Initialize the disclosure
241
- let d = initDisclosure('content', disclosureInfo)
242
- contentOpeningActions(d)
243
-
244
- // Add event listeners
245
- const listeners = [
246
- {
247
- event: 'click',
248
- node: document,
249
- handler: handleOutsideClick,
250
- },
251
-
252
- {
253
- event: 'keydown',
254
- node,
255
- handler: handleKeydown,
256
- },
257
- {
258
- event: 'mouseenter',
259
- node,
260
- handler: handleMouseEnter,
261
- },
262
- {
263
- event: 'mouseleave',
264
- node,
265
- handler: handleMouseLeave,
266
- },
267
- ]
268
-
269
- addListeners(listeners)
270
-
271
- return {
272
- destroy() {
273
- removeListeners(listeners)
274
- },
275
- }
276
-
277
- // ========================
278
- // Event Listeners
279
- // ========================
280
- // Close if click is outside of contentNode and triggerNode
281
- function handleOutsideClick(event) {
282
- if (d.options.closeOnOutsideClick === false) return
283
- if (d.nodes.content.contains(event.target)) return
284
- if (d.nodes.trigger.contains(event.target)) return
285
- // Only close if state is currently set to show
286
- if (d.state.show) {
287
- if (d.options.closeOthers) close(d)
288
- else if (!clickedOnOtherDisclosureNodes(event.target, d.id)) close(d)
289
- }
290
- }
291
-
292
- function handleKeydown(event) {
293
- const { focusManagement } = d.options
294
-
295
- if (focusManagement === 'trap') {
296
- trapFocus({ event, focusables: d.nodes.contentFocusables })
297
- }
298
-
299
- if (focusManagement === 'manage') {
300
- manageFocus({
301
- event,
302
- triggerNode: d.nodes.trigger,
303
- contentNode: d.nodes.content,
304
- focusables: d.nodes.contentFocusables,
305
- shouldFocusTriggerWhenTabbingOut: d.options.focusTriggerWhenTabbingOut,
306
- onLeave(direction) {
307
- if (direction === 'forward') close(d)
308
- },
309
- })
310
- }
311
-
312
- if (d.options.closeOnEscapeButton) closeOnEscape(event)
313
- }
314
-
315
- function closeOnEscape(event) {
316
- if (event.key === 'Escape') close(d, { shouldFocusTrigger: true })
317
- }
318
-
319
- function handleMouseEnter() {
320
- if (!d.options.showOnHover) return
321
- d.state.pointerLocation = 'content'
322
- }
323
-
324
- function handleMouseLeave() {
325
- if (!d.options.showOnHover) return
326
- d.state.pointerLocation = null
327
- close(d)
328
- }
329
- }
330
-
331
- // ========================================
332
- // Methods
333
- // ========================================
334
- export function open(d) {
335
- if (d.state.show) return
336
- d.state.show = true
337
-
338
- const { trigger } = d.nodes
339
- trigger.setAttribute('aria-expanded', d.state.show)
340
- if (d.options.closeOthers) closeOthersExcept(d.id)
341
-
342
- // Note: The rest of the opening actions are handled openingActions after we retrieve disclosureContent props
343
- }
344
-
345
- function contentOpeningActions(d) {
346
- const { content: node, contentFocusables } = d.nodes
347
- const { focusWhenOpen, usePortal, useModal, useTriggerWidth } = d.options
348
- node.setAttribute('tabindex', '-1')
349
-
350
- if (useTriggerWidth) {
351
- const { width: triggerWidth } = d.nodes.trigger.getBoundingClientRect()
352
- node.style.width = triggerWidth + 'px'
353
- }
354
-
355
- // Re-introduce useModal, stopBodyScroll, allowBodyScroll, inertOthers, etc later when working on modal or elements that causes scroll
356
- // if (useModal) {
357
- // inertOthers(d)
358
- // stopBodyScroll(d)
359
- // }
360
-
361
- // Set focus.
362
- // This requires timeout. Otherwise focus is set too early.
363
- setTimeout(_ => {
364
- if (focusWhenOpen) {
365
- if (focusWhenOpen === 'content') node.focus()
366
- if (focusWhenOpen === 'first') contentFocusables.first?.focus()
367
- if (focusWhenOpen === 'second') contentFocusables[1]?.focus()
368
- }
369
- // Dispatch an open event when all is done
370
- d.nodes.content.dispatchEvent(new CustomEvent('open'))
371
- }, 0)
372
- }
373
-
374
- export function close(d, { shouldFocusWhenClosing = true } = {}) {
375
- if (!d.state.show) return
376
- d.state.show = false
377
-
378
- const { trigger, content } = d.nodes
379
- const { isMouseOut } = d.state
380
- const { focusWhenClose } = d.options
381
- trigger.setAttribute('aria-expanded', d.state.show)
382
-
383
- // Handle Inert and bodyscroll later
384
- // revertInert(d)
385
- // if (d.options.preventBodyScroll) revertPreventBodyScroll()
386
-
387
- // Decide whether to focus on trigger
388
- if (focusWhenClose === false) shouldFocusWhenClosing = false
389
- // Mouseout can cause focus to jump around
390
- if (isMouseOut) shouldFocusWhenClosing = false
391
-
392
- // Focuses on trigger
393
- if (shouldFocusWhenClosing) trigger.focus()
394
-
395
- // Dispatch close event when all is done
396
- // Checks content to ensure content node is present before dispatching event.
397
- if (content) content.dispatchEvent(new CustomEvent('close'))
398
- }
399
-
400
- function closeOthersExcept(id) {
401
- disclosures.forEach(d => {
402
- if (d.id !== id) close(d, { shouldFocusWhenClosing: false })
403
- })
404
- }
405
-
406
- function clickedOnOtherDisclosureNodes(target, id) {
407
- const disclosures = getDisclosures()
408
- return disclosures.some(d => {
409
- if (d.id === id) return false
410
- if (d.nodes.trigger?.contains(target)) return true
411
- if (d.nodes.content?.contains(target)) return true
412
- return false
413
- })
414
- }
415
-
416
- function makeOthersInert(d) {
417
- const bodyChildren = getChildrenElements(document.body)
418
- // Disclosures are often placed inside Portal divs — not directly on the body element — so we need to account for portals.
419
- const elementsToInert = bodyChildren.filter(
420
- child => !isAncestor(child, d.nodes.content),
421
- )
422
- elementsToInert.forEach(el => el.setAttribute('inert', ''))
423
- d.state.inertElements = elementsToInert
424
- // Apparently, no need to updateDisclosure before reverting Inert later.
425
- // (Probably cos close() updates disclosure first).
426
- }
427
-
428
- function revertInert(d) {
429
- d.state.inertElements?.forEach(el => {
430
- if (el.removeAttribute) el.removeAttribute('inert', '')
431
- })
432
- }
@@ -1,48 +0,0 @@
1
- // @vitest-environment node
2
- import { beforeAll, describe, expect, test } from 'vitest'
3
- import { loadSvelteComponent, renderComponent } from '../../test-utils.js'
4
-
5
- let Markdown
6
-
7
- beforeAll(async () => {
8
- Markdown = await loadSvelteComponent(new URL('./Markdown.svelte', import.meta.url))
9
- })
10
-
11
- describe('Markdown', () => {
12
- describe('block (default)', () => {
13
- test('wraps content in <p>', () => {
14
- const result = renderComponent(Markdown, { content: 'hello' })
15
- expect(result).toBe('<p>hello</p>')
16
- })
17
-
18
- test('renders bold', () => {
19
- const result = renderComponent(Markdown, { content: '**bold**' })
20
- expect(result).toBe('<p><strong>bold</strong></p>')
21
- })
22
- })
23
-
24
- describe('inline', () => {
25
- test('does not produce <p> tags', () => {
26
- const result = renderComponent(Markdown, { content: 'hello', inline: true })
27
- expect(result).not.toContain('<p>')
28
- expect(result).not.toContain('</p>')
29
- })
30
-
31
- test('renders bold without <p>', () => {
32
- const result = renderComponent(Markdown, { content: '**bold**', inline: true })
33
- expect(result).toBe('<strong>bold</strong>')
34
- })
35
- })
36
-
37
- describe('edge cases', () => {
38
- test('returns empty for empty content', () => {
39
- const result = renderComponent(Markdown, { content: '' })
40
- expect(result).toBe('')
41
- })
42
-
43
- test('returns empty for undefined content', () => {
44
- const result = renderComponent(Markdown, {})
45
- expect(result).toBe('')
46
- })
47
- })
48
- })
@@ -1,43 +0,0 @@
1
- import { readFile } from 'node:fs/promises'
2
- import { beforeAll, describe, expect, test } from 'vitest'
3
- import { fileURLToPath } from 'node:url'
4
- import path from 'node:path'
5
-
6
- // Resolve the CSS file relative to this test
7
- const cssPath = path.resolve(
8
- fileURLToPath(import.meta.url),
9
- '../../../../styles/src/components/svelte/tabs.css'
10
- )
11
-
12
- describe('tabs.css --placement custom property', () => {
13
- let css
14
-
15
- beforeAll(async () => {
16
- css = await readFile(cssPath, 'utf8')
17
- })
18
-
19
- test('tablist-top sets --placement: top', () => {
20
- // Match the @utility tablist-top block (may contain nested braces)
21
- const match = css.match(/@utility tablist-top\s*\{[\s\S]*?\n\}/)
22
- expect(match).toBeTruthy()
23
- expect(match[0]).toContain('--placement: top')
24
- })
25
-
26
- test('tablist-left sets --placement: left', () => {
27
- const match = css.match(/@utility tablist-left\s*\{[\s\S]*?\n\}/)
28
- expect(match).toBeTruthy()
29
- expect(match[0]).toContain('--placement: left')
30
- })
31
-
32
- test('tablist-right sets --placement: right', () => {
33
- const match = css.match(/@utility tablist-right\s*\{[\s\S]*?\n\}/)
34
- expect(match).toBeTruthy()
35
- expect(match[0]).toContain('--placement: right')
36
- })
37
-
38
- test('all three placements are covered', () => {
39
- expect(css).toContain('--placement: top')
40
- expect(css).toContain('--placement: left')
41
- expect(css).toContain('--placement: right')
42
- })
43
- })