@splendidlabz/svelte 4.10.0 → 4.11.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @splendidlabz/svelte
2
2
 
3
+ ## 4.11.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 8e3054d: Refactor Status to use `<output>` element with automatic live region: `role="alert"` for errors, `role="status"` for all other states. Add `statusText` prop. Form now accepts `status` and `statusText` props and renders a linked Status component.
8
+
9
+ ### Patch Changes
10
+
11
+ - 8e3054d: Fix accessibility of TextInput and Textarea: move `aria-label` onto the input element, make error live region always present in DOM (sr-only when empty), use `<output for={id} role="alert">` pattern for error announcements.
12
+ - df4623c: Resolve Svelte `state_referenced_locally` warnings in form, tabcordion, disclosure, and image components.
13
+ - Updated dependencies [42cd5a2]
14
+ - @splendidlabz/utils@1.11.6
15
+
3
16
  ## 4.10.0
4
17
 
5
18
  ### Minor Changes
@@ -10,42 +10,55 @@
10
10
  as = 'dialog',
11
11
  ref = $bindable(null),
12
12
  referenceNode = $bindable(null),
13
- ...p
13
+ ...props
14
14
  } = $props()
15
15
 
16
- p = mergeOpts(p)
17
-
18
- const {
19
- componentName = 'Popover',
20
- 'aria-label': ariaLabel,
21
- 'aria-labelledBy': ariaLabelledBy,
22
- 'aria-describedby': ariaDescribedBy,
23
-
24
- // Props
25
- id,
26
- popover = 'auto', // manual, hint
27
-
28
- // Event listeners
29
- onbeforetoggle = null,
30
- ontoggle = null,
31
- onopen = null,
32
- onclose = null,
33
-
34
- // Slots
35
- children,
36
- ...rest
37
- } = p
16
+ const p = $derived(mergeOpts(props))
17
+
18
+ const componentName = $derived(p.componentName ?? 'Popover')
19
+ const ariaLabel = $derived(p['aria-label'])
20
+ const ariaLabelledBy = $derived(p['aria-labelledBy'])
21
+ const ariaDescribedBy = $derived(p['aria-describedby'])
22
+
23
+ const id = $derived(p.id)
24
+ const popover = $derived(p.popover ?? 'auto') // manual, hint
25
+
26
+ const onbeforetoggle = $derived(p.onbeforetoggle ?? null)
27
+ const ontoggle = $derived(p.ontoggle ?? null)
28
+ const onopen = $derived(p.onopen ?? null)
29
+ const onclose = $derived(p.onclose ?? null)
30
+
31
+ const children = $derived(p.children)
32
+ const rest = $derived.by(() => {
33
+ const {
34
+ componentName = 'Popover',
35
+ 'aria-label': ariaLabel,
36
+ 'aria-labelledBy': ariaLabelledBy,
37
+ 'aria-describedby': ariaDescribedBy,
38
+ id,
39
+ popover = 'auto',
40
+ onbeforetoggle = null,
41
+ ontoggle = null,
42
+ onopen = null,
43
+ onclose = null,
44
+ children,
45
+ ...rest
46
+ } = p
47
+ return rest
48
+ })
38
49
 
39
50
  // ========================
40
51
  // Checks
41
52
  // ========================
42
- if (!id) throw new Error(`${componentName} requires an id`)
43
-
44
- // Accessible name warning
45
- checkForAccessibleName({
46
- name: `${componentName} (id: ${id})`,
47
- ariaLabel,
48
- ariaLabelledBy,
53
+ $effect(() => {
54
+ if (!id) throw new Error(`${componentName} requires an id`)
55
+
56
+ // Accessible name warning
57
+ checkForAccessibleName({
58
+ name: `${componentName} (id: ${id})`,
59
+ ariaLabel,
60
+ ariaLabelledBy,
61
+ })
49
62
  })
50
63
 
51
64
  // ========================================
@@ -54,7 +67,8 @@
54
67
  let trigger = null
55
68
  let floating = null
56
69
 
57
- const { picked: floatingOpts } = splitObject(rest, FLOATING_OPTS_KEYS)
70
+ const floatingSplit = $derived(splitObject(rest, FLOATING_OPTS_KEYS))
71
+ const floatingOpts = $derived(floatingSplit.picked)
58
72
 
59
73
  // ========================================
60
74
  // Event Listeners
@@ -20,26 +20,39 @@
20
20
  ...p
21
21
  } = $props()
22
22
 
23
- const opts = mergeOpts(DRAWER_CONTENT_OPTS, DRAWER_POSITION_OPTS[position], p)
23
+ const opts = $derived(
24
+ mergeOpts(DRAWER_CONTENT_OPTS, DRAWER_POSITION_OPTS[position], p),
25
+ )
24
26
 
25
- const {
26
- id,
27
- class: klass = '',
28
- style: styleString = '',
29
- backdropClass = '',
30
- portal = true,
31
- placement,
32
- backdrop,
33
- blurBackdrop,
27
+ const id = $derived(opts.id)
28
+ const klass = $derived(opts.class ?? '')
29
+ const styleString = $derived(opts.style ?? '')
30
+ const backdropClass = $derived(opts.backdropClass ?? '')
31
+ const portal = $derived(opts.portal ?? true)
32
+ const placement = $derived(opts.placement)
33
+ const backdrop = $derived(opts.backdrop)
34
+ const blurBackdrop = $derived(opts.blurBackdrop)
34
35
 
35
- // Slots
36
- ...rest
37
- } = opts
36
+ const rest = $derived.by(() => {
37
+ const {
38
+ id,
39
+ class: klass = '',
40
+ style: styleString = '',
41
+ backdropClass = '',
42
+ portal = true,
43
+ placement,
44
+ backdrop,
45
+ blurBackdrop,
46
+ ...rest
47
+ } = opts
48
+ return rest
49
+ })
38
50
 
39
- const { picked: transitionOpts } = splitObject(rest, TRANSITION_OPT_KEYS)
51
+ const transitionSplit = $derived(splitObject(rest, TRANSITION_OPT_KEYS))
52
+ const transitionOpts = $derived(transitionSplit.picked)
40
53
 
41
- const TBLR = getTBLRValues({ placement, position })
42
- const style = toStyleString(TBLR) + styleString
54
+ const TBLR = $derived(getTBLRValues({ placement, position }))
55
+ const style = $derived(toStyleString(TBLR) + styleString)
43
56
 
44
57
  let d = $derived(getDisclosure(id))
45
58
  let show = $derived(d?.state.show)
@@ -79,7 +92,7 @@
79
92
 
80
93
  {#if show}
81
94
  {#if position === 'fixed'}
82
- {#if opts.portal}
95
+ {#if portal}
83
96
  <Portal>{@render drawerBackdrop()}</Portal>
84
97
  {:else}
85
98
  {@render drawerBackdrop()}
@@ -9,17 +9,25 @@
9
9
  import { MODAL_CONTENT_OPTS } from './modal.js'
10
10
 
11
11
  let { ref = $bindable(null), ...p } = $props()
12
- const opts = mergeOpts(MODAL_CONTENT_OPTS, p)
12
+ const opts = $derived(mergeOpts(MODAL_CONTENT_OPTS, p))
13
13
 
14
- const {
15
- class: klass = '',
16
- backdrop,
17
- backdropClass,
18
- blurBackdrop,
19
- ...rest
20
- } = opts
14
+ const klass = $derived(opts.class ?? '')
15
+ const backdrop = $derived(opts.backdrop)
16
+ const backdropClass = $derived(opts.backdropClass)
17
+ const blurBackdrop = $derived(opts.blurBackdrop)
18
+ const rest = $derived.by(() => {
19
+ const {
20
+ class: klass = '',
21
+ backdrop,
22
+ backdropClass,
23
+ blurBackdrop,
24
+ ...rest
25
+ } = opts
26
+ return rest
27
+ })
21
28
 
22
- const { picked: transitionOpts } = splitObject(rest, TRANSITION_OPT_KEYS)
29
+ const transitionSplit = $derived(splitObject(rest, TRANSITION_OPT_KEYS))
30
+ const transitionOpts = $derived(transitionSplit.picked)
23
31
  </script>
24
32
 
25
33
  {#snippet modalBackdrop()}
@@ -18,43 +18,55 @@
18
18
 
19
19
  let { ref = $bindable(null), ...p } = $props()
20
20
 
21
- const {
22
- class: klass,
23
- id,
24
- role = 'dialog',
25
- 'aria-label': ariaLabel,
26
- 'aria-labelledBy': ariaLabelledBy,
27
- 'aria-describedby': ariaDescribedBy,
28
- componentName = 'DisclosureContent',
29
- portal = true,
30
- scrollToAnchor = false,
31
-
32
- // Slots
33
- children,
34
-
35
- // Event handlers are spread directly into ...rest
36
- // Custom events are onopen and onclose
37
- ...restProps
38
- } = p
21
+ const klass = $derived(p.class)
22
+ const id = $derived(p.id)
23
+ const role = $derived(p.role ?? 'dialog')
24
+ const ariaLabel = $derived(p['aria-label'])
25
+ const ariaLabelledBy = $derived(p['aria-labelledBy'])
26
+ const ariaDescribedBy = $derived(p['aria-describedby'])
27
+ const componentName = $derived(p.componentName ?? 'DisclosureContent')
28
+ const portal = $derived(p.portal ?? true)
29
+ const scrollToAnchor = $derived(p.scrollToAnchor ?? false)
30
+ const children = $derived(p.children)
31
+ const restProps = $derived.by(() => {
32
+ const {
33
+ class: klass,
34
+ id,
35
+ role = 'dialog',
36
+ 'aria-label': ariaLabel,
37
+ 'aria-labelledBy': ariaLabelledBy,
38
+ 'aria-describedby': ariaDescribedBy,
39
+ componentName = 'DisclosureContent',
40
+ portal = true,
41
+ scrollToAnchor = false,
42
+ children,
43
+ ...restProps
44
+ } = p
45
+ return restProps
46
+ })
39
47
 
40
- const { picked: disclosureContentOpts, omitted: rest } = splitObject(
41
- restProps,
42
- DISCLOSURE_CONTENT_OPT_KEYS,
48
+ const disclosureSplit = $derived(
49
+ splitObject(restProps, DISCLOSURE_CONTENT_OPT_KEYS),
43
50
  )
51
+ const disclosureContentOpts = $derived(disclosureSplit.picked)
52
+ const rest = $derived(disclosureSplit.omitted)
44
53
 
45
- const { picked: transitionOpts, omitted: remaining } = splitObject(
46
- rest,
47
- TRANSITION_OPT_KEYS,
54
+ const transitionSplit = $derived(
55
+ splitObject(rest, TRANSITION_OPT_KEYS),
48
56
  )
57
+ const transitionOpts = $derived(transitionSplit.picked)
58
+ const remaining = $derived(transitionSplit.omitted)
49
59
 
50
- if (!id) throw new Error(`${componentName} requires an id`)
60
+ $effect(() => {
61
+ if (!id) throw new Error(`${componentName} requires an id`)
51
62
 
52
- // Accessible name warning
53
- if (role === 'dialog' && !hasAccessibleName(ariaLabel, ariaLabelledBy)) {
54
- console.warn(
55
- `DrawerContent (with id: ${id}) is a dialog and should have an accessible name. Please create one with aria-label or aria-labelledby.`,
56
- )
57
- }
63
+ // Accessible name warning
64
+ if (role === 'dialog' && !hasAccessibleName(ariaLabel, ariaLabelledBy)) {
65
+ console.warn(
66
+ `DrawerContent (with id: ${id}) is a dialog and should have an accessible name. Please create one with aria-label or aria-labelledby.`,
67
+ )
68
+ }
69
+ })
58
70
 
59
71
  let d = $derived(getDisclosure(id))
60
72
  let show = $derived(d?.state.show)
@@ -8,26 +8,37 @@
8
8
 
9
9
  let { show = $bindable(false), ...p } = $props()
10
10
 
11
- const {
12
- class: klass = '',
13
- componentName = 'DisclosureTrigger',
14
- icon = 'tabler:menu',
15
- iconClass,
16
- 'aria-haspopup': ariaHasPopup = 'dialog',
17
- target,
11
+ const klass = $derived(p.class ?? '')
12
+ const componentName = $derived(p.componentName ?? 'DisclosureTrigger')
13
+ const icon = $derived(p.icon ?? 'tabler:menu')
14
+ const iconClass = $derived(p.iconClass)
15
+ const ariaHasPopup = $derived(p['aria-haspopup'] ?? 'dialog')
16
+ const target = $derived(p.target)
17
+ const children = $derived(p.children)
18
+ const restProps = $derived.by(() => {
19
+ const {
20
+ class: klass = '',
21
+ componentName = 'DisclosureTrigger',
22
+ icon = 'tabler:menu',
23
+ iconClass,
24
+ 'aria-haspopup': ariaHasPopup = 'dialog',
25
+ target,
26
+ children,
27
+ ...restProps
28
+ } = p
29
+ return restProps
30
+ })
18
31
 
19
- // Slots
20
- children,
21
- ...restProps
22
- } = p
23
-
24
- const { picked: triggerOpts, omitted: rest } = splitObject(
25
- restProps,
26
- DISCLOSURE_TRIGGER_OPT_KEYS,
32
+ const triggerSplit = $derived(
33
+ splitObject(restProps, DISCLOSURE_TRIGGER_OPT_KEYS),
27
34
  )
35
+ const triggerOpts = $derived(triggerSplit.picked)
36
+ const rest = $derived(triggerSplit.omitted)
28
37
 
29
- if (!target)
30
- throw new Error(`${componentName} must contain a target property`)
38
+ $effect(() => {
39
+ if (!target)
40
+ throw new Error(`${componentName} must contain a target property`)
41
+ })
31
42
 
32
43
  // TODO: Test creating an IconSnippet to render the icon vs contents
33
44
  // <script module> currently breaks when prettier is enabled. So, ¯\_(ツ)_/¯ if we can ever do that.
@@ -1,10 +1,12 @@
1
1
  <script>
2
2
  import { flattenArrayFields, formDataToObject } from '@splendidlabz/utils'
3
- import { sanitize as sanitizeFn } from '@splendidlabz/utils/dom'
3
+ import { randomString, sanitize as sanitizeFn } from '@splendidlabz/utils/dom'
4
+ import Status from '../Status.svelte'
4
5
  import Honeypot from './Honeypot.svelte'
5
6
 
6
7
  let {
7
8
  ref = $bindable(null),
9
+ id = $bindable(randomString()),
8
10
  class: klass = '',
9
11
  method = 'post',
10
12
  action = null,
@@ -14,6 +16,8 @@
14
16
  honeypotDuration = 2000,
15
17
  onsubmit = null,
16
18
  children,
19
+ status,
20
+ statusText,
17
21
  ...rest
18
22
  } = $props()
19
23
 
@@ -49,8 +53,9 @@
49
53
  </script>
50
54
 
51
55
  <form
52
- {...rest}
53
56
  bind:this={ref}
57
+ {id}
58
+ {...rest}
54
59
  class="form {klass}"
55
60
  {action}
56
61
  {method}
@@ -64,3 +69,4 @@
64
69
  {/if}
65
70
  {@render children?.()}
66
71
  </form>
72
+ <Status for={id} {status} {statusText} />
@@ -1,8 +1,8 @@
1
1
  <script>
2
2
  import { randomString } from '@splendidlabz/utils/dom'
3
3
  import { useActions } from '../../actions/use-actions'
4
- import Markdown from '../Markdown/Markdown.svelte'
5
4
  import { init } from '../../utils'
5
+ import Markdown from '../Markdown/Markdown.svelte'
6
6
 
7
7
  let {
8
8
  value = $bindable(''),
@@ -37,7 +37,7 @@
37
37
 
38
38
  // IDs for aria-describedby
39
39
  const hintId = $derived(hint ? id + '-hint' : '')
40
- const errorId = $derived(error ? id + '-error' : '')
40
+ const errorId = $derived(id + '-error')
41
41
  const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
42
42
 
43
43
  // ========================================
@@ -47,7 +47,8 @@
47
47
  const SUPPORTED_TYPES = ['text', 'email', 'password', 'search', 'url', 'tel']
48
48
  init(() => {
49
49
  if (!name) throw new Error('TextInput requires a name')
50
- if (!SUPPORTED_TYPES.includes(type)) throw new Error(`Invalid type: ${type}`)
50
+ if (!SUPPORTED_TYPES.includes(type))
51
+ throw new Error(`Invalid type: ${type}`)
51
52
  })
52
53
  </script>
53
54
 
@@ -62,6 +63,7 @@
62
63
  {...{ type }}
63
64
  aria-describedby={describedBy}
64
65
  aria-invalid={error ? 'true' : null}
66
+ aria-label={!labelVisible && label ? label : null}
65
67
  {disabled}
66
68
  {readonly}
67
69
  bind:value
@@ -75,7 +77,6 @@
75
77
  class="input-container text-input text-field {klass}"
76
78
  class:disabled
77
79
  class:readonly
78
- aria-label={(!labelVisible && label) || null}
79
80
  >
80
81
  {#if label && labelVisible}
81
82
  <label class="label input-meta {labelClass}" for={id}>
@@ -87,11 +88,17 @@
87
88
  <Markdown content={hint} inline />
88
89
  </div>
89
90
  {/if}
90
- {#if error}
91
- <output id={errorId} class="error input-meta {errorClass}" for={id}>
91
+ <output
92
+ id={errorId}
93
+ class="error input-meta {errorClass}"
94
+ class:sr-only={!error}
95
+ for={id}
96
+ role="alert"
97
+ >
98
+ {#if error}
92
99
  <Markdown content={error} inline />
93
- </output>
94
- {/if}
100
+ {/if}
101
+ </output>
95
102
 
96
103
  {#if before || after}
97
104
  <div class="input-group {inputGroupClass}">
@@ -33,7 +33,7 @@
33
33
 
34
34
  // IDs for aria-describedby
35
35
  const hintId = $derived(hint ? id + '-hint' : '')
36
- const errorId = $derived(error ? id + '-error' : '')
36
+ const errorId = $derived(id + '-error')
37
37
  const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
38
38
 
39
39
  // ========================================
@@ -84,11 +84,15 @@
84
84
  {#if hint}
85
85
  <div id={hintId} class="hint input-meta {hintClass}">{hint}</div>
86
86
  {/if}
87
- {#if error}
88
- <output id={errorId} class="error input-meta {errorClass}" for={id}
89
- >{error}</output
90
- >
91
- {/if}
87
+ <output
88
+ id={errorId}
89
+ for={id}
90
+ class="error input-meta {errorClass}"
91
+ class:sr-only={!error}
92
+ role="alert"
93
+ >
94
+ {#if error}{error}{/if}
95
+ </output>
92
96
 
93
97
  {#if before || after}
94
98
  <div class="input-group {inputGroupClass}">
@@ -16,9 +16,9 @@
16
16
  ...rest
17
17
  } = $props()
18
18
 
19
- const image = resolveImage(src)
20
- const isGif = image.format === 'gif' || image.src?.includes('.gif')
21
- const usePicture = picture && !isGif
19
+ const image = $derived(resolveImage(src))
20
+ const isGif = $derived(image.format === 'gif' || image.src?.includes('.gif'))
21
+ const usePicture = $derived(picture && !isGif)
22
22
  </script>
23
23
 
24
24
  {#snippet img()}
@@ -15,8 +15,12 @@
15
15
  ...p
16
16
  } = $props()
17
17
 
18
- const { p: figureProps, o: p1 } = splitObject(p, ['caption', 'captionClass'])
19
- const { p: pictureProps, o: rest } = splitObject(p1, ['pictureClass'])
18
+ const figureSplit = $derived(splitObject(p, ['caption', 'captionClass']))
19
+ const figureProps = $derived(figureSplit.p)
20
+ const p1 = $derived(figureSplit.o)
21
+ const pictureSplit = $derived(splitObject(p1, ['pictureClass']))
22
+ const pictureProps = $derived(pictureSplit.p)
23
+ const rest = $derived(pictureSplit.o)
20
24
  </script>
21
25
 
22
26
  {#snippet img()}
@@ -16,11 +16,12 @@
16
16
  ...rest
17
17
  } = $props()
18
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
19
+ const fallback = $derived.by(() => {
20
+ if (!sources) throw new Error('Missing prop: sources')
21
+ return sources[fallbackIndex] ?? sources[sources.length - 1]
22
+ })
23
+ const fallbackSrc = $derived(fallback.image.src)
24
+ const fallbackAttrs = $derived(fallback.image.attributes)
24
25
  </script>
25
26
 
26
27
  {#if figure}
@@ -1,5 +1,17 @@
1
1
  <script>
2
- let { status = $bindable('info'), class: klass = '', children, ...rest } = $props()
2
+ let {
3
+ class: klass = '',
4
+ status = $bindable('info'),
5
+ statusText = '',
6
+ children,
7
+ ...rest
8
+ } = $props()
9
+
10
+ const statusClasses = {
11
+ info: 'status-info',
12
+ success: 'status-success',
13
+ error: 'status-error',
14
+ }
3
15
 
4
16
  function getStatusString(status) {
5
17
  if (typeof status === 'string') return status
@@ -9,17 +21,23 @@
9
21
  return 'info'
10
22
  }
11
23
 
12
- const statusClasses = {
13
- info: 'status-info',
14
- success: 'status-success',
15
- error: 'status-error',
24
+ function getStatusClass(status) {
25
+ return statusClasses[getStatusString(status)]
16
26
  }
17
27
 
18
- let statusClass = $derived.by(_ => statusClasses[getStatusString(status)])
28
+ let statusClass = $derived(getStatusClass(status))
29
+ let role = $derived(getStatusString(status) === 'error' ? 'alert' : 'status')
19
30
  </script>
20
31
 
21
- <div {...rest} class="status {statusClass} {klass}">
22
- {#if children}
23
- {@render children()}
32
+ <output
33
+ {...rest}
34
+ class="status {statusClass} {klass}"
35
+ class:sr-only={!statusText && !children}
36
+ {role}
37
+ >
38
+ {#if statusText}
39
+ {statusText}
40
+ {:else}
41
+ {@render children?.()}
24
42
  {/if}
25
- </div>
43
+ </output>
@@ -23,7 +23,11 @@
23
23
 
24
24
  const tabcordionId = randomString()
25
25
  const tabpanelsId = randomString()
26
- let activeIndex = $state(activeIndexProp)
26
+ function getActiveIndexProp() {
27
+ return activeIndexProp
28
+ }
29
+
30
+ let activeIndex = $state(getActiveIndexProp())
27
31
  const items = $state([])
28
32
 
29
33
  setContext('tabcordion', {
@@ -33,6 +37,10 @@
33
37
  },
34
38
  })
35
39
 
40
+ $effect(() => {
41
+ activeIndex = activeIndexProp
42
+ })
43
+
36
44
  $effect(() => {
37
45
  const tabStore = getTab(tabpanelsId)
38
46
  if (!tabStore) return
@@ -3,5 +3,12 @@
3
3
 
4
4
  const { title, content } = $props()
5
5
  const ctx = getContext('tabcordion')
6
- ctx?.register({ title, content })
6
+ ctx?.register({
7
+ get title() {
8
+ return title
9
+ },
10
+ get content() {
11
+ return content
12
+ },
13
+ })
7
14
  </script>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splendidlabz/svelte",
3
3
  "type": "module",
4
- "version": "4.10.0",
4
+ "version": "4.11.0",
5
5
  "sideEffects": false,
6
6
  "homepage": "https://splendidlabz.com/docs/svelte",
7
7
  "scripts": {
package/svelte.config.js CHANGED
@@ -3,7 +3,6 @@ export default {
3
3
  runes: true,
4
4
  },
5
5
  compilerOptions: {
6
- enableSourcemap: true,
7
6
  cssHash: ({ hash, css, name, filename }) => `${name}-${hash(css)}`,
8
7
  },
9
8
  onwarn: (warning, handler) => {