@splendidlabz/svelte 4.6.13 → 4.7.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @splendidlabz/svelte
2
2
 
3
+ ## 4.7.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Improve browserframe code abit
8
+
9
+ ## 4.7.0
10
+
11
+ ### Minor Changes
12
+
13
+ - Update Tablist to use Svelte 5 syntax
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies
18
+ - @splendidlabz/utils@1.11.0
19
+
3
20
  ## 4.6.13
4
21
 
5
22
  ### Patch Changes
@@ -27,11 +27,12 @@
27
27
  background,
28
28
  } = p
29
29
 
30
- // svelte-ignore non_reactive_update
31
30
  let { tablistItems = [] } = p
32
- const tabs = tablistItems.length > 0
33
- if (tablistItems.length > 0) {
34
- tablistItems = tablistItems.map(tab => {
31
+ let items = $state(tablistItems || [])
32
+
33
+ const hasTabs = tablistItems.length > 0
34
+ if (hasTabs) {
35
+ items = tablistItems.map(tab => {
35
36
  if (typeof tab === 'string') return { text: tab }
36
37
  return tab
37
38
  })
@@ -45,7 +46,7 @@
45
46
  class="tablist-tab tablist-top {tablistClass}"
46
47
  target={tablistTarget}
47
48
  >
48
- {#each tablistItems as tab, index (tab)}
49
+ {#each items as tab, index (tab)}
49
50
  {@const { text, ...tabRest } = tab}
50
51
  <Tab {...tabRest} active={index === selectedTabIndex}>
51
52
  {text}
@@ -76,7 +77,7 @@
76
77
  {@render dot()}
77
78
  {@render dot()}
78
79
  </div>
79
- {#if tabs}
80
+ {#if hasTabs}
80
81
  {@render tablist()}
81
82
  {/if}
82
83
  </div>
@@ -88,7 +89,7 @@
88
89
  {/if}
89
90
 
90
91
  <div class="content vertical {contentClass}">
91
- {#if tabs}
92
+ {#if hasTabs}
92
93
  {@render tabpanels()}
93
94
  {:else if children}{@render children()}{/if}
94
95
  </div>
@@ -1,32 +1,31 @@
1
1
  <script>
2
2
  import { preferHorizontalScroll as preferHorizontalScrollAction } from '@splendidlabz/utils/dom'
3
3
  import { breakpointObserver } from '../../actions/breakpoint-observer.js'
4
- import { tabs } from './tab-store.js'
5
- import { tablist, updatePlacement } from './tabs.js'
4
+ import { mergeOpts } from '../../utils/merge-opts.js'
5
+ import { getTab, tablist, updatePlacement } from './tabs.svelte.js'
6
6
 
7
- export let klass = ''
8
- export { klass as class }
9
-
10
- export let target = null
11
- export let options = {}
12
- export let style = ''
7
+ let { ...p } = $props()
8
+ p = mergeOpts(p)
13
9
 
14
10
  const {
11
+ class: klass = '',
12
+ target = null,
15
13
  breakpoints,
16
14
  breakpointType,
17
15
  placement: initialPlacement,
18
16
  scrollable = true,
19
17
  preferHorizontalScroll = true,
20
18
  preserveOutlines = true,
19
+
20
+ // slots
21
+ children,
22
+
21
23
  ...rest
22
- } = options
24
+ } = p
23
25
 
24
26
  let placement = initialPlacement || 'top'
25
-
26
- let tab
27
- let tablistClasses
28
- $: tab = $tabs.find(t => t.id === target)
29
- $: tablistClasses = getTablistClasses(tab)
27
+ let tab = $derived(getTab(target))
28
+ let tablistClasses = $derived(getTablistClasses(tab))
30
29
 
31
30
  if (!target) throw new Error('Tablist must have a target')
32
31
 
@@ -59,12 +58,10 @@
59
58
  </script>
60
59
 
61
60
  <div
62
- {style}
61
+ {...rest}
63
62
  class="tablist {tablistClasses} {klass}"
64
63
  role="tablist"
65
64
  {target}
66
- {...$$restProps}
67
- on:tab-change
68
65
  use:breakpointObserver={{
69
66
  breakpoints,
70
67
  breakpointType,
@@ -77,5 +74,5 @@
77
74
  }}
78
75
  >
79
76
  <!-- Need to add callback to breakpoint opts -->
80
- <slot />
77
+ {@render children?.()}
81
78
  </div>
@@ -1,5 +1,5 @@
1
1
  <script>
2
- import { tabpanels } from './tabs.js'
2
+ import { tabpanels } from './tabs.svelte.js'
3
3
 
4
4
  const { id = null, class: klass = '', children, ...rest } = $props()
5
5
  if (!id) throw new Error('Tabpanels must have an id')
@@ -0,0 +1,382 @@
1
+ import { isFunction, omitEmpty } from '@splendidlabz/utils'
2
+ import {
3
+ Focusable,
4
+ addListeners,
5
+ getSelfIndex,
6
+ isArrowKey,
7
+ removeListeners,
8
+ } from '@splendidlabz/utils/dom'
9
+
10
+ // ========================
11
+ // Shared State
12
+ // ========================
13
+ const tabs = $state([])
14
+
15
+ export function getTab(id) {
16
+ return tabs.find(t => t.id === id)
17
+ }
18
+
19
+ export function initTab({ id, state = {}, nodes = {}, options = {} }) {
20
+ const tab = getTab(id)
21
+ if (!tab) {
22
+ tabs.push({ id, state, nodes, options })
23
+ } else {
24
+ tab.state = { ...tab.state, ...omitEmpty(state) }
25
+ tab.nodes = { ...tab.nodes, ...omitEmpty(nodes, { shallow: true }) }
26
+ tab.options = { ...tab.options, ...omitEmpty(options) }
27
+ }
28
+
29
+ return getTab(id)
30
+ }
31
+
32
+ // ========================
33
+ // Default Values
34
+ // ========================
35
+ export const DEFAULT_TABLIST_OPTIONS = {
36
+ state: {
37
+ placement: 'top', // top, left, right
38
+ },
39
+ options: {
40
+ useHash: false,
41
+ scrollIntoView: false,
42
+ scrollIntoViewOnLoad: true,
43
+ scrollBehaviour: 'smooth', // smooth, auto
44
+ loop: false,
45
+ autoplay: false,
46
+ autoplayInterval: 3000,
47
+ onTabChange: null,
48
+ },
49
+ }
50
+
51
+ export const TABLIST_OPTION_KEYS = Object.keys({
52
+ ...DEFAULT_TABLIST_OPTIONS.state,
53
+ ...DEFAULT_TABLIST_OPTIONS.options,
54
+ })
55
+
56
+ export function tablist(node, props) {
57
+ const targetId = node.getAttribute('target')
58
+
59
+ // The filter allows us to nest tabs in tabs
60
+ const tablistItems = Array.from(node.querySelectorAll('[role="tab"]')).filter(
61
+ tab => tab.closest('.tablist') === node,
62
+ )
63
+ let activeTabIndex
64
+ activeTabIndex = tablistItems.findIndex(tab =>
65
+ tab.hasAttribute('aria-selected'),
66
+ )
67
+ if (activeTabIndex === -1) activeTabIndex = 0
68
+
69
+ const tabInfo = {
70
+ id: targetId,
71
+ state: {
72
+ ...DEFAULT_TABLIST_OPTIONS.state,
73
+ placement: props.state.placement,
74
+ activeTabIndex,
75
+ length: tablistItems.length,
76
+ },
77
+ nodes: { tablist: node, tablistItems },
78
+ options: {
79
+ ...DEFAULT_TABLIST_OPTIONS.options,
80
+ ...omitEmpty(props.options),
81
+ },
82
+ }
83
+
84
+ const Tab = initTab(tabInfo)
85
+
86
+ setInitialState(Tab)
87
+
88
+ // Add listeners
89
+ const listeners = [
90
+ {
91
+ event: 'click',
92
+ node,
93
+ handler: handleClick,
94
+ },
95
+ {
96
+ event: 'keydown',
97
+ node,
98
+ handler: handleKeydown,
99
+ },
100
+ ]
101
+
102
+ addListeners(listeners)
103
+
104
+ const { autoplay } = Tab.options
105
+ if (autoplay) setAutoplayInterval(Tab)
106
+
107
+ return {
108
+ destroy() {
109
+ removeListeners(listeners)
110
+ },
111
+ }
112
+
113
+ function handleClick(event) {
114
+ const tab = event.target.closest('[role="tab"]')
115
+ if (!tab) return
116
+ const { tablistItems } = Tab.nodes
117
+
118
+ const index = tablistItems.findIndex(t => t === tab)
119
+ selectTab(Tab, index)
120
+ }
121
+
122
+ function handleKeydown(event) {
123
+ const { placement } = Tab.state
124
+ const { key } = event
125
+ if (isArrowKey(key)) {
126
+ event.preventDefault() // prevents scrolling
127
+
128
+ if (placement === 'top') {
129
+ if (key === 'ArrowLeft') selectPrevTab(Tab)
130
+ if (key === 'ArrowRight') selectNextTab(Tab)
131
+ if (key === 'ArrowDown') focusOnTabpanel(Tab)
132
+ }
133
+
134
+ if (placement === 'left') {
135
+ if (key === 'ArrowUp') selectPrevTab(Tab)
136
+ if (key === 'ArrowDown') selectNextTab(Tab)
137
+ if (key === 'ArrowRight') focusOnTabpanel(Tab)
138
+ }
139
+
140
+ if (placement === 'right') {
141
+ if (key === 'ArrowUp') selectPrevTab(Tab)
142
+ if (key === 'ArrowDown') selectNextTab(Tab)
143
+ if (key === 'ArrowLeft') focusOnTabpanel(Tab)
144
+ }
145
+ }
146
+ }
147
+ }
148
+
149
+ export function tabpanels(node, props) {
150
+ const tabpanelsId = node.id
151
+
152
+ // The filter allows us to nest tabs in tabs
153
+ const tabpanelItems = Array.from(
154
+ node.querySelectorAll('[role="tabpanel"]'),
155
+ ).filter(tab => tab.closest('.tabpanels') === node)
156
+
157
+ let activeTabIndex
158
+ activeTabIndex = tabpanelItems.findIndex(
159
+ tabpanel => !tabpanel.hasAttribute('hidden'),
160
+ )
161
+ if (activeTabIndex === -1) activeTabIndex = 0
162
+
163
+ // Initialize the Tab
164
+ const Tab = initTab({
165
+ id: tabpanelsId,
166
+ state: { activeTabIndex, length: tabpanelItems.length },
167
+ nodes: { tabpanels: node, tabpanelItems },
168
+ })
169
+
170
+ setInitialState(Tab)
171
+
172
+ // Add listeners
173
+ const listeners = [
174
+ {
175
+ event: 'keydown',
176
+ node,
177
+ handler: handleKeydown,
178
+ },
179
+ ]
180
+
181
+ addListeners(listeners)
182
+
183
+ return {
184
+ destroy() {
185
+ removeListeners(listeners)
186
+ },
187
+ }
188
+
189
+ function handleKeydown(event) {
190
+ if (!event.target.matches('.tabpanel')) return
191
+
192
+ if (event.target.closest('.tabpanels') !== node) return
193
+
194
+ const { placement } = Tab.state
195
+ const { key } = event
196
+
197
+ if (isArrowKey(key)) {
198
+ event.preventDefault() // prevents scrolling
199
+ event.stopPropagation() // Prevents nested tabpanel from focusing on external tabpanel
200
+
201
+ if (placement === 'top' && key === 'ArrowUp') focusOnTab(Tab)
202
+ if (placement === 'left' && key === 'ArrowLeft') focusOnTab(Tab)
203
+ if (placement === 'right' && key === 'ArrowRight') focusOnTab(Tab)
204
+
205
+ // The following would be to focus on the first content item.
206
+ // Not necessary, but would be nice and seems like a good idea.
207
+ if (key === 'ArrowDown') focusOnTabpanelContent(event.target)
208
+ }
209
+ }
210
+ }
211
+
212
+ function setInitialState(Tab) {
213
+ // Wait until component is fully initialized before proceeding.
214
+ const { tablist, tabpanels } = Tab.nodes
215
+ if (!(tablist && tabpanels)) return
216
+
217
+ const { hash } = window.location
218
+
219
+ // Standard initialization
220
+ if (!hash) {
221
+ selectTab(Tab, Tab.state.activeTabIndex, { shouldFocusOnTab: false })
222
+ return
223
+ }
224
+
225
+ // Hash-based initiation
226
+ // Activates tab and tabpanel if hashed ID is within the tabpanel.
227
+ const element = document.querySelector(hash)
228
+ const inTabpanel = element?.closest('.tabpanels') === tabpanels
229
+ if (!inTabpanel) return
230
+
231
+ const closestTabpanel = element.closest('.tabpanel')
232
+ const index = getSelfIndex(closestTabpanel)
233
+ selectTab(Tab, index, { shouldUpdateHash: false, shouldScroll: false })
234
+
235
+ // Scroll to content
236
+ if (element !== closestTabpanel)
237
+ element.scrollIntoView({ behavior: Tab.options.scrollBehaviour })
238
+ else {
239
+ // Scrolls to Tablist
240
+ if (Tab.options.scrollIntoViewOnLoad) scrollIntoView(Tab)
241
+ else scrollIntoView(Tab, 'instant')
242
+ }
243
+ }
244
+
245
+ export function selectTab(
246
+ Tab,
247
+ index,
248
+ {
249
+ shouldFocusOnTab = true,
250
+ shouldUpdateHash = true,
251
+ shouldScroll = true,
252
+ } = {},
253
+ ) {
254
+ Tab.state.activeTabIndex = index
255
+
256
+ setActiveTab(Tab)
257
+ setActivePanel(Tab)
258
+ if (shouldFocusOnTab) focusOnTab(Tab)
259
+
260
+ const activeTab = Tab.nodes.tablistItems[index]
261
+ const activePanel = Tab.nodes.tabpanelItems[index]
262
+ const { onTabChange } = Tab.options
263
+
264
+ if (isFunction(onTabChange)) {
265
+ onTabChange({ Tab, activeTab, activePanel })
266
+ }
267
+
268
+ // TODO: Does this break if we don't include splendidLabz/layout in the build?
269
+ // If it does, we need to import layout scripts with await import.
270
+ // Then we'll have to turn `selectTab` into an async function and manage the rest accordingly
271
+ // if (Tab.options.runLayoutScripts) {
272
+ // setTimeout(_ => layoutScripts(activePanel), 0)
273
+ // }
274
+
275
+ const { useHash, autoplay } = Tab.options
276
+
277
+ // Update the URL hash url if link to tab is active
278
+ if (useHash && shouldUpdateHash) {
279
+ const activeTabpanelId = Tab.nodes.tabpanelItems[index].id
280
+ history.pushState(null, null, `#${activeTabpanelId}`)
281
+ if (shouldScroll && Tab.options.scrollIntoView) scrollIntoView(Tab)
282
+ }
283
+
284
+ // Reset progression interval ID
285
+ if (autoplay) {
286
+ clearInterval(Tab.state.autoplayID)
287
+ setAutoplayInterval(Tab)
288
+ }
289
+ }
290
+
291
+ function scrollIntoView(Tab, behaviour) {
292
+ const { scrollBehaviour } = Tab.options
293
+ Tab.nodes.tablist.scrollIntoView({
294
+ behavior: behaviour || scrollBehaviour,
295
+ })
296
+ }
297
+
298
+ export function selectNextTab(Tab) {
299
+ const { activeTabIndex, length } = Tab.state
300
+ const { loop } = Tab.options
301
+
302
+ let nextIndex = activeTabIndex + 1
303
+ if (nextIndex >= length) {
304
+ if (loop) nextIndex = 0
305
+ else return
306
+ }
307
+
308
+ selectTab(Tab, nextIndex)
309
+ }
310
+ export function selectPrevTab(Tab) {
311
+ const { activeTabIndex, length } = Tab.state
312
+ const { loop } = Tab.options
313
+
314
+ let prevIndex = activeTabIndex - 1
315
+ if (prevIndex < 0) {
316
+ if (loop) prevIndex = length - 1
317
+ else return
318
+ }
319
+
320
+ selectTab(Tab, prevIndex)
321
+ }
322
+
323
+ export function focusOnTab(Tab) {
324
+ const { activeTabIndex } = Tab.state
325
+ const { tablistItems } = Tab.nodes
326
+ tablistItems[activeTabIndex].focus()
327
+ }
328
+
329
+ export function focusOnTabpanel(Tab) {
330
+ const { activeTabIndex } = Tab.state
331
+ const { tabpanelItems } = Tab.nodes
332
+ tabpanelItems[activeTabIndex].focus()
333
+ }
334
+
335
+ export function focusOnTabpanelContent(Tabpanel) {
336
+ const focusables = Focusable(Tabpanel)
337
+ if (focusables.length) focusables.first.focus()
338
+ }
339
+
340
+ function setActiveTab(Tab) {
341
+ const { activeTabIndex } = Tab.state
342
+ const { tablistItems } = Tab.nodes
343
+
344
+ tablistItems.forEach((tab, index) => {
345
+ const isActiveTab = index === activeTabIndex
346
+ if (isActiveTab) {
347
+ tab.setAttribute('aria-selected', 'true')
348
+ tab.setAttribute('tabindex', '0')
349
+ } else {
350
+ tab.removeAttribute('aria-selected')
351
+ tab.setAttribute('tabindex', '-1')
352
+ }
353
+ })
354
+ }
355
+
356
+ function setActivePanel(Tab) {
357
+ const { activeTabIndex } = Tab.state
358
+ const { tabpanelItems } = Tab.nodes
359
+
360
+ tabpanelItems.forEach((panel, index) => {
361
+ const isActiveTab = index === activeTabIndex
362
+ if (isActiveTab) {
363
+ panel.removeAttribute('hidden')
364
+ panel.setAttribute('tabindex', '0')
365
+ } else {
366
+ panel.setAttribute('hidden', 'true')
367
+ panel.removeAttribute('tabindex')
368
+ }
369
+ })
370
+ }
371
+
372
+ export function updatePlacement(Tab, placement) {
373
+ Tab.state.placement = placement
374
+ }
375
+
376
+ function setAutoplayInterval(Tab) {
377
+ const { autoplayInterval } = Tab.options
378
+ const intervalID = setInterval(() => {
379
+ selectNextTab(Tab)
380
+ }, autoplayInterval)
381
+ Tab.state.autoplayID = intervalID
382
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splendidlabz/svelte",
3
3
  "type": "module",
4
- "version": "4.6.13",
4
+ "version": "4.7.1",
5
5
  "sideEffects": false,
6
6
  "homepage": "https://splendidlabz.com/docs/svelte",
7
7
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  "@floating-ui/dom": "^1.6.13",
23
23
  "@iconify/svelte": "^4.2.0",
24
24
  "@splendidlabz/styles": "4.6.0",
25
- "@splendidlabz/utils": "1.10.7",
25
+ "@splendidlabz/utils": "1.11.0",
26
26
  "autosuggest-highlight": "^3.3.4",
27
27
  "lodash-es": "^4.17.21",
28
28
  "node-html-parser": "^7.0.1"