@splendidlabz/svelte 4.8.5 → 4.10.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 +38 -0
- package/components/Accordion/Accordion.svelte +24 -24
- package/components/Accordion/accordion-controlled.js +4 -0
- package/components/Accordion/accordion-controlled.spec.js +14 -0
- package/components/BrowserFrame/index.js +1 -2
- package/components/Markdown/Markdown.spec.js +1 -0
- package/components/Tabcordion/Tabcordion.svelte +89 -0
- package/components/Tabcordion/TabcordionItem.svelte +7 -0
- package/components/Tabcordion/index.js +4 -0
- package/components/Tabs/Tab.svelte +17 -4
- package/components/Tabs/Tablist.svelte +8 -34
- package/components/Tabs/TabsExample.svelte +67 -0
- package/components/Tabs/tabs-css.spec.js +43 -0
- package/components/Tabs/tabs.spec.js +553 -0
- package/components/Tabs/tabs.svelte.js +30 -6
- package/components/index.js +1 -0
- package/package.json +13 -11
- package/vitest.config.js +3 -0
- package/components/BrowserFrame/BrowserWithTabs.svelte +0 -37
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# @splendidlabz/svelte
|
|
2
2
|
|
|
3
|
+
## 4.10.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 13fdcd9: feat(Tabcordion): refactor to context-based API with TabcordionItem; simplify Accordion
|
|
8
|
+
|
|
9
|
+
**Tabcordion** — replaced `tabs` array prop with Svelte context + `TabcordionItem` children. CSS scoped per instance via IDs (no global `.tabs`/`.accordion-stack` bleed). New passthrough props: `accordion`, `tablist`, `tab`, `tabpanels`, `tabpanel`.
|
|
10
|
+
|
|
11
|
+
```svelte
|
|
12
|
+
<Tabcordion>
|
|
13
|
+
<TabcordionItem title="Tab 1">Content for tab 1</TabcordionItem>
|
|
14
|
+
<TabcordionItem title="Tab 2">Content for tab 2</TabcordionItem>
|
|
15
|
+
</Tabcordion>
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Accordion** — simplified: removed `closeOthers` prop and `accordion-controlled.js` dependency. `open` is now a bindable prop (default `false`). `onToggle` called via `isFunction` guard. Controlled and uncontrolled modes unified.
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- 5fdfc45: Updated `@iconify/svelte` to `5.2.1` for Svelte 5 compatibility.
|
|
23
|
+
- 58f768f: Removed `BrowserWithTabs` component. Added `title` and `titleClass` props to `Tab` for vertical tab layouts. Fixed `Tablist` class generation for non-top placements.
|
|
24
|
+
- Updated dependencies [c054af3]
|
|
25
|
+
- Updated dependencies [92d1320]
|
|
26
|
+
- Updated dependencies [60ce1ac]
|
|
27
|
+
- @splendidlabz/utils@1.11.5
|
|
28
|
+
- @splendidlabz/styles@4.8.0
|
|
29
|
+
|
|
30
|
+
## 4.9.0
|
|
31
|
+
|
|
32
|
+
### Minor Changes
|
|
33
|
+
|
|
34
|
+
- c1a3797: Replace JS breakpoint-driven tab placement with CSS-driven placement. Placement classes (tablist-top, tablist-left, tablist-right) now set a --placement CSS custom property that JS reads for keyboard navigation. Removes breakpointObserver from Tablist.
|
|
35
|
+
|
|
36
|
+
### Patch Changes
|
|
37
|
+
|
|
38
|
+
- Updated dependencies [c1a3797]
|
|
39
|
+
- @splendidlabz/styles@4.7.0
|
|
40
|
+
|
|
3
41
|
## 4.8.5
|
|
4
42
|
|
|
5
43
|
### Patch Changes
|
|
@@ -1,42 +1,44 @@
|
|
|
1
1
|
<script>
|
|
2
|
+
import { isFunction } from '@splendidlabz/utils'
|
|
2
3
|
import { randomString } from '@splendidlabz/utils/dom'
|
|
3
4
|
import { applyTransition, init } from '../../utils/index.js'
|
|
4
5
|
import { reloadScripts } from '../../utils/loadscripts.js'
|
|
5
6
|
import { IconSnippet } from '../Icon/IconSnippet.svelte'
|
|
6
7
|
import { MarkdownSnippet } from '../Markdown/MarkdownSnippet.svelte'
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
let {
|
|
9
10
|
id = randomString(9),
|
|
10
11
|
class: klass,
|
|
11
|
-
titleClass,
|
|
12
|
-
contentClass,
|
|
13
|
-
buttonClass,
|
|
14
|
-
icon = 'tabler:chevron-right',
|
|
15
|
-
iconClass,
|
|
16
|
-
iconPosition = 'end',
|
|
17
|
-
closeOthers = false,
|
|
18
|
-
useMarkdown = true,
|
|
19
12
|
titleTag = 'h3',
|
|
20
13
|
transition = 'slide',
|
|
21
14
|
title,
|
|
22
15
|
content,
|
|
16
|
+
open = $bindable(false),
|
|
17
|
+
onToggle = undefined,
|
|
23
18
|
children,
|
|
19
|
+
...rest
|
|
24
20
|
} = $props()
|
|
25
21
|
|
|
26
|
-
|
|
22
|
+
// svelte-ignore state_referenced_locally
|
|
23
|
+
const {
|
|
24
|
+
titleClass,
|
|
25
|
+
contentClass,
|
|
26
|
+
buttonClass,
|
|
27
|
+
icon = 'tabler:chevron-right',
|
|
28
|
+
iconClass,
|
|
29
|
+
iconPosition = 'end',
|
|
30
|
+
useMarkdown = true,
|
|
31
|
+
} = rest
|
|
27
32
|
|
|
28
33
|
init(() => {
|
|
29
34
|
if (!title) console.warn('Accordion: No title provided')
|
|
30
35
|
if (!content && !children) console.warn('Accordion: No content provided')
|
|
31
36
|
})
|
|
32
37
|
|
|
33
|
-
// ========================================
|
|
34
|
-
// Event Handlers
|
|
35
|
-
// ========================================
|
|
36
38
|
function handleClick(event) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (
|
|
39
|
+
open = !open
|
|
40
|
+
if (isFunction(onToggle)) onToggle(open)
|
|
41
|
+
if (open) closeOtherAccordions(event)
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
function handleKeydown(event) {
|
|
@@ -45,28 +47,26 @@
|
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
function closeAccordion() {
|
|
48
|
-
|
|
50
|
+
open = false
|
|
51
|
+
if (isFunction(onToggle)) onToggle(false)
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
// ========================================
|
|
52
|
-
// Helper functions
|
|
53
|
-
// ========================================
|
|
54
54
|
function closeOtherAccordions(event) {
|
|
55
55
|
const container = event.target.closest('.accordion-stack')
|
|
56
56
|
if (!container) return
|
|
57
57
|
const closeButtons = container.querySelectorAll('.accordion-close-button')
|
|
58
58
|
closeButtons.forEach(button => button.click())
|
|
59
|
-
|
|
59
|
+
open = true
|
|
60
60
|
}
|
|
61
61
|
</script>
|
|
62
62
|
|
|
63
63
|
<!-- Accordion HTML -->
|
|
64
|
-
<div class="accordion {klass}" class:open
|
|
64
|
+
<div class="accordion {klass}" class:open>
|
|
65
65
|
<svelte:element this={titleTag} class="accordion-title {titleClass}">
|
|
66
66
|
<button
|
|
67
67
|
class="accordion-button {buttonClass}"
|
|
68
68
|
aria-controls={id}
|
|
69
|
-
aria-expanded={
|
|
69
|
+
aria-expanded={open}
|
|
70
70
|
onclick={handleClick}
|
|
71
71
|
onkeydown={handleKeydown}
|
|
72
72
|
>
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
</button>
|
|
86
86
|
</svelte:element>
|
|
87
87
|
|
|
88
|
-
{#if
|
|
88
|
+
{#if open}
|
|
89
89
|
<div
|
|
90
90
|
{id}
|
|
91
91
|
class="accordion-content {contentClass}"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
|
2
|
+
import { resolveShow } from './accordion-controlled.js'
|
|
3
|
+
|
|
4
|
+
describe('resolveShow', () => {
|
|
5
|
+
test('returns internalShow when open is undefined', () => {
|
|
6
|
+
expect(resolveShow(undefined, true)).toBe(true)
|
|
7
|
+
expect(resolveShow(undefined, false)).toBe(false)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('returns open value when open is provided', () => {
|
|
11
|
+
expect(resolveShow(true, false)).toBe(true)
|
|
12
|
+
expect(resolveShow(false, true)).toBe(false)
|
|
13
|
+
})
|
|
14
|
+
})
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { randomString } from '@splendidlabz/utils/dom'
|
|
3
|
+
import { setContext } from 'svelte'
|
|
4
|
+
import Accordion from '../Accordion/Accordion.svelte'
|
|
5
|
+
import Tab from '../Tabs/Tab.svelte'
|
|
6
|
+
import Tablist from '../Tabs/Tablist.svelte'
|
|
7
|
+
import Tabpanel from '../Tabs/Tabpanel.svelte'
|
|
8
|
+
import Tabpanels from '../Tabs/Tabpanels.svelte'
|
|
9
|
+
import { getTab, selectTab } from '../Tabs/tabs.svelte.js'
|
|
10
|
+
|
|
11
|
+
const {
|
|
12
|
+
class: klass = '',
|
|
13
|
+
breakpoint = 768,
|
|
14
|
+
activeIndex: activeIndexProp = 0,
|
|
15
|
+
accordion = {},
|
|
16
|
+
tablist = {},
|
|
17
|
+
tab = {},
|
|
18
|
+
tabpanels = {},
|
|
19
|
+
tabpanel = {},
|
|
20
|
+
children,
|
|
21
|
+
...rest
|
|
22
|
+
} = $props()
|
|
23
|
+
|
|
24
|
+
const tabcordionId = randomString()
|
|
25
|
+
const tabpanelsId = randomString()
|
|
26
|
+
let activeIndex = $state(activeIndexProp)
|
|
27
|
+
const items = $state([])
|
|
28
|
+
|
|
29
|
+
setContext('tabcordion', {
|
|
30
|
+
register(item) {
|
|
31
|
+
items.push(item)
|
|
32
|
+
return items.length - 1
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
$effect(() => {
|
|
37
|
+
const tabStore = getTab(tabpanelsId)
|
|
38
|
+
if (!tabStore) return
|
|
39
|
+
selectTab(tabStore, activeIndex, { shouldFocusOnTab: false })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
function onTabChange({ Tab: tabStore }) {
|
|
43
|
+
activeIndex = tabStore.state.activeTabIndex
|
|
44
|
+
}
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
|
|
48
|
+
{@html `
|
|
49
|
+
<style>
|
|
50
|
+
#${tabcordionId} .tabs { display: none; }
|
|
51
|
+
|
|
52
|
+
@media (min-width: ${breakpoint}px) {
|
|
53
|
+
#${tabcordionId} .accordion-stack { display: none; }
|
|
54
|
+
#${tabcordionId} .tabs { display: block; }
|
|
55
|
+
}
|
|
56
|
+
</style>
|
|
57
|
+
`}
|
|
58
|
+
|
|
59
|
+
<!-- Render children purely for their registration side effect; they emit no DOM -->
|
|
60
|
+
{@render children?.()}
|
|
61
|
+
|
|
62
|
+
<div id={tabpanelsId} {...rest} class="tabcordion {klass}">
|
|
63
|
+
<div class="accordion-stack">
|
|
64
|
+
{#each items as item, i (i)}
|
|
65
|
+
<Accordion
|
|
66
|
+
{...accordion}
|
|
67
|
+
content={item.content}
|
|
68
|
+
onToggle={() => (activeIndex = i)}
|
|
69
|
+
open={i === activeIndex}
|
|
70
|
+
title={item.title}
|
|
71
|
+
/>
|
|
72
|
+
{/each}
|
|
73
|
+
</div>
|
|
74
|
+
|
|
75
|
+
<div class="tabs">
|
|
76
|
+
<Tablist {...tablist} {onTabChange} target={tabpanelsId}>
|
|
77
|
+
{#each items as item, i (i)}
|
|
78
|
+
<Tab {...tab} active={i === activeIndex}>{@render item.title()}</Tab>
|
|
79
|
+
{/each}
|
|
80
|
+
</Tablist>
|
|
81
|
+
<Tabpanels {...tabpanels} id={tabpanelsId}>
|
|
82
|
+
{#each items as item, i (i)}
|
|
83
|
+
<Tabpanel {...tabpanel} active={i === activeIndex}>
|
|
84
|
+
{@render item.content()}
|
|
85
|
+
</Tabpanel>
|
|
86
|
+
{/each}
|
|
87
|
+
</Tabpanels>
|
|
88
|
+
</div>
|
|
89
|
+
</div>
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
<script>
|
|
2
|
-
const {
|
|
2
|
+
const {
|
|
3
|
+
class: klass = '',
|
|
4
|
+
title,
|
|
5
|
+
titleClass,
|
|
6
|
+
active,
|
|
7
|
+
children,
|
|
8
|
+
...rest
|
|
9
|
+
} = $props()
|
|
3
10
|
</script>
|
|
4
11
|
|
|
5
|
-
<button
|
|
6
|
-
{
|
|
7
|
-
|
|
12
|
+
<button
|
|
13
|
+
{...rest}
|
|
14
|
+
class="tab {title && 'vertical'} {klass}"
|
|
15
|
+
aria-selected={active}
|
|
16
|
+
role="tab"
|
|
17
|
+
>
|
|
18
|
+
{#if title}
|
|
19
|
+
<span class="tab-title {titleClass}">{title}</span>
|
|
8
20
|
{/if}
|
|
21
|
+
{@render children?.()}
|
|
9
22
|
</button>
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import { preferHorizontalScroll as preferHorizontalScrollAction } from '@splendidlabz/utils/dom'
|
|
3
|
-
import { breakpointObserver } from '../../actions/breakpoint-observer.js'
|
|
4
3
|
import { init } from '../../utils'
|
|
5
|
-
import { getTab, tablist
|
|
4
|
+
import { getTab, tablist } from './tabs.svelte.js'
|
|
6
5
|
|
|
7
6
|
let {
|
|
8
7
|
class: klass = '',
|
|
9
8
|
target = null,
|
|
10
|
-
breakpoints,
|
|
11
|
-
breakpointType,
|
|
12
|
-
placement: initialPlacement,
|
|
13
9
|
scrollable = true,
|
|
14
10
|
preferHorizontalScroll = true,
|
|
15
11
|
preserveOutlines = true,
|
|
@@ -20,7 +16,6 @@
|
|
|
20
16
|
...rest
|
|
21
17
|
} = $props()
|
|
22
18
|
|
|
23
|
-
let placement = $state(_ => initialPlacement || 'top')
|
|
24
19
|
let tab = $derived(getTab(target))
|
|
25
20
|
let tablistClasses = $derived(getTablistClasses(tab))
|
|
26
21
|
|
|
@@ -31,28 +26,13 @@
|
|
|
31
26
|
function getTablistClasses(tab) {
|
|
32
27
|
if (!tab) return ''
|
|
33
28
|
const { placement } = tab.state
|
|
29
|
+
if (placement !== 'top') return ''
|
|
34
30
|
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// Gah these need to change according to the placement
|
|
42
|
-
const scrollableClass = [
|
|
43
|
-
placement === 'top' && scrollable && 'scrollable-horizontal',
|
|
44
|
-
placement === 'top' &&
|
|
45
|
-
preferHorizontalScroll &&
|
|
46
|
-
'scrollable-preferHorizontalScroll',
|
|
47
|
-
placement === 'top' && preserveOutlines && 'preserve-outlines',
|
|
48
|
-
].join(' ')
|
|
49
|
-
|
|
50
|
-
return `${tablistClass} ${scrollableClass}`
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function breakpointCB({ value }) {
|
|
54
|
-
placement = value.placement
|
|
55
|
-
updatePlacement(tab, value.placement)
|
|
31
|
+
const classes = []
|
|
32
|
+
if (scrollable) classes.push('scrollable-horizontal')
|
|
33
|
+
if (preferHorizontalScroll) classes.push('scrollable-preferHorizontalScroll')
|
|
34
|
+
if (preserveOutlines) classes.push('preserve-outlines')
|
|
35
|
+
return classes.join(' ')
|
|
56
36
|
}
|
|
57
37
|
</script>
|
|
58
38
|
|
|
@@ -61,17 +41,11 @@
|
|
|
61
41
|
class="tablist {tablistClasses} {klass}"
|
|
62
42
|
role="tablist"
|
|
63
43
|
{target}
|
|
64
|
-
use:breakpointObserver={{
|
|
65
|
-
breakpoints,
|
|
66
|
-
breakpointType,
|
|
67
|
-
callback: breakpointCB,
|
|
68
|
-
}}
|
|
69
44
|
use:preferHorizontalScrollAction
|
|
70
45
|
use:tablist={{
|
|
71
|
-
state: {
|
|
46
|
+
state: {},
|
|
72
47
|
options: rest,
|
|
73
48
|
}}
|
|
74
49
|
>
|
|
75
|
-
<!-- Need to add callback to breakpoint opts -->
|
|
76
50
|
{@render children?.()}
|
|
77
51
|
</div>
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Example: Responsive tabs — horizontal on mobile, vertical (left) on desktop.
|
|
3
|
+
Copy-paste this as a starting point. Adjust classes and breakpoints as needed.
|
|
4
|
+
|
|
5
|
+
Usage in Astro:
|
|
6
|
+
<TabsExample client:load />
|
|
7
|
+
|
|
8
|
+
The key concept: placement classes (tablist-top, tablist-left, tablist-right)
|
|
9
|
+
control both the CSS layout AND keyboard navigation automatically.
|
|
10
|
+
Use responsive prefixes to change placement at breakpoints.
|
|
11
|
+
-->
|
|
12
|
+
<script>
|
|
13
|
+
import Tab from './Tab.svelte'
|
|
14
|
+
import Tablist from './Tablist.svelte'
|
|
15
|
+
import Tabpanel from './Tabpanel.svelte'
|
|
16
|
+
import Tabpanels from './Tabpanels.svelte'
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
<div class="tabs-example" style="display: grid; gap: 1rem;">
|
|
20
|
+
<!--
|
|
21
|
+
Mobile: tablist-top (horizontal tabs)
|
|
22
|
+
Desktop (≥1000px): tablist-left (vertical tabs on the left)
|
|
23
|
+
|
|
24
|
+
Replace "max-bp10:" and "bp10:" with your own responsive prefixes,
|
|
25
|
+
or use standard Tailwind breakpoints like "max-md:" and "md:".
|
|
26
|
+
|
|
27
|
+
If you don't use responsive utility prefixes, use media queries in your CSS:
|
|
28
|
+
@media (max-width: 999px) { .my-tablist { --placement: top; } }
|
|
29
|
+
@media (min-width: 1000px) { .my-tablist { --placement: left; } }
|
|
30
|
+
-->
|
|
31
|
+
<Tablist
|
|
32
|
+
class="tablist-top"
|
|
33
|
+
target="example-panels"
|
|
34
|
+
>
|
|
35
|
+
<Tab active>First Tab</Tab>
|
|
36
|
+
<Tab>Second Tab</Tab>
|
|
37
|
+
<Tab>Third Tab</Tab>
|
|
38
|
+
</Tablist>
|
|
39
|
+
|
|
40
|
+
<Tabpanels id="example-panels">
|
|
41
|
+
<Tabpanel active>
|
|
42
|
+
<p>Content for the first tab.</p>
|
|
43
|
+
</Tabpanel>
|
|
44
|
+
<Tabpanel>
|
|
45
|
+
<p>Content for the second tab.</p>
|
|
46
|
+
</Tabpanel>
|
|
47
|
+
<Tabpanel>
|
|
48
|
+
<p>Content for the third tab.</p>
|
|
49
|
+
</Tabpanel>
|
|
50
|
+
</Tabpanels>
|
|
51
|
+
</div>
|
|
52
|
+
|
|
53
|
+
<style>
|
|
54
|
+
/*
|
|
55
|
+
For responsive placement, add media queries that change the placement class.
|
|
56
|
+
The CSS placement classes (tablist-top, tablist-left, tablist-right) each set
|
|
57
|
+
a --placement CSS custom property that JS reads for keyboard navigation.
|
|
58
|
+
|
|
59
|
+
Example with custom breakpoint:
|
|
60
|
+
@media (min-width: 1000px) {
|
|
61
|
+
:global(.tablist) {
|
|
62
|
+
This would need tablist-left class applied at this breakpoint.
|
|
63
|
+
If using Tailwind/utility CSS, apply via responsive prefix in the class attribute.
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
*/
|
|
67
|
+
</style>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
import { beforeAll, describe, expect, test } from 'vitest'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
|
|
6
|
+
// Resolve the CSS file relative to this test
|
|
7
|
+
const cssPath = path.resolve(
|
|
8
|
+
fileURLToPath(import.meta.url),
|
|
9
|
+
'../../../../styles/src/components/svelte/tabs.css'
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
describe('tabs.css --placement custom property', () => {
|
|
13
|
+
let css
|
|
14
|
+
|
|
15
|
+
beforeAll(async () => {
|
|
16
|
+
css = await readFile(cssPath, 'utf8')
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('tablist-top sets --placement: top', () => {
|
|
20
|
+
// Match the @utility tablist-top block (may contain nested braces)
|
|
21
|
+
const match = css.match(/@utility tablist-top\s*\{[\s\S]*?\n\}/)
|
|
22
|
+
expect(match).toBeTruthy()
|
|
23
|
+
expect(match[0]).toContain('--placement: top')
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('tablist-left sets --placement: left', () => {
|
|
27
|
+
const match = css.match(/@utility tablist-left\s*\{[\s\S]*?\n\}/)
|
|
28
|
+
expect(match).toBeTruthy()
|
|
29
|
+
expect(match[0]).toContain('--placement: left')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('tablist-right sets --placement: right', () => {
|
|
33
|
+
const match = css.match(/@utility tablist-right\s*\{[\s\S]*?\n\}/)
|
|
34
|
+
expect(match).toBeTruthy()
|
|
35
|
+
expect(match[0]).toContain('--placement: right')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('all three placements are covered', () => {
|
|
39
|
+
expect(css).toContain('--placement: top')
|
|
40
|
+
expect(css).toContain('--placement: left')
|
|
41
|
+
expect(css).toContain('--placement: right')
|
|
42
|
+
})
|
|
43
|
+
})
|
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
getTab,
|
|
4
|
+
tablist,
|
|
5
|
+
tabpanels,
|
|
6
|
+
selectTab,
|
|
7
|
+
selectNextTab,
|
|
8
|
+
selectPrevTab,
|
|
9
|
+
} from './tabs.svelte.js'
|
|
10
|
+
|
|
11
|
+
// ============================================
|
|
12
|
+
// Test Helpers
|
|
13
|
+
// ============================================
|
|
14
|
+
|
|
15
|
+
let testCounter = 0
|
|
16
|
+
const cleanupNodes = []
|
|
17
|
+
|
|
18
|
+
function uniqueId() {
|
|
19
|
+
return `test-panels-${++testCounter}`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function createTabsDOM({ placement = 'top', tabCount = 3, id } = {}) {
|
|
23
|
+
const panelId = id || uniqueId()
|
|
24
|
+
|
|
25
|
+
// Create tablist
|
|
26
|
+
const tablistNode = document.createElement('div')
|
|
27
|
+
tablistNode.classList.add('tablist')
|
|
28
|
+
tablistNode.setAttribute('role', 'tablist')
|
|
29
|
+
tablistNode.setAttribute('target', panelId)
|
|
30
|
+
if (placement) {
|
|
31
|
+
tablistNode.style.setProperty('--placement', placement)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (let i = 0; i < tabCount; i++) {
|
|
35
|
+
const tab = document.createElement('button')
|
|
36
|
+
tab.setAttribute('role', 'tab')
|
|
37
|
+
tab.classList.add('tab')
|
|
38
|
+
tab.textContent = `Tab ${i}`
|
|
39
|
+
tab.setAttribute('tabindex', i === 0 ? '0' : '-1')
|
|
40
|
+
if (i === 0) tab.setAttribute('aria-selected', 'true')
|
|
41
|
+
tablistNode.appendChild(tab)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
document.body.appendChild(tablistNode)
|
|
45
|
+
|
|
46
|
+
// Create tabpanels
|
|
47
|
+
const panelsNode = document.createElement('div')
|
|
48
|
+
panelsNode.id = panelId
|
|
49
|
+
panelsNode.classList.add('tabpanels')
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < tabCount; i++) {
|
|
52
|
+
const panel = document.createElement('div')
|
|
53
|
+
panel.setAttribute('role', 'tabpanel')
|
|
54
|
+
panel.classList.add('tabpanel')
|
|
55
|
+
panel.setAttribute('tabindex', '0')
|
|
56
|
+
panel.textContent = `Panel content ${i}`
|
|
57
|
+
if (i !== 0) panel.setAttribute('hidden', 'true')
|
|
58
|
+
panelsNode.appendChild(panel)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
document.body.appendChild(panelsNode)
|
|
62
|
+
cleanupNodes.push(tablistNode, panelsNode)
|
|
63
|
+
|
|
64
|
+
return { tablistNode, panelsNode, panelId }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function initActions({ tablistNode, panelsNode }) {
|
|
68
|
+
const tablistAction = tablist(tablistNode, { state: {}, options: {} })
|
|
69
|
+
const panelsAction = tabpanels(panelsNode, {})
|
|
70
|
+
return { tablistAction, panelsAction }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function pressKey(node, key) {
|
|
74
|
+
node.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true }))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function clickTab(tab) {
|
|
78
|
+
tab.dispatchEvent(new MouseEvent('click', { bubbles: true }))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function getTabElements(tablistNode) {
|
|
82
|
+
return Array.from(tablistNode.querySelectorAll('[role="tab"]'))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function getPanelElements(panelsNode) {
|
|
86
|
+
return Array.from(panelsNode.querySelectorAll('[role="tabpanel"]'))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
afterEach(() => {
|
|
90
|
+
cleanupNodes.forEach(n => n.parentNode?.removeChild(n))
|
|
91
|
+
cleanupNodes.length = 0
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
// ============================================
|
|
95
|
+
// readPlacement
|
|
96
|
+
// ============================================
|
|
97
|
+
|
|
98
|
+
describe('readPlacement', () => {
|
|
99
|
+
test('reads --placement: top from CSS', async () => {
|
|
100
|
+
const { readPlacement } = await import('./tabs.svelte.js')
|
|
101
|
+
const node = document.createElement('div')
|
|
102
|
+
node.style.setProperty('--placement', 'top')
|
|
103
|
+
document.body.appendChild(node)
|
|
104
|
+
cleanupNodes.push(node)
|
|
105
|
+
|
|
106
|
+
expect(readPlacement(node)).toBe('top')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('reads --placement: left from CSS', async () => {
|
|
110
|
+
const { readPlacement } = await import('./tabs.svelte.js')
|
|
111
|
+
const node = document.createElement('div')
|
|
112
|
+
node.style.setProperty('--placement', 'left')
|
|
113
|
+
document.body.appendChild(node)
|
|
114
|
+
cleanupNodes.push(node)
|
|
115
|
+
|
|
116
|
+
expect(readPlacement(node)).toBe('left')
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('reads --placement: right from CSS', async () => {
|
|
120
|
+
const { readPlacement } = await import('./tabs.svelte.js')
|
|
121
|
+
const node = document.createElement('div')
|
|
122
|
+
node.style.setProperty('--placement', 'right')
|
|
123
|
+
document.body.appendChild(node)
|
|
124
|
+
cleanupNodes.push(node)
|
|
125
|
+
|
|
126
|
+
expect(readPlacement(node)).toBe('right')
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('defaults to "top" when --placement is not set', async () => {
|
|
130
|
+
const { readPlacement } = await import('./tabs.svelte.js')
|
|
131
|
+
const node = document.createElement('div')
|
|
132
|
+
document.body.appendChild(node)
|
|
133
|
+
cleanupNodes.push(node)
|
|
134
|
+
|
|
135
|
+
expect(readPlacement(node)).toBe('top')
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
// ============================================
|
|
140
|
+
// Tab Selection (click)
|
|
141
|
+
// ============================================
|
|
142
|
+
|
|
143
|
+
describe('tab selection via click', () => {
|
|
144
|
+
test('clicking a tab selects it', () => {
|
|
145
|
+
const { tablistNode, panelsNode, panelId } = createTabsDOM()
|
|
146
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
147
|
+
|
|
148
|
+
const tabs = getTabElements(tablistNode)
|
|
149
|
+
const panels = getPanelElements(panelsNode)
|
|
150
|
+
|
|
151
|
+
clickTab(tabs[1])
|
|
152
|
+
|
|
153
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true')
|
|
154
|
+
expect(tabs[0].hasAttribute('aria-selected')).toBe(false)
|
|
155
|
+
expect(panels[1].hasAttribute('hidden')).toBe(false)
|
|
156
|
+
expect(panels[0].getAttribute('hidden')).toBe('true')
|
|
157
|
+
|
|
158
|
+
tablistAction.destroy()
|
|
159
|
+
panelsAction.destroy()
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('clicking a tab updates tabindex roving', () => {
|
|
163
|
+
const { tablistNode, panelsNode } = createTabsDOM()
|
|
164
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
165
|
+
|
|
166
|
+
const tabs = getTabElements(tablistNode)
|
|
167
|
+
|
|
168
|
+
clickTab(tabs[2])
|
|
169
|
+
|
|
170
|
+
expect(tabs[2].getAttribute('tabindex')).toBe('0')
|
|
171
|
+
expect(tabs[0].getAttribute('tabindex')).toBe('-1')
|
|
172
|
+
expect(tabs[1].getAttribute('tabindex')).toBe('-1')
|
|
173
|
+
|
|
174
|
+
tablistAction.destroy()
|
|
175
|
+
panelsAction.destroy()
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('clicking the already-active tab does not break state', () => {
|
|
179
|
+
const { tablistNode, panelsNode } = createTabsDOM()
|
|
180
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
181
|
+
|
|
182
|
+
const tabs = getTabElements(tablistNode)
|
|
183
|
+
|
|
184
|
+
clickTab(tabs[0])
|
|
185
|
+
|
|
186
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
187
|
+
|
|
188
|
+
tablistAction.destroy()
|
|
189
|
+
panelsAction.destroy()
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
// ============================================
|
|
194
|
+
// Accessibility: ARIA attributes
|
|
195
|
+
// ============================================
|
|
196
|
+
|
|
197
|
+
describe('accessibility: ARIA attributes', () => {
|
|
198
|
+
test('only one tab has aria-selected at any time', () => {
|
|
199
|
+
const { tablistNode, panelsNode } = createTabsDOM()
|
|
200
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
201
|
+
|
|
202
|
+
const tabs = getTabElements(tablistNode)
|
|
203
|
+
|
|
204
|
+
// Click through each tab
|
|
205
|
+
for (let i = 0; i < tabs.length; i++) {
|
|
206
|
+
clickTab(tabs[i])
|
|
207
|
+
const selected = tabs.filter(t => t.getAttribute('aria-selected') === 'true')
|
|
208
|
+
expect(selected.length).toBe(1)
|
|
209
|
+
expect(selected[0]).toBe(tabs[i])
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
tablistAction.destroy()
|
|
213
|
+
panelsAction.destroy()
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('only one panel is visible at any time', () => {
|
|
217
|
+
const { tablistNode, panelsNode } = createTabsDOM()
|
|
218
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
219
|
+
|
|
220
|
+
const tabs = getTabElements(tablistNode)
|
|
221
|
+
const panels = getPanelElements(panelsNode)
|
|
222
|
+
|
|
223
|
+
for (let i = 0; i < tabs.length; i++) {
|
|
224
|
+
clickTab(tabs[i])
|
|
225
|
+
const visible = panels.filter(p => !p.hasAttribute('hidden'))
|
|
226
|
+
expect(visible.length).toBe(1)
|
|
227
|
+
expect(visible[0]).toBe(panels[i])
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
tablistAction.destroy()
|
|
231
|
+
panelsAction.destroy()
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
test('inactive tabs have tabindex="-1", active tab has tabindex="0"', () => {
|
|
235
|
+
const { tablistNode, panelsNode } = createTabsDOM()
|
|
236
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
237
|
+
|
|
238
|
+
const tabs = getTabElements(tablistNode)
|
|
239
|
+
|
|
240
|
+
clickTab(tabs[1])
|
|
241
|
+
|
|
242
|
+
expect(tabs[1].getAttribute('tabindex')).toBe('0')
|
|
243
|
+
tabs.filter((_, i) => i !== 1).forEach(t => {
|
|
244
|
+
expect(t.getAttribute('tabindex')).toBe('-1')
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
tablistAction.destroy()
|
|
248
|
+
panelsAction.destroy()
|
|
249
|
+
})
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
// ============================================
|
|
253
|
+
// Keyboard Navigation: Horizontal (top)
|
|
254
|
+
// ============================================
|
|
255
|
+
|
|
256
|
+
describe('keyboard navigation: horizontal (placement=top)', () => {
|
|
257
|
+
test('ArrowRight selects next tab', () => {
|
|
258
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'top' })
|
|
259
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
260
|
+
|
|
261
|
+
const tabs = getTabElements(tablistNode)
|
|
262
|
+
tabs[0].focus()
|
|
263
|
+
pressKey(tablistNode, 'ArrowRight')
|
|
264
|
+
|
|
265
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true')
|
|
266
|
+
expect(tabs[0].hasAttribute('aria-selected')).toBe(false)
|
|
267
|
+
|
|
268
|
+
tablistAction.destroy()
|
|
269
|
+
panelsAction.destroy()
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
test('ArrowLeft selects previous tab', () => {
|
|
273
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'top' })
|
|
274
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
275
|
+
|
|
276
|
+
const tabs = getTabElements(tablistNode)
|
|
277
|
+
|
|
278
|
+
// First select tab 1
|
|
279
|
+
clickTab(tabs[1])
|
|
280
|
+
pressKey(tablistNode, 'ArrowLeft')
|
|
281
|
+
|
|
282
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
283
|
+
|
|
284
|
+
tablistAction.destroy()
|
|
285
|
+
panelsAction.destroy()
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
test('ArrowRight at last tab does nothing (no loop by default)', () => {
|
|
289
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'top' })
|
|
290
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
291
|
+
|
|
292
|
+
const tabs = getTabElements(tablistNode)
|
|
293
|
+
|
|
294
|
+
clickTab(tabs[2]) // last tab
|
|
295
|
+
pressKey(tablistNode, 'ArrowRight')
|
|
296
|
+
|
|
297
|
+
expect(tabs[2].getAttribute('aria-selected')).toBe('true')
|
|
298
|
+
|
|
299
|
+
tablistAction.destroy()
|
|
300
|
+
panelsAction.destroy()
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
test('ArrowLeft at first tab does nothing (no loop by default)', () => {
|
|
304
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'top' })
|
|
305
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
306
|
+
|
|
307
|
+
const tabs = getTabElements(tablistNode)
|
|
308
|
+
tabs[0].focus()
|
|
309
|
+
pressKey(tablistNode, 'ArrowLeft')
|
|
310
|
+
|
|
311
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
312
|
+
|
|
313
|
+
tablistAction.destroy()
|
|
314
|
+
panelsAction.destroy()
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
test('ArrowUp/ArrowDown do NOT navigate tabs when placement is top', () => {
|
|
318
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'top' })
|
|
319
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
320
|
+
|
|
321
|
+
const tabs = getTabElements(tablistNode)
|
|
322
|
+
tabs[0].focus()
|
|
323
|
+
|
|
324
|
+
pressKey(tablistNode, 'ArrowUp')
|
|
325
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
326
|
+
|
|
327
|
+
// ArrowDown focuses tabpanel, not next tab
|
|
328
|
+
// We verify tab selection doesn't change
|
|
329
|
+
pressKey(tablistNode, 'ArrowDown')
|
|
330
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
331
|
+
|
|
332
|
+
tablistAction.destroy()
|
|
333
|
+
panelsAction.destroy()
|
|
334
|
+
})
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
// ============================================
|
|
338
|
+
// Keyboard Navigation: Vertical (left)
|
|
339
|
+
// ============================================
|
|
340
|
+
|
|
341
|
+
describe('keyboard navigation: vertical (placement=left)', () => {
|
|
342
|
+
test('ArrowDown selects next tab', () => {
|
|
343
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'left' })
|
|
344
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
345
|
+
|
|
346
|
+
const tabs = getTabElements(tablistNode)
|
|
347
|
+
tabs[0].focus()
|
|
348
|
+
pressKey(tablistNode, 'ArrowDown')
|
|
349
|
+
|
|
350
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true')
|
|
351
|
+
expect(tabs[0].hasAttribute('aria-selected')).toBe(false)
|
|
352
|
+
|
|
353
|
+
tablistAction.destroy()
|
|
354
|
+
panelsAction.destroy()
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
test('ArrowUp selects previous tab', () => {
|
|
358
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'left' })
|
|
359
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
360
|
+
|
|
361
|
+
const tabs = getTabElements(tablistNode)
|
|
362
|
+
|
|
363
|
+
clickTab(tabs[1])
|
|
364
|
+
pressKey(tablistNode, 'ArrowUp')
|
|
365
|
+
|
|
366
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
367
|
+
|
|
368
|
+
tablistAction.destroy()
|
|
369
|
+
panelsAction.destroy()
|
|
370
|
+
})
|
|
371
|
+
|
|
372
|
+
test('ArrowLeft/ArrowRight do NOT navigate tabs when placement is left', () => {
|
|
373
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'left' })
|
|
374
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
375
|
+
|
|
376
|
+
const tabs = getTabElements(tablistNode)
|
|
377
|
+
tabs[0].focus()
|
|
378
|
+
|
|
379
|
+
pressKey(tablistNode, 'ArrowLeft')
|
|
380
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
381
|
+
|
|
382
|
+
// ArrowRight focuses tabpanel, not next tab
|
|
383
|
+
pressKey(tablistNode, 'ArrowRight')
|
|
384
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
385
|
+
|
|
386
|
+
tablistAction.destroy()
|
|
387
|
+
panelsAction.destroy()
|
|
388
|
+
})
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
// ============================================
|
|
392
|
+
// Keyboard Navigation: Vertical (right)
|
|
393
|
+
// ============================================
|
|
394
|
+
|
|
395
|
+
describe('keyboard navigation: vertical (placement=right)', () => {
|
|
396
|
+
test('ArrowDown selects next tab', () => {
|
|
397
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'right' })
|
|
398
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
399
|
+
|
|
400
|
+
const tabs = getTabElements(tablistNode)
|
|
401
|
+
tabs[0].focus()
|
|
402
|
+
pressKey(tablistNode, 'ArrowDown')
|
|
403
|
+
|
|
404
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true')
|
|
405
|
+
|
|
406
|
+
tablistAction.destroy()
|
|
407
|
+
panelsAction.destroy()
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
test('ArrowUp selects previous tab', () => {
|
|
411
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'right' })
|
|
412
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
413
|
+
|
|
414
|
+
const tabs = getTabElements(tablistNode)
|
|
415
|
+
|
|
416
|
+
clickTab(tabs[2])
|
|
417
|
+
pressKey(tablistNode, 'ArrowUp')
|
|
418
|
+
|
|
419
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true')
|
|
420
|
+
|
|
421
|
+
tablistAction.destroy()
|
|
422
|
+
panelsAction.destroy()
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
test('ArrowLeft focuses tabpanel (does not navigate tabs)', () => {
|
|
426
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'right' })
|
|
427
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
428
|
+
|
|
429
|
+
const tabs = getTabElements(tablistNode)
|
|
430
|
+
tabs[0].focus()
|
|
431
|
+
|
|
432
|
+
pressKey(tablistNode, 'ArrowLeft')
|
|
433
|
+
// Tab 0 should still be selected (ArrowLeft goes to panel, doesn't change tab)
|
|
434
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
435
|
+
|
|
436
|
+
tablistAction.destroy()
|
|
437
|
+
panelsAction.destroy()
|
|
438
|
+
})
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
// ============================================
|
|
442
|
+
// Placement from CSS (the core new behavior)
|
|
443
|
+
// ============================================
|
|
444
|
+
|
|
445
|
+
describe('tablist reads placement from CSS', () => {
|
|
446
|
+
test('reads --placement: left from CSS on mount', () => {
|
|
447
|
+
const { tablistNode, panelsNode, panelId } = createTabsDOM({ placement: 'left' })
|
|
448
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
449
|
+
|
|
450
|
+
const tab = getTab(panelId)
|
|
451
|
+
expect(tab.state.placement).toBe('left')
|
|
452
|
+
|
|
453
|
+
tablistAction.destroy()
|
|
454
|
+
panelsAction.destroy()
|
|
455
|
+
})
|
|
456
|
+
|
|
457
|
+
test('reads --placement: right from CSS on mount', () => {
|
|
458
|
+
const { tablistNode, panelsNode, panelId } = createTabsDOM({ placement: 'right' })
|
|
459
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
460
|
+
|
|
461
|
+
const tab = getTab(panelId)
|
|
462
|
+
expect(tab.state.placement).toBe('right')
|
|
463
|
+
|
|
464
|
+
tablistAction.destroy()
|
|
465
|
+
panelsAction.destroy()
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
test('defaults to "top" when --placement is not set', () => {
|
|
469
|
+
const { tablistNode, panelsNode, panelId } = createTabsDOM({})
|
|
470
|
+
tablistNode.style.removeProperty('--placement')
|
|
471
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
472
|
+
|
|
473
|
+
const tab = getTab(panelId)
|
|
474
|
+
expect(tab.state.placement).toBe('top')
|
|
475
|
+
|
|
476
|
+
tablistAction.destroy()
|
|
477
|
+
panelsAction.destroy()
|
|
478
|
+
})
|
|
479
|
+
})
|
|
480
|
+
|
|
481
|
+
// ============================================
|
|
482
|
+
// CLS prevention: no JS-added placement classes
|
|
483
|
+
// ============================================
|
|
484
|
+
|
|
485
|
+
describe('CLS prevention', () => {
|
|
486
|
+
test('tablist action does NOT add tablist-top/left/right classes to the DOM', () => {
|
|
487
|
+
const { tablistNode, panelsNode } = createTabsDOM({ placement: 'left' })
|
|
488
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
489
|
+
|
|
490
|
+
// The action should not add placement classes — CSS handles that
|
|
491
|
+
// The node should only have what was set during DOM creation
|
|
492
|
+
expect(tablistNode.classList.contains('tablist-left')).toBe(false)
|
|
493
|
+
expect(tablistNode.classList.contains('tablist-top')).toBe(false)
|
|
494
|
+
expect(tablistNode.classList.contains('tablist-right')).toBe(false)
|
|
495
|
+
|
|
496
|
+
tablistAction.destroy()
|
|
497
|
+
panelsAction.destroy()
|
|
498
|
+
})
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
// ============================================
|
|
502
|
+
// selectTab, selectNextTab, selectPrevTab
|
|
503
|
+
// ============================================
|
|
504
|
+
|
|
505
|
+
describe('selectTab API', () => {
|
|
506
|
+
test('selectTab changes active tab and panel', () => {
|
|
507
|
+
const { tablistNode, panelsNode, panelId } = createTabsDOM()
|
|
508
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
509
|
+
|
|
510
|
+
const tab = getTab(panelId)
|
|
511
|
+
const tabs = getTabElements(tablistNode)
|
|
512
|
+
const panels = getPanelElements(panelsNode)
|
|
513
|
+
|
|
514
|
+
selectTab(tab, 2, { shouldFocusOnTab: false })
|
|
515
|
+
|
|
516
|
+
expect(tabs[2].getAttribute('aria-selected')).toBe('true')
|
|
517
|
+
expect(panels[2].hasAttribute('hidden')).toBe(false)
|
|
518
|
+
expect(panels[0].getAttribute('hidden')).toBe('true')
|
|
519
|
+
|
|
520
|
+
tablistAction.destroy()
|
|
521
|
+
panelsAction.destroy()
|
|
522
|
+
})
|
|
523
|
+
|
|
524
|
+
test('selectNextTab advances from current', () => {
|
|
525
|
+
const { tablistNode, panelsNode, panelId } = createTabsDOM()
|
|
526
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
527
|
+
|
|
528
|
+
const tab = getTab(panelId)
|
|
529
|
+
const tabs = getTabElements(tablistNode)
|
|
530
|
+
|
|
531
|
+
selectNextTab(tab)
|
|
532
|
+
|
|
533
|
+
expect(tabs[1].getAttribute('aria-selected')).toBe('true')
|
|
534
|
+
|
|
535
|
+
tablistAction.destroy()
|
|
536
|
+
panelsAction.destroy()
|
|
537
|
+
})
|
|
538
|
+
|
|
539
|
+
test('selectPrevTab does nothing at index 0 without loop', () => {
|
|
540
|
+
const { tablistNode, panelsNode, panelId } = createTabsDOM()
|
|
541
|
+
const { tablistAction, panelsAction } = initActions({ tablistNode, panelsNode })
|
|
542
|
+
|
|
543
|
+
const tab = getTab(panelId)
|
|
544
|
+
const tabs = getTabElements(tablistNode)
|
|
545
|
+
|
|
546
|
+
selectPrevTab(tab)
|
|
547
|
+
|
|
548
|
+
expect(tabs[0].getAttribute('aria-selected')).toBe('true')
|
|
549
|
+
|
|
550
|
+
tablistAction.destroy()
|
|
551
|
+
panelsAction.destroy()
|
|
552
|
+
})
|
|
553
|
+
})
|
|
@@ -7,6 +7,15 @@ import {
|
|
|
7
7
|
removeListeners,
|
|
8
8
|
} from '@splendidlabz/utils/dom'
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Reads the --placement CSS custom property from a node's computed style.
|
|
12
|
+
* Falls back to 'top' if not set.
|
|
13
|
+
*/
|
|
14
|
+
export function readPlacement(node) {
|
|
15
|
+
const value = getComputedStyle(node).getPropertyValue('--placement').trim()
|
|
16
|
+
return value || 'top'
|
|
17
|
+
}
|
|
18
|
+
|
|
10
19
|
// ========================
|
|
11
20
|
// Shared State
|
|
12
21
|
// ========================
|
|
@@ -70,7 +79,7 @@ export function tablist(node, props) {
|
|
|
70
79
|
id: targetId,
|
|
71
80
|
state: {
|
|
72
81
|
...DEFAULT_TABLIST_OPTIONS.state,
|
|
73
|
-
placement:
|
|
82
|
+
placement: readPlacement(node),
|
|
74
83
|
activeTabIndex,
|
|
75
84
|
length: tablistItems.length,
|
|
76
85
|
},
|
|
@@ -101,12 +110,23 @@ export function tablist(node, props) {
|
|
|
101
110
|
|
|
102
111
|
addListeners(listeners)
|
|
103
112
|
|
|
113
|
+
// Re-read placement from CSS on resize (handles responsive breakpoints)
|
|
114
|
+
function handleResize() {
|
|
115
|
+
const newPlacement = readPlacement(node)
|
|
116
|
+
if (newPlacement !== Tab.state.placement) {
|
|
117
|
+
updatePlacement(Tab, newPlacement)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
window.addEventListener('resize', handleResize)
|
|
122
|
+
|
|
104
123
|
const { autoplay } = Tab.options
|
|
105
124
|
if (autoplay) setAutoplayInterval(Tab)
|
|
106
125
|
|
|
107
126
|
return {
|
|
108
127
|
destroy() {
|
|
109
128
|
removeListeners(listeners)
|
|
129
|
+
window.removeEventListener('resize', handleResize)
|
|
110
130
|
},
|
|
111
131
|
}
|
|
112
132
|
|
|
@@ -154,13 +174,17 @@ export function tabpanels(node, props) {
|
|
|
154
174
|
node.querySelectorAll('[role="tabpanel"]'),
|
|
155
175
|
).filter(tab => tab.closest('.tabpanels') === node)
|
|
156
176
|
|
|
177
|
+
const existingTab = getTab(tabpanelsId)
|
|
157
178
|
let activeTabIndex
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
179
|
+
if (existingTab?.nodes?.tablist != null) {
|
|
180
|
+
activeTabIndex = existingTab.state.activeTabIndex ?? 0
|
|
181
|
+
} else {
|
|
182
|
+
activeTabIndex = tabpanelItems.findIndex(
|
|
183
|
+
tabpanel => !tabpanel.hasAttribute('hidden'),
|
|
184
|
+
)
|
|
185
|
+
if (activeTabIndex === -1) activeTabIndex = 0
|
|
186
|
+
}
|
|
162
187
|
|
|
163
|
-
// Initialize the Tab
|
|
164
188
|
const Tab = initTab({
|
|
165
189
|
id: tabpanelsId,
|
|
166
190
|
state: { activeTabIndex, length: tabpanelItems.length },
|
package/components/index.js
CHANGED
|
@@ -25,6 +25,7 @@ export * from './Populate/index.js'
|
|
|
25
25
|
export * from './Switcheroo/index.js'
|
|
26
26
|
export * from './Table/index.js'
|
|
27
27
|
export * from './Tabs/index.js'
|
|
28
|
+
export * from './Tabcordion/index.js'
|
|
28
29
|
|
|
29
30
|
// TO REMOVE AFTER SHIFTING TO ASTRO
|
|
30
31
|
export * from './Textwall/index.js'
|
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.10.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"homepage": "https://splendidlabz.com/docs/svelte",
|
|
7
7
|
"scripts": {
|
|
@@ -23,20 +23,22 @@
|
|
|
23
23
|
"./utils": "./utils/index.js"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@floating-ui/dom": "^1.6
|
|
27
|
-
"@iconify/svelte": "^
|
|
26
|
+
"@floating-ui/dom": "^1.7.6",
|
|
27
|
+
"@iconify/svelte": "^5.2.1",
|
|
28
28
|
"@splendidlabz/styles": "*",
|
|
29
29
|
"@splendidlabz/utils": "*",
|
|
30
30
|
"autosuggest-highlight": "^3.3.4",
|
|
31
|
-
"lodash-es": "^4.
|
|
32
|
-
"node-html-parser": "^7.0
|
|
31
|
+
"lodash-es": "^4.18.1",
|
|
32
|
+
"node-html-parser": "^7.1.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@splendidlabz/eslint-config": "2.1.
|
|
36
|
-
"@sveltejs/vite-plugin-svelte": "^
|
|
37
|
-
"@
|
|
38
|
-
"svelte": "^5.
|
|
39
|
-
"
|
|
40
|
-
"
|
|
35
|
+
"@splendidlabz/eslint-config": "2.1.4",
|
|
36
|
+
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
|
37
|
+
"@testing-library/svelte": "^5.3.1",
|
|
38
|
+
"@tsconfig/svelte": "^5.0.8",
|
|
39
|
+
"happy-dom": "^20.9.0",
|
|
40
|
+
"svelte": "^5.55.4",
|
|
41
|
+
"svelte-check": "^4.4.6",
|
|
42
|
+
"vitest": "^4.1.4"
|
|
41
43
|
}
|
|
42
44
|
}
|
package/vitest.config.js
CHANGED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { Resizer } from '../Resizer/index.js'
|
|
3
|
-
|
|
4
|
-
let klass = ''
|
|
5
|
-
export { klass as class }
|
|
6
|
-
export let frameContentClass = ''
|
|
7
|
-
export let contentClass = ''
|
|
8
|
-
export let options = {}
|
|
9
|
-
export let elem = null
|
|
10
|
-
</script>
|
|
11
|
-
|
|
12
|
-
<div class="BrowserFrame {klass}" bind:this={elem}>
|
|
13
|
-
<div class="Vertical gap-0">
|
|
14
|
-
<div class="FrameHeader">
|
|
15
|
-
<div class="FrameHeaderDots">
|
|
16
|
-
<div class="FrameHeaderDot"></div>
|
|
17
|
-
<div class="FrameHeaderDot"></div>
|
|
18
|
-
<div class="FrameHeaderDot"></div>
|
|
19
|
-
</div>
|
|
20
|
-
<div class="FrameHeaderTabs">
|
|
21
|
-
<slot name="tablist" />
|
|
22
|
-
</div>
|
|
23
|
-
</div>
|
|
24
|
-
<div class="FrameContent Stack {frameContentClass}">
|
|
25
|
-
<div class="Background">
|
|
26
|
-
<slot name="background" />
|
|
27
|
-
</div>
|
|
28
|
-
<div class="Content Vertical {contentClass}">
|
|
29
|
-
<slot name="tabpanels" />
|
|
30
|
-
<slot />
|
|
31
|
-
</div>
|
|
32
|
-
{#if options.resize}
|
|
33
|
-
<Resizer options={{ target: '.BrowserFrame', ...options.resize }} />
|
|
34
|
-
{/if}
|
|
35
|
-
</div>
|
|
36
|
-
</div>
|
|
37
|
-
</div>
|