@splendidlabz/svelte 4.8.3 → 4.8.5

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 (51) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/components/Accordion/Accordion.svelte +17 -21
  3. package/components/Breadcrumbs/Breadcrumbs.svelte +8 -19
  4. package/components/Breadcrumbs/Crumb.svelte +19 -14
  5. package/components/BrowserFrame/BrowserFrame.svelte +8 -16
  6. package/components/Codepen/Codepen.svelte +2 -1
  7. package/components/ContentList/ContentList.svelte +6 -7
  8. package/components/Copyright.svelte +6 -7
  9. package/components/Disclosure/Disclosure/DisclosureContent.svelte +21 -21
  10. package/components/Disclosure/Disclosure/DisclosureTrigger.svelte +23 -22
  11. package/components/Disclosure/Drawer/DrawerCloseButton.svelte +2 -2
  12. package/components/Disclosure/Drawer/DrawerContent.svelte +20 -18
  13. package/components/Disclosure/Modal/ModalCloseButton.svelte +3 -6
  14. package/components/Disclosure/Modal/ModalContent.svelte +18 -19
  15. package/components/Disclosure/Modal/ModalTrigger.svelte +9 -11
  16. package/components/Disclosure/Popover/PopoverCloseButton.svelte +4 -5
  17. package/components/Disclosure/Popover/PopoverContent.svelte +12 -23
  18. package/components/Disclosure/Popover/PopoverTrigger.svelte +9 -13
  19. package/components/FancyList/FancyList.svelte +3 -6
  20. package/components/Form/ButtonGroup.svelte +30 -22
  21. package/components/Form/Checkbox.svelte +23 -18
  22. package/components/Form/Checkboxes.svelte +29 -27
  23. package/components/Form/Form.svelte +3 -6
  24. package/components/Form/Radio.svelte +11 -14
  25. package/components/Form/Radios.svelte +27 -27
  26. package/components/Form/Select.svelte +48 -60
  27. package/components/Form/TextInput.svelte +13 -15
  28. package/components/Form/Textarea.svelte +13 -20
  29. package/components/Image/Figure.svelte +13 -0
  30. package/components/Image/Image-old.svelte +58 -0
  31. package/components/Image/Image.svelte +41 -0
  32. package/components/Image/Picture.svelte +71 -0
  33. package/components/Image/image.js +61 -0
  34. package/components/LocalStorage/LocalStoragePush.svelte +1 -2
  35. package/components/LocalStorage/LocalStorageRemove.svelte +1 -2
  36. package/components/Markdown/Markdown.svelte +1 -1
  37. package/components/Resizer/Resizer.svelte +24 -23
  38. package/components/SVG/SVG.svelte +23 -21
  39. package/components/SVG/SVGGradient.svelte +7 -5
  40. package/components/Scrambler/Scrambler.svelte +1 -2
  41. package/components/Status.svelte +1 -3
  42. package/components/Tabs/Tablist.svelte +7 -8
  43. package/components/Tabs/Tabpanels.svelte +5 -1
  44. package/components/Tabs/Tabs.svelte +2 -3
  45. package/components/Youtube.svelte +7 -10
  46. package/package.json +2 -1
  47. package/utils/index.js +1 -0
  48. package/utils/init.js +3 -0
  49. package/utils/loadscripts.js +2 -0
  50. package/vitest.config.js +8 -0
  51. package/components/Image.svelte +0 -24
@@ -1,21 +1,16 @@
1
1
  <script>
2
- /* eslint-disable no-labels */
3
2
  import { toTitle } from '@splendidlabz/utils'
4
3
  import { randomString } from '@splendidlabz/utils/dom'
4
+ import { init } from '../../utils'
5
5
  import Icon from '../Icon/Icon.svelte'
6
6
  import HiddenInput from './HiddenInput.svelte'
7
7
 
8
8
  let {
9
- name = '',
9
+ name: nameProp = '',
10
10
  items = $bindable([]),
11
11
  options = $bindable([]),
12
12
  selected = $bindable(null),
13
13
  ref = $bindable(null),
14
- ...p
15
- } = $props()
16
-
17
- const {
18
- // Classes
19
14
  class: klass = '',
20
15
  icon = null,
21
16
  iconClass = null,
@@ -25,76 +20,69 @@
25
20
  hint = null,
26
21
  hintClass,
27
22
  fieldClass = null,
28
-
29
- // Options and props
30
23
  id = randomString(8),
31
24
  required = null,
32
25
  readonly = null,
33
26
  disabled = null,
34
27
  multiple = null,
35
- size = 0, // Number of lines to show, useful for multiselect, 0, but show 4 lines by default.
36
-
28
+ size = 0,
37
29
  onchange = null,
38
30
  ...rest
39
- } = p
31
+ } = $props()
40
32
 
41
- if (!options.length && !items.length) {
42
- console.error(
43
- 'Select requires either `options` or `items` to be passed in.',
44
- )
45
- }
33
+ const isSingle = $derived(!multiple)
34
+ const hintId = $derived(hint ? id + '-hint' : '')
35
+ const hintDescription = $derived(`${hintId}`.trim() || null)
46
36
 
47
- const isSingle = !multiple
48
- const hintId = hint ? id + '-hint' : ''
49
- const hintDescription = `${hintId}`.trim() || null
50
-
51
- // ========================================
52
- // Some Checks & Normalizations
53
- // ========================================
54
- // Fixes multiple name and selected values
55
- if (multiple) {
56
- if (!name.endsWith('[]')) name = name += '[]'
57
- if (!Array.isArray(selected)) selected = []
58
- }
37
+ let name = $state('')
59
38
 
60
- // Normalize items
61
- if (options.length) items = options
62
- items = items.map(option => {
63
- if (typeof option === 'string') option = { value: option }
39
+ init(() => {
40
+ name = nameProp
64
41
 
65
- if (!option.label) option.label = toTitle(option.value)
42
+ if (!options.length && !items.length) {
43
+ console.error(
44
+ 'Select requires either `options` or `items` to be passed in.',
45
+ )
46
+ }
66
47
 
67
- if (isSingle) {
68
- // Updates selected with selected options
69
- if (option.selected) selected = option.value
48
+ // Fixes multiple name and selected values
49
+ if (multiple) {
50
+ if (!name.endsWith('[]')) name += '[]'
51
+ if (!Array.isArray(selected)) selected = []
52
+ }
70
53
 
71
- // Updates options with selected values
72
- if (option.value === selected) option.selected = true
73
- else option.selected = false
74
- } else {
75
- // Updates selected with selected options
76
- if (option.selected && !selected.includes(option.value))
77
- selected.push(option.value)
54
+ // Normalize items
55
+ if (options.length) items = options
56
+ items = items.map(option => {
57
+ if (typeof option === 'string') option = { value: option }
58
+ if (!option.label) option.label = toTitle(option.value)
59
+
60
+ if (isSingle) {
61
+ if (option.selected) selected = option.value
62
+ if (option.value === selected) option.selected = true
63
+ else option.selected = false
64
+ } else {
65
+ if (option.selected && !selected.includes(option.value))
66
+ selected.push(option.value)
67
+ if (selected.includes(option.value)) option.selected = true
68
+ else option.selected = false
69
+ }
70
+
71
+ return option
72
+ })
78
73
 
79
- // Updates options with selected values
80
- if (selected.includes(option.value)) option.selected = true
81
- else option.selected = false
74
+ // Preselect first item that is not disabled. (Single only)
75
+ setSelected: if (!multiple && !selected) {
76
+ const firstNotDisabled = items.findIndex(item => !item.disabled)
77
+ if (firstNotDisabled === -1) break setSelected
78
+ const s = items[firstNotDisabled] || items[0]
79
+ s.selected = true
80
+ selected = s.value
82
81
  }
83
-
84
- return option
85
82
  })
86
83
 
87
- // Preselect first item that is not disabled. (Single only)
88
- setSelected: if (!multiple && !selected) {
89
- const firstNotDisabled = items.findIndex(item => !item.disabled)
90
- if (firstNotDisabled === -1) break setSelected
91
- const s = items[firstNotDisabled] || items[0]
92
- s.selected = true
93
- selected = s.value
94
- }
95
-
96
84
  // Properties for the select element
97
- const properties = {
85
+ const properties = $derived({
98
86
  ...rest,
99
87
  id,
100
88
  name,
@@ -104,7 +92,7 @@
104
92
  onchange: handleChange,
105
93
  required,
106
94
  size,
107
- }
95
+ })
108
96
 
109
97
  function handleChange() {
110
98
  items.forEach(item => {
@@ -2,9 +2,11 @@
2
2
  import { randomString } from '@splendidlabz/utils/dom'
3
3
  import { useActions } from '../../actions/use-actions'
4
4
  import Markdown from '../Markdown/Markdown.svelte'
5
+ import { init } from '../../utils'
5
6
 
6
- let { value = $bindable(''), ref = $bindable(null), ...p } = $props()
7
- const {
7
+ let {
8
+ value = $bindable(''),
9
+ ref = $bindable(null),
8
10
  // Classes
9
11
  class: klass,
10
12
  style,
@@ -13,12 +15,10 @@
13
15
  errorClass,
14
16
  inputGroupClass,
15
17
  fieldClass,
16
-
17
18
  // Slots
18
19
  before,
19
20
  after,
20
21
  children,
21
-
22
22
  // Options and props
23
23
  id = randomString(8),
24
24
  type = 'text',
@@ -31,26 +31,24 @@
31
31
  readonly = null,
32
32
  actions = [], // Might not need this anymore in S5.
33
33
  ...rest
34
- } = p
34
+ } = $props()
35
35
 
36
36
  // For this binding, does it send upwards?? Need testing
37
37
 
38
38
  // IDs for aria-describedby
39
- const hintId = hint ? id + '-hint' : ''
40
- const errorId = error ? id + '-error' : ''
41
- const describedBy = `${hintId} ${errorId}`.trim() || null
39
+ const hintId = $derived(hint ? id + '-hint' : '')
40
+ const errorId = $derived(error ? id + '-error' : '')
41
+ const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
42
42
 
43
43
  // ========================================
44
44
  // Check for errors
45
45
  // ========================================
46
- if (!name) throw new Error('TextInput requires a name')
47
-
46
+ // Other possible types: date, number, time, week, file, image, month, number, radio, color
48
47
  const SUPPORTED_TYPES = ['text', 'email', 'password', 'search', 'url', 'tel']
49
- // Other possible types
50
- // date, number, time, week, file, image, month, number, radio, color
51
- if (!SUPPORTED_TYPES.includes(type)) {
52
- throw new Error(`Invalid type: ${type}`)
53
- }
48
+ init(() => {
49
+ if (!name) throw new Error('TextInput requires a name')
50
+ if (!SUPPORTED_TYPES.includes(type)) throw new Error(`Invalid type: ${type}`)
51
+ })
54
52
  </script>
55
53
 
56
54
  <!-- Snippets -->
@@ -1,10 +1,10 @@
1
1
  <script>
2
2
  import { randomString } from '@splendidlabz/utils/dom'
3
3
  import { useActions } from '../../actions/use-actions'
4
- import { mergeOpts } from '../../utils'
5
4
 
6
- let { value = $bindable(''), ref = $bindable(null), ...p } = $props()
7
- const {
5
+ let {
6
+ value = $bindable(''),
7
+ ref = $bindable(null),
8
8
  class: klass,
9
9
  style,
10
10
  labelClass,
@@ -12,36 +12,29 @@
12
12
  errorClass,
13
13
  inputGroupClass,
14
14
  fieldClass,
15
-
16
- // Slots
17
- before,
18
- after,
19
- children,
20
- } = p
21
-
22
- const {
23
- // Options and props
24
15
  id = randomString(8),
25
16
  name,
26
-
27
17
  label = '',
28
18
  hint = null,
29
19
  error = null,
30
20
  disabled = null,
31
21
  readonly = null,
32
22
  actions = [],
33
- stack = false,
34
-
35
- // Features
36
23
  oninput = null,
37
24
  autogrow = false,
25
+
26
+ // Slots
27
+ before,
28
+ after,
29
+ children,
30
+
38
31
  ...rest
39
- } = mergeOpts(p) // Better name would be better to extract options...
32
+ } = $props()
40
33
 
41
34
  // IDs for aria-describedby
42
- const hintId = hint ? id + '-hint' : ''
43
- const errorId = error ? id + '-error' : ''
44
- const describedBy = `${hintId} ${errorId}`.trim() || null
35
+ const hintId = $derived(hint ? id + '-hint' : '')
36
+ const errorId = $derived(error ? id + '-error' : '')
37
+ const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
45
38
 
46
39
  // ========================================
47
40
  // Extra Features
@@ -0,0 +1,13 @@
1
+ <script>
2
+ import Markdown from '../Markdown/Markdown.svelte'
3
+ const { children, caption, captionClass } = $props()
4
+ </script>
5
+
6
+ <figure>
7
+ {@render children?.()}
8
+ {#if caption}
9
+ <figcaption class={captionClass}>
10
+ <Markdown content={caption} inline />
11
+ </figcaption>
12
+ {/if}
13
+ </figure>
@@ -0,0 +1,58 @@
1
+ <script>
2
+ import Markdown from './Markdown/Markdown.svelte'
3
+ import { resolveImage } from './image.js'
4
+
5
+ let {
6
+ class: klass = '',
7
+ figureClass = undefined,
8
+ figure = true,
9
+ caption = undefined,
10
+ captionClass = undefined,
11
+ picture = true,
12
+ pictureClass = undefined,
13
+ src,
14
+ alt = '',
15
+ loading = 'lazy',
16
+ ...rest
17
+ } = $props()
18
+
19
+ const image = resolveImage(src)
20
+ const isGif = image.format === 'gif' || image.src?.includes('.gif')
21
+ const usePicture = picture && !isGif
22
+ </script>
23
+
24
+ {#snippet img()}
25
+ <img
26
+ class="{klass} image media"
27
+ {alt}
28
+ height={image.height}
29
+ {loading}
30
+ src={image.src}
31
+ srcset={image.srcset}
32
+ width={image.width}
33
+ {...rest}
34
+ />
35
+ {/snippet}
36
+
37
+ {#snippet media()}
38
+ {#if usePicture}
39
+ <picture class={pictureClass}>
40
+ {@render img()}
41
+ </picture>
42
+ {:else}
43
+ {@render img()}
44
+ {/if}
45
+ {/snippet}
46
+
47
+ {#if figure}
48
+ <figure class={figureClass}>
49
+ {@render media()}
50
+ {#if caption}
51
+ <figcaption class={captionClass}>
52
+ <Markdown content={caption} inline />
53
+ </figcaption>
54
+ {/if}
55
+ </figure>
56
+ {:else}
57
+ {@render media()}
58
+ {/if}
@@ -0,0 +1,41 @@
1
+ <script>
2
+ import { splitObject } from '@splendidlabz/utils'
3
+ import Figure from './Figure.svelte'
4
+ const {
5
+ class: klass = '',
6
+ figure = true,
7
+ picture = true,
8
+ figureClass = '',
9
+ caption,
10
+ src,
11
+ alt = '',
12
+ width,
13
+ height,
14
+ loading = 'lazy',
15
+ ...p
16
+ } = $props()
17
+
18
+ const { p: figureProps, o: p1 } = splitObject(p, ['caption', 'captionClass'])
19
+ const { p: pictureProps, o: rest } = splitObject(p1, ['pictureClass'])
20
+ </script>
21
+
22
+ {#snippet img()}
23
+ <img
24
+ class="{klass} image media"
25
+ {alt}
26
+ height={image.height}
27
+ {loading}
28
+ src={image.src}
29
+ srcset={image.srcset}
30
+ width={image.width}
31
+ {...rest}
32
+ />
33
+ {/snippet}
34
+
35
+ {#if figure}
36
+ <Figure class={figureClass} {...figureProps}>
37
+ {@render media()}
38
+ </Figure>
39
+ {:else}
40
+ {@render media()}
41
+ {/if}
@@ -0,0 +1,71 @@
1
+ <script>
2
+ import Markdown from '../Markdown/Markdown.svelte'
3
+ import { getQuery } from './image.js'
4
+
5
+ let {
6
+ class: klass = '',
7
+ pictureClass = undefined,
8
+ sources,
9
+ alt = '',
10
+ loading = 'lazy',
11
+ fallbackIndex = undefined,
12
+ figure = true,
13
+ figureClass = undefined,
14
+ captionClass = undefined,
15
+ caption = undefined,
16
+ ...rest
17
+ } = $props()
18
+
19
+ if (!sources) throw new Error('Missing prop: sources')
20
+
21
+ const fallback = sources[fallbackIndex] ?? sources[sources.length - 1]
22
+ const fallbackSrc = fallback.image.src
23
+ const fallbackAttrs = fallback.image.attributes
24
+ </script>
25
+
26
+ {#if figure}
27
+ <figure class={figureClass} {...rest}>
28
+ <picture class={pictureClass}>
29
+ {#each sources as source}
30
+ <source
31
+ media={getQuery(source)}
32
+ srcset={source.image.srcSet.attribute}
33
+ type={`image/${source.image.options.format}`}
34
+ />
35
+ {/each}
36
+
37
+ <img
38
+ class="{klass} image media"
39
+ {alt}
40
+ height={fallbackAttrs.height}
41
+ {loading}
42
+ src={fallbackSrc}
43
+ width={fallbackAttrs.width}
44
+ />
45
+ </picture>
46
+ {#if caption}
47
+ <figcaption class={captionClass}>
48
+ <Markdown content={caption} inline />
49
+ </figcaption>
50
+ {/if}
51
+ </figure>
52
+ {:else}
53
+ <picture class={pictureClass} {...rest}>
54
+ {#each sources as source}
55
+ <source
56
+ media={getQuery(source)}
57
+ srcset={source.image.srcSet.attribute}
58
+ type={`image/${source.image.options.format}`}
59
+ />
60
+ {/each}
61
+
62
+ <img
63
+ class="{klass} image media"
64
+ {alt}
65
+ height={fallbackAttrs.height}
66
+ {loading}
67
+ src={fallbackSrc}
68
+ width={fallbackAttrs.width}
69
+ />
70
+ </picture>
71
+ {/if}
@@ -0,0 +1,61 @@
1
+ const images = import.meta.glob(
2
+ '/src/assets/**/*.{jpeg,jpg,png,gif,avif,webp}',
3
+ {
4
+ eager: true,
5
+ },
6
+ )
7
+
8
+ /**
9
+ * Imports an image from /src/assets and returns its metadata.
10
+ * @param {string} src - Path to the image (e.g. '/src/assets/hero.jpg')
11
+ * @returns {{ src: string, width: number, height: number, format: string }}
12
+ */
13
+ export function importImage(src) {
14
+ const keys = Object.keys(images)
15
+ const key = keys.find(key => key.includes(src))
16
+ if (!key) throw new Error(`Image ${src} not found`)
17
+ return images[key].default
18
+ }
19
+
20
+ /**
21
+ * Creates a media query string from min/max values or a custom query.
22
+ * @param {Object} props
23
+ * @param {number} [props.min]
24
+ * @param {number} [props.max]
25
+ * @param {string} [props.query]
26
+ * @returns {string}
27
+ */
28
+ export function getQuery(props) {
29
+ const { min, max } = props
30
+ let { query = '' } = props
31
+
32
+ if (query) return query
33
+
34
+ if (!min && !max)
35
+ throw new Error('min or max must be declared in artDirection item')
36
+ if (min) query += `(min-width: ${min}px)`
37
+ if (max) query += `(max-width: ${max}px)`
38
+ return query
39
+ }
40
+
41
+ /**
42
+ * Normalizes any image input into a flat { src, width, height, srcset } for rendering.
43
+ * Accepts a string path, a static import result, or an img()/getAstroImage result.
44
+ * @param {string|Object} image
45
+ * @returns {{ src: string, width: number, height: number, srcset?: string, format?: string }}
46
+ */
47
+ export function resolveImage(image) {
48
+ if (typeof image === 'string') image = importImage(image)
49
+
50
+ if (image.attributes) {
51
+ return {
52
+ src: image.src,
53
+ width: image.attributes.width,
54
+ height: image.attributes.height,
55
+ srcset: image.srcSet?.attribute,
56
+ format: image.options?.format,
57
+ }
58
+ }
59
+
60
+ return image
61
+ }
@@ -10,13 +10,12 @@
10
10
  import { onMount } from 'svelte'
11
11
 
12
12
  const { name, value } = $props()
13
- if (!name) throw new Error('name is required')
14
13
 
15
14
  onMount(store)
16
15
 
17
16
  function store() {
18
17
  if (typeof window === 'undefined') return
19
- if (!name) return
18
+ if (!name) throw new Error('name is required')
20
19
  localStore.set(name, value)
21
20
  }
22
21
  </script>
@@ -3,13 +3,12 @@
3
3
  import { onMount } from 'svelte'
4
4
 
5
5
  const { name } = $props()
6
- if (!name) throw new Error('name is required')
7
6
 
8
7
  onMount(store)
9
8
 
10
9
  function store() {
11
10
  if (typeof window === 'undefined') return
12
- if (!name) return
11
+ if (!name) throw new Error('name is required')
13
12
  localStore.remove(name)
14
13
  }
15
14
  </script>
@@ -8,7 +8,7 @@
8
8
  <script>
9
9
  import { markdown } from '@splendidlabz/utils'
10
10
  const { content, inline = false } = $props()
11
- const html = markdown(content, { inline })
11
+ const html = $derived(markdown(content, { inline }))
12
12
  </script>
13
13
 
14
14
  <!-- eslint-disable-next-line svelte/no-at-html-tags -->
@@ -1,38 +1,39 @@
1
1
  <script>
2
- import { mergeOpts } from '../../utils/merge-opts.js'
3
2
  import { resizer } from './resizer.js'
4
3
 
5
- let p = $props()
6
-
7
- const DEFAULT_OPTIONS = {
8
- direction: 'horizontal', // horizontal | vertical
9
- placement: 'right', // Only works for right and bottom for now.
10
- target: null,
11
- minWidth: 0,
12
- minHeight: 0,
13
- canExceedOriginal: false,
14
- }
15
-
16
- p = mergeOpts(DEFAULT_OPTIONS, p)
17
-
18
- const { class: klass = '', direction, placement } = p
4
+ const {
5
+ class: klass = '',
6
+ direction = 'horizontal', // horizontal | vertical
7
+ placement = 'right', // Only works for right and bottom for now.
8
+ target = null,
9
+ minWidth = 0,
10
+ minHeight = 0,
11
+ canExceedOriginal = false,
12
+ } = $props()
19
13
 
20
14
  // Notes:
21
15
  // - Tricky to get 'left' to work because it requires logical CSS positioning properties which may not be used to create the element.
22
16
  // - Tricky to get 'top' to work because it requires element to be bottom-fixed so top can be adjustable.
23
17
 
24
- const placementClass = {
25
- bottom: 'resizer-bottom',
26
- right: 'resizer-right',
27
- }[placement]
28
-
29
- const directionClass = {
18
+ const placementClasses = { bottom: 'resizer-bottom', right: 'resizer-right' }
19
+ const directionClasses = {
30
20
  horizontal: 'resizer-horizontal',
31
21
  vertical: 'resizer-vertical',
32
- }[direction]
22
+ }
23
+
24
+ const placementClass = $derived(placementClasses[placement])
25
+ const directionClass = $derived(directionClasses[direction])
26
+ const opts = $derived({
27
+ direction,
28
+ placement,
29
+ target,
30
+ minWidth,
31
+ minHeight,
32
+ canExceedOriginal,
33
+ })
33
34
  </script>
34
35
 
35
36
  <div
36
37
  class="resizer {placementClass} {directionClass} {klass}"
37
- use:resizer={p}
38
+ use:resizer={opts}
38
39
  ></div>