noph-ui 0.23.1 → 0.23.2
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/dist/tabs/Tab.svelte +1 -1
- package/dist/tabs/Tabs.svelte +34 -28
- package/package.json +1 -1
package/dist/tabs/Tab.svelte
CHANGED
package/dist/tabs/Tabs.svelte
CHANGED
|
@@ -4,27 +4,36 @@
|
|
|
4
4
|
import type { TabsProps } from './types.ts'
|
|
5
5
|
|
|
6
6
|
let { children, element = $bindable(), value = $bindable(), ...attributes }: TabsProps = $props()
|
|
7
|
-
let tabs: HTMLElement[] | undefined = $state()
|
|
8
|
-
let activeTab: HTMLElement | undefined = $state()
|
|
9
7
|
const initialValue = value
|
|
10
8
|
|
|
9
|
+
const getCurrentTabs = () => {
|
|
10
|
+
if (!element) {
|
|
11
|
+
return []
|
|
12
|
+
}
|
|
13
|
+
return Array.from(element.querySelectorAll<HTMLElement>('.np-tab'))
|
|
14
|
+
}
|
|
15
|
+
|
|
11
16
|
$effect(() => {
|
|
12
17
|
if (value) {
|
|
13
|
-
const
|
|
18
|
+
const tabs = getCurrentTabs()
|
|
14
19
|
const newTab = tabs?.find((tab) => tab.getAttribute('data-value') === value)
|
|
15
|
-
if (newTab
|
|
16
|
-
selectTab(newTab,
|
|
20
|
+
if (newTab) {
|
|
21
|
+
selectTab(newTab, tabs, { id: newTab.id, value })
|
|
17
22
|
}
|
|
18
23
|
}
|
|
19
24
|
})
|
|
20
25
|
|
|
21
26
|
const selectTab = (
|
|
22
27
|
newTab: HTMLElement,
|
|
23
|
-
|
|
28
|
+
tabs: HTMLElement[],
|
|
24
29
|
detail: { id: string; value: string | number },
|
|
25
30
|
) => {
|
|
26
|
-
const
|
|
27
|
-
|
|
31
|
+
const oldTab = tabs.find((tab) => tab.getAttribute('aria-selected') === 'true')
|
|
32
|
+
if (!oldTab || oldTab === newTab) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
const oldIndicator = oldTab.querySelector<HTMLElement>('.np-indicator')
|
|
36
|
+
const oldIndicatorRect = oldIndicator?.getBoundingClientRect()
|
|
28
37
|
if (oldIndicatorRect) {
|
|
29
38
|
const newIndicator = newTab.querySelector<HTMLElement>('.np-indicator')
|
|
30
39
|
if (newIndicator) {
|
|
@@ -39,7 +48,6 @@
|
|
|
39
48
|
}
|
|
40
49
|
}
|
|
41
50
|
value = detail.value
|
|
42
|
-
activeTab = newTab
|
|
43
51
|
tabs?.forEach((tab) => {
|
|
44
52
|
tab.dispatchEvent(new CustomEvent('change', { detail }))
|
|
45
53
|
})
|
|
@@ -47,10 +55,10 @@
|
|
|
47
55
|
|
|
48
56
|
const onChange = (event: Event) => {
|
|
49
57
|
const { detail } = event as CustomEvent<{ id: string; value: string | number }>
|
|
50
|
-
const
|
|
58
|
+
const tabs = getCurrentTabs()
|
|
51
59
|
const newTab = tabs?.find((tab) => tab.id === detail.id)
|
|
52
|
-
if (newTab
|
|
53
|
-
selectTab(newTab,
|
|
60
|
+
if (newTab) {
|
|
61
|
+
selectTab(newTab, tabs, detail)
|
|
54
62
|
}
|
|
55
63
|
}
|
|
56
64
|
|
|
@@ -61,20 +69,17 @@
|
|
|
61
69
|
}
|
|
62
70
|
})
|
|
63
71
|
const initialSetup = (el: HTMLElement) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
value = initialValue
|
|
76
|
-
}
|
|
77
|
-
tabs = childTabs
|
|
72
|
+
const tabs = Array.from(el.querySelectorAll<HTMLElement>('.np-tab'))
|
|
73
|
+
const activeTab =
|
|
74
|
+
tabs && tabs.length > 0
|
|
75
|
+
? (tabs.find((t) => {
|
|
76
|
+
return t.getAttribute('data-value') === initialValue
|
|
77
|
+
}) ?? tabs[0])
|
|
78
|
+
: undefined
|
|
79
|
+
if (initialValue === undefined) {
|
|
80
|
+
value = activeTab?.getAttribute('data-value') ?? undefined
|
|
81
|
+
} else {
|
|
82
|
+
value = initialValue
|
|
78
83
|
}
|
|
79
84
|
}
|
|
80
85
|
</script>
|
|
@@ -87,9 +92,10 @@
|
|
|
87
92
|
tabindex="-1"
|
|
88
93
|
bind:this={element}
|
|
89
94
|
onkeydown={(event) => {
|
|
95
|
+
const tabs = Array.from(event.currentTarget.querySelectorAll<HTMLElement>('.np-tab'))
|
|
90
96
|
if (tabs && tabs.length > 0 && (event.key === 'ArrowRight' || event.key === 'ArrowLeft')) {
|
|
91
|
-
const focusedTab = event.currentTarget.querySelector('.np-tab:focus')
|
|
92
|
-
const currentIndex = tabs.indexOf(focusedTab)
|
|
97
|
+
const focusedTab = event.currentTarget.querySelector<HTMLElement>('.np-tab:focus')
|
|
98
|
+
const currentIndex = focusedTab ? tabs.indexOf(focusedTab) : 0
|
|
93
99
|
const index = currentIndex + (event.key === 'ArrowRight' ? 1 : -1)
|
|
94
100
|
const newTab =
|
|
95
101
|
index < 0 ? tabs[tabs.length - 1] : index >= tabs.length ? tabs[0] : tabs[index]
|