@splendidlabz/svelte 4.6.13 → 4.7.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 +11 -0
- package/components/Tabs/Tablist.svelte +20 -19
- package/components/Tabs/Tabpanels.svelte +1 -1
- package/components/Tabs/tabs.svelte.js +382 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,32 +1,35 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import { preferHorizontalScroll as preferHorizontalScrollAction } from '@splendidlabz/utils/dom'
|
|
3
3
|
import { breakpointObserver } from '../../actions/breakpoint-observer.js'
|
|
4
|
-
import {
|
|
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
|
-
|
|
8
|
-
|
|
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
|
-
} =
|
|
24
|
+
} = p
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
function onTabChange(event) {
|
|
27
|
+
console.log(event)
|
|
28
|
+
}
|
|
25
29
|
|
|
26
|
-
let
|
|
27
|
-
let
|
|
28
|
-
|
|
29
|
-
$: tablistClasses = getTablistClasses(tab)
|
|
30
|
+
let placement = initialPlacement || 'top'
|
|
31
|
+
let tab = $derived(getTab(target))
|
|
32
|
+
let tablistClasses = $derived(getTablistClasses(tab))
|
|
30
33
|
|
|
31
34
|
if (!target) throw new Error('Tablist must have a target')
|
|
32
35
|
|
|
@@ -59,12 +62,10 @@
|
|
|
59
62
|
</script>
|
|
60
63
|
|
|
61
64
|
<div
|
|
62
|
-
{
|
|
65
|
+
{...rest}
|
|
63
66
|
class="tablist {tablistClasses} {klass}"
|
|
64
67
|
role="tablist"
|
|
65
68
|
{target}
|
|
66
|
-
{...$$restProps}
|
|
67
|
-
on:tab-change
|
|
68
69
|
use:breakpointObserver={{
|
|
69
70
|
breakpoints,
|
|
70
71
|
breakpointType,
|
|
@@ -73,9 +74,9 @@
|
|
|
73
74
|
use:preferHorizontalScrollAction
|
|
74
75
|
use:tablist={{
|
|
75
76
|
state: { placement },
|
|
76
|
-
options: rest,
|
|
77
|
+
options: { ...rest, onTabChange },
|
|
77
78
|
}}
|
|
78
79
|
>
|
|
79
80
|
<!-- Need to add callback to breakpoint opts -->
|
|
80
|
-
|
|
81
|
+
{@render children?.()}
|
|
81
82
|
</div>
|
|
@@ -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.
|
|
4
|
+
"version": "4.7.0",
|
|
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.
|
|
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"
|