noph-ui 0.23.1 → 0.23.3

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.
@@ -36,7 +36,6 @@
36
36
 
37
37
  const setTabActive = (el: HTMLElement) => {
38
38
  parentElement?.dispatchEvent(new CustomEvent('change', { detail: { id: el.id, value } }))
39
- selected = true
40
39
  }
41
40
 
42
41
  const onClick = (event: MouseEvent & { currentTarget: EventTarget & HTMLElement }) => {
@@ -53,7 +52,7 @@
53
52
  onkeydown(event)
54
53
  }
55
54
  }
56
- const setCheckInitialState = (el: HTMLElement) => {
55
+ const setCheckInitialState = () => {
57
56
  if (parentElement?.getAttribute('data-value') === value) {
58
57
  selected = true
59
58
  }
@@ -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 oldTab = activeTab
18
+ const tabs = getCurrentTabs()
14
19
  const newTab = tabs?.find((tab) => tab.getAttribute('data-value') === value)
15
- if (newTab && oldTab && newTab !== oldTab) {
16
- selectTab(newTab, oldTab, { id: newTab.id, value })
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
- oldTab: HTMLElement,
28
+ tabs: HTMLElement[],
24
29
  detail: { id: string; value: string | number },
25
30
  ) => {
26
- const oldIndicator = oldTab.querySelector('.np-indicator') as HTMLElement
27
- const oldIndicatorRect = oldIndicator.getBoundingClientRect()
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 oldTab = activeTab
58
+ const tabs = getCurrentTabs()
51
59
  const newTab = tabs?.find((tab) => tab.id === detail.id)
52
- if (newTab && oldTab && newTab !== oldTab) {
53
- selectTab(newTab, oldTab, detail)
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
- if (activeTab === undefined) {
65
- const childTabs = Array.from(el.querySelectorAll<HTMLElement>('.np-tab'))
66
- activeTab =
67
- childTabs && childTabs.length > 0
68
- ? (childTabs.find((t) => {
69
- return t.getAttribute('data-value') === initialValue
70
- }) ?? childTabs[0])
71
- : undefined
72
- if (initialValue === undefined) {
73
- value = activeTab?.getAttribute('data-value') ?? undefined
74
- } else {
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') as HTMLElement
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]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "noph-ui",
3
- "version": "0.23.1",
3
+ "version": "0.23.3",
4
4
  "license": "MIT",
5
5
  "homepage": "https://noph.dev",
6
6
  "repository": {