@splendidlabz/svelte 4.5.0 → 4.6.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,37 @@
1
1
  # @splendidlabz/svelte
2
2
 
3
+ ## 4.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 4efacb3: - 9b5ea9a2 Upgrade Content List
8
+ - Add aria-current attribute to list items
9
+ - Improve structure building and updating
10
+
11
+ ### Patch Changes
12
+
13
+ - 96c92cc: Some cleanup and typo fixes
14
+ - Updated dependencies [4efacb3]
15
+ - Updated dependencies [4efacb3]
16
+ - Updated dependencies [96c92cc]
17
+ - @splendidlabz/styles@4.4.0
18
+ - @splendidlabz/utils@1.10.0
19
+
20
+ ## 4.5.1
21
+
22
+ ### Patch Changes
23
+
24
+ - - 4d4bf110 Fix close others for accordion
25
+ - - e27ace5d Renamed components to be more descriptive
26
+ - PopoverClose to PopoverCloseButton
27
+ - Deprecated `Popover` in favor of `PopoverContent`
28
+ - Deprecated `ModalClose` in favor of `ModalCloseButton`
29
+ - Deprecated `Modal` in favor of `ModalContent`
30
+ - Updated dependencies
31
+ - Updated dependencies
32
+ - @splendidlabz/utils@1.9.1
33
+ - @splendidlabz/styles@4.3.2
34
+
3
35
  ## 4.5.0
4
36
 
5
37
  ### Minor Changes
@@ -22,6 +22,7 @@
22
22
  buttonClass,
23
23
  icon,
24
24
  iconClass,
25
+ closeOthers = false,
25
26
  useMarkdown = true,
26
27
  } = opts
27
28
 
@@ -37,7 +38,7 @@
37
38
  function handleClick(event) {
38
39
  // Toggle accordion show
39
40
  show = !show
40
- if (show) closeOtherAccordions(event)
41
+ if (show && closeOthers) closeOtherAccordions(event)
41
42
  }
42
43
 
43
44
  function handleKeydown(event) {
@@ -53,7 +54,7 @@
53
54
  // Helper functions
54
55
  // ========================================
55
56
  function closeOtherAccordions(event) {
56
- const container = event.target.closest('.accordions')
57
+ const container = event.target.closest('.accordion-stack')
57
58
  if (!container) return
58
59
  const closeButtons = container.querySelectorAll('.accordion-close-button')
59
60
  closeButtons.forEach(button => button.click())
@@ -1,17 +1,12 @@
1
- <!-- BREAKING CHANGES TO CALLOUT -->
2
- <!-- Rename CalloutPrefix to CalloutHeader -->
3
-
4
1
  <script>
5
2
  import { toTitle } from '@splendidlabz/utils'
6
3
 
7
- let klass = ''
8
- export { klass as class }
9
- export let type = 'note'
4
+ const { class: klass = '', type = 'note', children, ...rest } = $props()
10
5
  </script>
11
6
 
12
- <div class="callout callout-{type} ${klass}" data-type={type}>
7
+ <div {...rest} class="callout callout-{type} {klass}" data-type={type}>
13
8
  <div class="callout-header">{toTitle(type)}</div>
14
9
  <div class="callout-content prose">
15
- <slot />
10
+ {@render children?.()}
16
11
  </div>
17
12
  </div>
@@ -1,66 +1,108 @@
1
1
  <script>
2
+ /**
3
+ * @component ContentList
4
+ * A dynamic table of contents component that tracks and highlights the current heading
5
+ * based on scroll position. Automatically builds a hierarchical structure from headings
6
+ * in the document or a specified container.
7
+ *
8
+ * @param {string} [selector] - CSS selector for container to search for headings. If not provided, searches entire document
9
+ * @param {string} [label] - Label text to display above the content list
10
+ * @param {string} [class] - Additional CSS classes for the component wrapper
11
+ * @param {string} [labelClass] - CSS classes for the label element
12
+ * @param {string} [listClass] - CSS classes for the list element
13
+ * @param {string} [liClass] - CSS classes for list item elements
14
+ * @param {string} [aClass] - CSS classes for anchor elements
15
+ * @param {Object} [options] - Configuration options for heading selection
16
+ * @param {string[]} [options.includes=['h2','h3','h4','h5','h6']] - Heading tags to include
17
+ * @param {string[]} [options.excludes=[]] - CSS selectors to exclude from the list
18
+ * @param {...*} [rest] - Additional props passed to the wrapper div
19
+ *
20
+ * @example
21
+ * <ContentList
22
+ * label="On this page"
23
+ * selector=".content"
24
+ * options={{ includes: ['h2', 'h3'] }}
25
+ * />
26
+ */
27
+ import {
28
+ findScrollContainer,
29
+ rlh,
30
+ scrollObserver,
31
+ } from '@splendidlabz/utils/dom'
2
32
  import { onMount } from 'svelte'
3
33
  import NestedList from '../List/NestedListItem.svelte'
34
+ import { buildStructure, updateAriaCurrent } from './contentlist.js'
4
35
 
5
- let klass = ''
6
- export { klass as class }
7
- export let selector = null
8
- export let label = 'Contents'
9
- export let labelClass = ''
10
- export let listClass = null
11
- export let liClass = null
12
- export let aClass = null
36
+ const {
37
+ selector,
38
+ label,
39
+ class: klass = '',
40
+ labelClass,
41
+ listClass,
42
+ liClass,
43
+ aClass,
44
+ options,
45
+ ...rest
46
+ } = $props()
13
47
 
14
- export let options = {
48
+ const defaultOptions = {
15
49
  includes: ['h2', 'h3', 'h4', 'h5', 'h6'],
16
50
  excludes: [],
17
51
  }
18
52
 
19
- let structure = []
53
+ const opts = { ...defaultOptions, ...options, ...rest }
54
+
55
+ let ref = $state(null)
56
+ let elements = $state([])
57
+ let structure = $state([])
58
+ let currentHeading = $state(null)
20
59
 
21
60
  onMount(() => {
22
61
  if (typeof window === 'undefined') return
23
62
  const targetElement = selector ? document.querySelector(selector) : document
24
- const elements = Array.from(
25
- targetElement.querySelectorAll(options.includes.join(',')),
63
+
64
+ elements = Array.from(
65
+ targetElement.querySelectorAll(opts.includes.join(',')),
26
66
  ).filter(el => {
27
67
  if (el.matches('.contentlist-exclude')) return false
28
- if (options.excludes.some(selector => el.matches(selector))) return false
68
+ if (opts.excludes.some(selector => el.matches(selector))) return false
29
69
  return true
30
70
  })
31
71
 
32
- structure = buildStructure(elements)
72
+ structure = buildStructure(elements, liClass, aClass)
33
73
  })
34
74
 
35
- function buildStructure(elements) {
36
- const structure = []
37
- const stack = [{ headingLevel: 0, children: structure }]
38
-
39
- elements.forEach(element => {
40
- const headingLevel = parseInt(element.tagName.charAt(1))
41
- const item = {
42
- href: `#${element.id}`,
43
- label: element.textContent,
44
- liClass,
45
- aClass,
46
- children: [],
47
- }
75
+ $effect(_ => {
76
+ if (!ref) return
77
+ const scrollContainer = findScrollContainer(ref)
48
78
 
49
- while (headingLevel <= stack[stack.length - 1].headingLevel) {
50
- stack.pop()
51
- }
79
+ const observer = scrollObserver(scrollContainer, {
80
+ callback: data => {
81
+ const { scrollTop } = data
82
+ const offset = rlh() + 10 // 10px leeway for any rounding errors + some headroom
52
83
 
53
- stack[stack.length - 1].children.push(item)
54
- stack.push({ headingLevel, children: item.children })
84
+ const closestHeading = [...elements]
85
+ .reverse()
86
+ .find(h => h.offsetTop <= scrollTop + offset)
87
+ // Update current heading and structure when closest heading changes
88
+ if (closestHeading && closestHeading !== currentHeading) {
89
+ currentHeading = closestHeading
90
+ structure = updateAriaCurrent(structure, `#${closestHeading.id}`)
91
+ }
92
+ },
55
93
  })
56
94
 
57
- return structure
58
- }
95
+ return () => observer.destroy()
96
+ })
59
97
  </script>
60
98
 
61
99
  {#if structure.length}
62
- <div class="contentlist {klass}" {...$$restProps}>
63
- <div class="label {labelClass}">{label}</div>
100
+ <div bind:this={ref} {...rest} class="contentlist {klass}">
101
+ {#if typeof label === 'string'}
102
+ <div class="label {labelClass}">{label}</div>
103
+ {:else}
104
+ {@render label?.()}
105
+ {/if}
64
106
  <ul class={listClass}>
65
107
  <NestedList items={structure} />
66
108
  </ul>
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Builds a hierarchical structure from a flat list of heading elements
3
+ * @param {HTMLElement[]} elements - Array of heading elements to structure
4
+ * @param {string} liClass - CSS classes for list items
5
+ * @param {string} aClass - CSS classes for anchors
6
+ * @return {Object[]} Hierarchical array of items with nested children
7
+ * @property {string} return[].href - Anchor link to the heading
8
+ * @property {string} return[].label - Text content of the heading
9
+ * @property {string} return[].liClass - CSS classes for the list item
10
+ * @property {string} return[].aClass - CSS classes for the anchor
11
+ * @property {Object[]} return[].children - Nested child items
12
+ */
13
+
14
+ export function buildStructure(elements, liClass, aClass) {
15
+ const structure = []
16
+ const stack = [{ headingLevel: 0, children: structure }]
17
+
18
+ elements.forEach(element => {
19
+ const headingLevel = parseInt(element.tagName.charAt(1))
20
+ const item = {
21
+ href: `#${element.id}`,
22
+ label: element.textContent,
23
+ liClass,
24
+ aClass,
25
+ children: [],
26
+ }
27
+
28
+ while (headingLevel <= stack[stack.length - 1].headingLevel) {
29
+ stack.pop()
30
+ }
31
+
32
+ stack[stack.length - 1].children.push(item)
33
+ stack.push({ headingLevel, children: item.children })
34
+ })
35
+
36
+ return structure
37
+ }
38
+
39
+ /**
40
+ * Recursively updates aria-current attribute for items matching the target href
41
+ * @param {Object[]} items - Array of content list items to process
42
+ * @param {string} targetHref - Target href to mark as current location
43
+ * @return {Object[]} Updated array with aria-current attributes set
44
+ * @property {string|null} return[].aria-current - 'location' if item matches targetHref, null otherwise
45
+ */
46
+ export function updateAriaCurrent(items, targetHref) {
47
+ return items.map(item => {
48
+ const newItem = { ...item }
49
+ if (item.href === targetHref) {
50
+ newItem['aria-current'] = 'location'
51
+ } else {
52
+ newItem['aria-current'] = null
53
+ }
54
+ if (item.children && item.children.length > 0) {
55
+ newItem.children = updateAriaCurrent(item.children, targetHref)
56
+ }
57
+ return newItem
58
+ })
59
+ }
@@ -1,7 +1,9 @@
1
1
  <script>
2
2
  import { getEntry } from '../../../actions/sync.svelte.js'
3
- import ModalClose from '../Modal/ModalClose.svelte'
4
- import PopoverClose from '../Popover/PopoverClose.svelte'
3
+ import ModalClose from '../Modal/ModalCloseButton.svelte'
4
+ import PopoverClose from '../Popover/PopoverCloseButton.svelte'
5
+
6
+ // TODO: Disclosure close button!
5
7
 
6
8
  const { class: klass = '', target, ...p } = $props()
7
9
 
@@ -2,9 +2,4 @@ import DrawerCloseButton from './DrawerCloseButton.svelte'
2
2
  import DrawerContent from './DrawerContent.svelte'
3
3
  import DrawerTrigger from './DrawerTrigger.svelte'
4
4
 
5
- export {
6
- DrawerContent as Drawer,
7
- DrawerCloseButton,
8
- DrawerContent,
9
- DrawerTrigger,
10
- }
5
+ export { DrawerCloseButton, DrawerContent, DrawerTrigger }
@@ -1,5 +1,5 @@
1
- import ModalClose from './ModalClose.svelte'
1
+ import ModalCloseButton from './ModalCloseButton.svelte'
2
2
  import ModalContent from './ModalContent.svelte'
3
3
  import ModalTrigger from './ModalTrigger.svelte'
4
4
 
5
- export { ModalContent as Modal, ModalClose, ModalContent, ModalTrigger }
5
+ export { ModalCloseButton, ModalContent, ModalTrigger }
@@ -1,6 +1,5 @@
1
- import Popover from './Popover.svelte'
2
- import PopoverClose from './PopoverClose.svelte'
1
+ import PopoverCloseButton from './PopoverCloseButton.svelte'
3
2
  import PopoverContent from './PopoverContent.svelte'
4
3
  import PopoverTrigger from './PopoverTrigger.svelte'
5
4
 
6
- export { Popover, PopoverClose, PopoverContent, PopoverTrigger }
5
+ export { PopoverCloseButton, PopoverContent, PopoverTrigger }
@@ -0,0 +1,6 @@
1
+ <script>
2
+ import { TextInput } from './TextInput.svelte'
3
+ const { ...rest } = $props()
4
+ </script>
5
+
6
+ <TextInput {...rest} type="search" />
@@ -1,6 +1,7 @@
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'
4
5
 
5
6
  let { value = $bindable(''), ref = $bindable(null), ...p } = $props()
6
7
  const {
@@ -79,14 +80,18 @@
79
80
  aria-label={!labelVisible && label}
80
81
  >
81
82
  {#if label && labelVisible}
82
- <label class="label input-meta {labelClass}" for={id}>{label}</label>
83
+ <label class="label input-meta {labelClass}" for={id}>
84
+ <Markdown content={label} inline />
85
+ </label>
83
86
  {/if}
84
87
  {#if hint}
85
- <div id={hintId} class="hint input-meta {hintClass}">{hint}</div>
88
+ <div id={hintId} class="hint input-meta {hintClass}">
89
+ <Markdown content={hint} inline />
90
+ </div>
86
91
  {/if}
87
92
  {#if error}
88
93
  <output id={errorId} class="error input-meta {errorClass}" for={id}>
89
- {error}
94
+ <Markdown content={error} inline />
90
95
  </output>
91
96
  {/if}
92
97
 
@@ -19,32 +19,50 @@ Props:
19
19
  - children (Object[]): Optional array of nested items to be rendered as a sublist.
20
20
  -->
21
21
  <script>
22
- // let klass = ''
23
- // export { klass as class }
24
- export let items = []
25
- export let liClass = ''
26
- export let aClass = ''
27
- </script>
22
+ const { items = [], liClass = '', aClass = '' } = $props()
28
23
 
29
- <!-- {@html renderList(items)} -->
24
+ function parseItemProps(item) {
25
+ const {
26
+ label,
27
+ children,
28
+ liClass: ilClass,
29
+ aClass: iaClass,
30
+ href,
31
+ 'aria-current': ariaCurrent,
32
+ ...irest
33
+ } = item
30
34
 
31
- <!-- Svelte version below is not ideal. -->
32
- <!-- Limitation: We cannot put the UL element here because we are using svelte:self to reference the entire element. -->
33
- <!-- This can be fixed with Svelte 5 Snippets. -->
35
+ return {
36
+ liProps: {
37
+ class: ilClass || liClass,
38
+ ...irest,
39
+ },
40
+ aProps: {
41
+ class: iaClass || aClass,
42
+ href,
43
+ 'aria-current': ariaCurrent,
44
+ },
45
+ }
46
+ }
47
+ </script>
34
48
 
35
- {#if items.length}
36
- {#each items as item}
37
- <li class={item.liClass || liClass}>
38
- <a class={item.aClass || aClass} href={item.href}>{item.label}</a>
49
+ {#snippet nestedItems(items)}
50
+ {#if items.length}
51
+ {#each items as item (item.label)}
52
+ {@const { liProps, aProps } = parseItemProps(item)}
53
+ <li {...liProps}>
54
+ <a {...aProps}>
55
+ {item.label}
56
+ </a>
39
57
 
40
- {#if item.children.length}
41
- <ul>
42
- <svelte:self {liClass} {aClass} items={item.children} />
43
- </ul>
44
- {/if}
45
- </li>
46
- {/each}
47
- {/if}
58
+ {#if item.children.length}
59
+ <ul>
60
+ {@render nestedItems(item.children)}
61
+ </ul>
62
+ {/if}
63
+ </li>
64
+ {/each}
65
+ {/if}
66
+ {/snippet}
48
67
 
49
- <!-- What about items that are active according to the TARGET?! -->
50
- <!-- Hm. How to style? CSS directly? -->
68
+ {@render nestedItems(items)}
@@ -1,22 +1,15 @@
1
1
  <script>
2
- let { status, ...p } = $props()
2
+ let { status = $bindable('info'), ...p } = $props()
3
3
 
4
4
  const { class: klass = '', children, ...rest } = p
5
5
 
6
- if (typeof status === 'number') {
7
- switch (true) {
8
- case status >= 200 && status < 300:
9
- status = 'success'
10
- break
11
- case status >= 300 && status < 400:
12
- status = 'redirect'
13
- break
14
- case status >= 400:
15
- status = 'error'
16
- break
17
- default:
18
- status = 'info'
19
- }
6
+ function getStatusString(status) {
7
+ console.log('status', status)
8
+ if (typeof status === 'string') return status
9
+ if (status >= 200 && status < 300) return 'success'
10
+ if (status >= 300 && status < 400) return 'redirect'
11
+ if (status >= 400) return 'error'
12
+ return 'info'
20
13
  }
21
14
 
22
15
  const statusClasses = {
@@ -24,7 +17,9 @@
24
17
  success: 'status-success',
25
18
  error: 'status-error',
26
19
  }
27
- const statusClass = statusClasses[status]
20
+
21
+ let statusClass = $derived.by(_ => statusClasses[getStatusString(status)])
22
+ $inspect(statusClass)
28
23
  </script>
29
24
 
30
25
  <div {...rest} class="status {statusClass} {klass}">
@@ -1,4 +1,3 @@
1
- // import { all as layoutScripts } from '@splendidlabz/styles/scripts'
2
1
  import { omitEmpty } from '@splendidlabz/utils'
3
2
  import {
4
3
  Focusable,
@@ -22,7 +21,6 @@ export const DEFAULT_TABLIST_OPTIONS = {
22
21
  loop: false,
23
22
  autoplay: false,
24
23
  autoplayInterval: 3000,
25
- // runLayoutScripts: false,
26
24
  },
27
25
  }
28
26
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splendidlabz/svelte",
3
3
  "type": "module",
4
- "version": "4.5.0",
4
+ "version": "4.6.0",
5
5
  "sideEffects": false,
6
6
  "scripts": {
7
7
  "lint": "eslint . --fix",
@@ -21,8 +21,8 @@
21
21
  "dependencies": {
22
22
  "@floating-ui/dom": "^1.6.13",
23
23
  "@iconify/svelte": "^4.2.0",
24
- "@splendidlabz/styles": "4.3.1",
25
- "@splendidlabz/utils": "1.9.0",
24
+ "@splendidlabz/styles": "4.4.0",
25
+ "@splendidlabz/utils": "1.10.0",
26
26
  "autosuggest-highlight": "^3.3.4",
27
27
  "lodash-es": "^4.17.21",
28
28
  "node-html-parser": "^7.0.1"
@@ -1,117 +0,0 @@
1
- <script>
2
- import { onMount } from 'svelte'
3
- export let label = 'Contents'
4
- export let labelClass = ''
5
- export let headings = ['h2', 'h3', 'h4', 'h5', 'h6']
6
- export let selector = null
7
-
8
- let structure = []
9
-
10
- // Search the DOM so we have to do onMount
11
- onMount(async _ => {
12
- selector = document.querySelector(selector) || document
13
-
14
- const elements = Array.from(selector.querySelectorAll(headings))
15
-
16
- elements.forEach(element => {
17
- const heading = element.tagName.toLowerCase()
18
- const id = element.id
19
- const label = element.textContent
20
-
21
- if (heading === 'h1' || heading === 'h2') {
22
- structure.push({ id, label, children: [] })
23
- }
24
-
25
- if (heading === 'h3') {
26
- const last = structure[structure.length - 1]
27
- last.children.push({ id, label, children: [] })
28
- }
29
-
30
- if (heading === 'h4') {
31
- const last = structure[structure.length - 1]
32
- const lastChild = last.children[last.children.length - 1]
33
- lastChild.children.push({ id, label, children: [] })
34
- }
35
-
36
- if (heading === 'h5') {
37
- const last = structure[structure.length - 1]
38
- const lastChild = last.children[last.children.length - 1]
39
- const lastGrandChild = lastChild.children[lastChild.children.length - 1]
40
- lastGrandChild.children.push({ id, label, children: [] })
41
- }
42
-
43
- if (heading === 'h6') {
44
- const last = structure[structure.length - 1]
45
- const lastChild = last.children[last.children.length - 1]
46
- const lastGrandChild = lastChild.children[lastChild.children.length - 1]
47
- const lastGreatGrandChild =
48
- lastGrandChild.children[lastGrandChild.children.length - 1]
49
- lastGreatGrandChild.children.push({ id, label })
50
- }
51
- })
52
-
53
- // Reassign to trigger update
54
- structure = structure
55
- })
56
- </script>
57
-
58
- {#if structure.length}
59
- <div class="ContentList" {...$$restProps}>
60
- <div class="label {labelClass}">{label}</div>
61
-
62
- <!-- H1 and H2 -->
63
- <ul>
64
- {#each structure as level1}
65
- <li class="level-1">
66
- <a href="#{level1.id}">{level1.label}</a>
67
-
68
- <!-- H3 -->
69
- {#if level1.children}
70
- <ul>
71
- {#each level1.children as level2}
72
- <li class="level-2">
73
- <a href="#{level2.id}">{level2.label}</a>
74
-
75
- <!-- H4 -->
76
- {#if level2.children}
77
- <ul>
78
- {#each level2.children as level3}
79
- <li class="level-3">
80
- <a href="#{level3.id}">{level3.label}</a>
81
-
82
- <!-- H5 -->
83
- {#if level3.children}
84
- <ul>
85
- {#each level3.children as level4}
86
- <li class="level-4">
87
- <a href="#{level4.id}">{level4.label}</a>
88
-
89
- <!-- H6 -->
90
- {#if level4.children}
91
- <ul>
92
- {#each level4.children as level5}
93
- <li class="level-5">
94
- <a href="#{level5.id}"
95
- >{level5.label}</a
96
- >
97
- </li>
98
- {/each}
99
- </ul>
100
- {/if}
101
- </li>
102
- {/each}
103
- </ul>
104
- {/if}
105
- </li>
106
- {/each}
107
- </ul>
108
- {/if}
109
- </li>
110
- {/each}
111
- </ul>
112
- {/if}
113
- </li>
114
- {/each}
115
- </ul>
116
- </div>
117
- {/if}