@pzerelles/headlessui-svelte 2.1.2-next.30 → 2.1.2-next.32

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.
Files changed (115) hide show
  1. package/dist/button/Button.svelte +84 -54
  2. package/dist/button/Button.svelte.d.ts +2 -3
  3. package/dist/checkbox/Checkbox.svelte +173 -120
  4. package/dist/checkbox/Checkbox.svelte.d.ts +2 -3
  5. package/dist/close-button/CloseButton.svelte +12 -6
  6. package/dist/close-button/CloseButton.svelte.d.ts +7 -8
  7. package/dist/combobox/Combobox.svelte +50 -3
  8. package/dist/data-interactive/DataInteractive.svelte +55 -29
  9. package/dist/data-interactive/DataInteractive.svelte.d.ts +2 -3
  10. package/dist/description/Description.svelte +31 -21
  11. package/dist/description/Description.svelte.d.ts +1 -2
  12. package/dist/dialog/Dialog.svelte +320 -228
  13. package/dist/dialog/Dialog.svelte.d.ts +2 -3
  14. package/dist/dialog/DialogBackdrop.svelte +30 -13
  15. package/dist/dialog/DialogBackdrop.svelte.d.ts +2 -3
  16. package/dist/dialog/DialogPanel.svelte +49 -26
  17. package/dist/dialog/DialogPanel.svelte.d.ts +2 -3
  18. package/dist/dialog/DialogTitle.svelte +38 -23
  19. package/dist/dialog/DialogTitle.svelte.d.ts +2 -3
  20. package/dist/field/Field.svelte +47 -25
  21. package/dist/field/Field.svelte.d.ts +1 -2
  22. package/dist/fieldset/Fieldset.svelte +50 -29
  23. package/dist/fieldset/Fieldset.svelte.d.ts +1 -2
  24. package/dist/focus-trap/FocusTrap.svelte +419 -283
  25. package/dist/focus-trap/FocusTrap.svelte.d.ts +1 -2
  26. package/dist/input/Input.svelte +84 -53
  27. package/dist/input/Input.svelte.d.ts +2 -3
  28. package/dist/internal/FloatingProvider.svelte +14 -9
  29. package/dist/internal/FocusSentinel.svelte +16 -8
  30. package/dist/internal/ForcePortalRoot.svelte +7 -3
  31. package/dist/internal/FormFields.svelte +47 -34
  32. package/dist/internal/FormFieldsProvider.svelte +9 -5
  33. package/dist/internal/FormResolver.svelte +20 -15
  34. package/dist/internal/Hidden.svelte +50 -29
  35. package/dist/internal/Hidden.svelte.d.ts +1 -2
  36. package/dist/internal/MainTreeProvider.svelte +89 -36
  37. package/dist/internal/Portal.svelte +18 -14
  38. package/dist/internal/floating.svelte.d.ts +2 -3
  39. package/dist/internal/floating.svelte.js +0 -1
  40. package/dist/label/Label.svelte +93 -58
  41. package/dist/label/Label.svelte.d.ts +1 -2
  42. package/dist/legend/Legend.svelte +12 -3
  43. package/dist/listbox/Listbox.svelte +525 -387
  44. package/dist/listbox/Listbox.svelte.d.ts +2 -3
  45. package/dist/listbox/ListboxButton.svelte +173 -127
  46. package/dist/listbox/ListboxButton.svelte.d.ts +2 -3
  47. package/dist/listbox/ListboxOption.svelte +170 -129
  48. package/dist/listbox/ListboxOption.svelte.d.ts +2 -3
  49. package/dist/listbox/ListboxOptions.svelte +400 -304
  50. package/dist/listbox/ListboxOptions.svelte.d.ts +2 -3
  51. package/dist/listbox/ListboxSelectedOption.svelte +38 -15
  52. package/dist/listbox/ListboxSelectedOption.svelte.d.ts +1 -2
  53. package/dist/menu/Menu.svelte +77 -51
  54. package/dist/menu/Menu.svelte.d.ts +2 -4
  55. package/dist/menu/MenuButton.svelte +157 -117
  56. package/dist/menu/MenuButton.svelte.d.ts +2 -3
  57. package/dist/menu/MenuHeading.svelte +32 -14
  58. package/dist/menu/MenuHeading.svelte.d.ts +1 -2
  59. package/dist/menu/MenuItem.svelte +142 -107
  60. package/dist/menu/MenuItem.svelte.d.ts +2 -3
  61. package/dist/menu/MenuItems.svelte +301 -229
  62. package/dist/menu/MenuItems.svelte.d.ts +2 -3
  63. package/dist/menu/MenuSection.svelte +24 -9
  64. package/dist/menu/MenuSection.svelte.d.ts +1 -2
  65. package/dist/menu/MenuSeparator.svelte +17 -4
  66. package/dist/menu/MenuSeparator.svelte.d.ts +1 -2
  67. package/dist/popover/Popover.svelte +216 -150
  68. package/dist/popover/Popover.svelte.d.ts +2 -3
  69. package/dist/popover/PopoverBackdrop.svelte +67 -41
  70. package/dist/popover/PopoverBackdrop.svelte.d.ts +2 -3
  71. package/dist/popover/PopoverButton.svelte +292 -212
  72. package/dist/popover/PopoverButton.svelte.d.ts +2 -3
  73. package/dist/popover/PopoverGroup.svelte +62 -35
  74. package/dist/popover/PopoverGroup.svelte.d.ts +1 -2
  75. package/dist/popover/PopoverPanel.svelte +311 -229
  76. package/dist/popover/PopoverPanel.svelte.d.ts +2 -3
  77. package/dist/portal/InternalPortal.svelte +141 -85
  78. package/dist/portal/InternalPortal.svelte.d.ts +1 -2
  79. package/dist/portal/Portal.svelte +5 -2
  80. package/dist/portal/PortalGroup.svelte +30 -9
  81. package/dist/portal/PortalGroup.svelte.d.ts +1 -2
  82. package/dist/select/Select.svelte +98 -68
  83. package/dist/select/Select.svelte.d.ts +2 -3
  84. package/dist/switch/Switch.svelte +179 -132
  85. package/dist/switch/Switch.svelte.d.ts +2 -3
  86. package/dist/switch/SwitchGroup.svelte +44 -31
  87. package/dist/switch/SwitchGroup.svelte.d.ts +1 -2
  88. package/dist/tabs/Tab.svelte +194 -142
  89. package/dist/tabs/Tab.svelte.d.ts +2 -3
  90. package/dist/tabs/TabGroup.svelte +86 -56
  91. package/dist/tabs/TabGroup.svelte.d.ts +2 -3
  92. package/dist/tabs/TabList.svelte +31 -11
  93. package/dist/tabs/TabList.svelte.d.ts +2 -3
  94. package/dist/tabs/TabPanel.svelte +67 -42
  95. package/dist/tabs/TabPanel.svelte.d.ts +2 -3
  96. package/dist/tabs/TabPanels.svelte +18 -7
  97. package/dist/tabs/TabPanels.svelte.d.ts +2 -3
  98. package/dist/textarea/Textarea.svelte +84 -53
  99. package/dist/textarea/Textarea.svelte.d.ts +2 -3
  100. package/dist/transition/InternalTransitionChild.svelte +259 -170
  101. package/dist/transition/InternalTransitionChild.svelte.d.ts +2 -3
  102. package/dist/transition/Transition.svelte +96 -66
  103. package/dist/transition/Transition.svelte.d.ts +2 -3
  104. package/dist/transition/TransitionChild.svelte +31 -11
  105. package/dist/transition/TransitionChild.svelte.d.ts +2 -3
  106. package/dist/utils/DisabledProvider.svelte +7 -3
  107. package/dist/utils/ElementOrComponent.svelte +46 -23
  108. package/dist/utils/ElementOrComponent.svelte.d.ts +8 -10
  109. package/dist/utils/Generic.svelte +30 -19
  110. package/dist/utils/Generic.svelte.d.ts +6 -7
  111. package/dist/utils/StableCollection.svelte +54 -36
  112. package/dist/utils/floating-ui/svelte/components/FloatingNode.svelte +27 -12
  113. package/dist/utils/floating-ui/svelte/components/FloatingTree.svelte +88 -44
  114. package/dist/utils/types.d.ts +4 -5
  115. package/package.json +1 -1
@@ -1,155 +1,207 @@
1
- <script lang="ts" module>const DEFAULT_TAB_TAG = "button";
2
- </script>
1
+ <script lang="ts" module>
2
+ import type { ElementType, Props } from "../utils/types.js"
3
3
 
4
- <script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_TAB_TAG">import { useId } from "../hooks/use-id.js";
5
- import { useStableCollectionIndex } from "../utils/StableCollection.svelte";
6
- import { Focus, focusIn, FocusResult } from "../utils/focus-management.js";
7
- import { getOwnerDocument } from "../utils/owner.js";
8
- import { match } from "../utils/match.js";
9
- import { microTask } from "../utils/microTask.js";
10
- import { useActivePress } from "../hooks/use-active-press.svelte.js";
11
- import { useFocusRing } from "../hooks/use-focus-ring.svelte.js";
12
- import { useResolveButtonType } from "../hooks/use-resolve-button-type.svelte.js";
13
- import { onMount } from "svelte";
14
- import { useHover } from "../hooks/use-hover.svelte.js";
15
- import { mergeProps } from "../utils/render.js";
16
- import ElementOrComponent from "../utils/ElementOrComponent.svelte";
17
- import { useTabs } from "./context.svelte.js";
18
- const internalId = useId();
19
- let {
20
- ref = $bindable(),
21
- id = `headlessui-tabs-tab-${internalId}`,
22
- disabled = false,
23
- autofocus = false,
24
- ...theirProps
25
- } = $props();
26
- const context = useTabs("Tab");
27
- const { orientation, activation, selectedIndex, tabs, panels, registerTab, change } = $derived(context);
28
- const tabRef = $derived({ current: ref });
29
- onMount(() => registerTab(tabRef));
30
- const mySSRIndex = useStableCollectionIndex("tabs");
31
- const myIndex = $derived.by(() => {
32
- const index = tabs.findIndex((tab) => tab === tabRef);
33
- return index === -1 ? mySSRIndex : index;
34
- });
35
- const selected = $derived(myIndex === selectedIndex);
36
- const activateUsing = $derived((cb) => {
37
- let result = cb();
38
- if (result === FocusResult.Success && activation === "auto") {
39
- let newTab = getOwnerDocument(ref)?.activeElement;
40
- let idx = context.tabs.findIndex((tab) => tab.current === newTab);
41
- if (idx !== -1) change(idx);
42
- }
43
- return result;
44
- });
45
- const handleKeyDown = (event) => {
46
- let list = tabs.map((tab) => tab.current).filter(Boolean);
47
- if (event.key === " " || event.key === "Enter") {
48
- event.preventDefault();
49
- event.stopPropagation();
50
- change(myIndex);
51
- return;
52
- }
53
- switch (event.key) {
54
- case "Home":
55
- case "PageUp":
56
- event.preventDefault();
57
- event.stopPropagation();
58
- return activateUsing(() => focusIn(list, Focus.First));
59
- case "End":
60
- case "PageDown":
61
- event.preventDefault();
62
- event.stopPropagation();
63
- return activateUsing(() => focusIn(list, Focus.Last));
4
+ const DEFAULT_TAB_TAG = "button" as const
5
+ type TabRenderPropArg = {
6
+ hover: boolean
7
+ focus: boolean
8
+ active: boolean
9
+ autofocus: boolean
10
+ selected: boolean
11
+ disabled: boolean
64
12
  }
65
- let result = activateUsing(() => {
66
- return match(orientation, {
67
- vertical() {
68
- if (event.key === "ArrowUp") return focusIn(list, Focus.Previous | Focus.WrapAround);
69
- if (event.key === "ArrowDown") return focusIn(list, Focus.Next | Focus.WrapAround);
70
- return FocusResult.Error;
71
- },
72
- horizontal() {
73
- if (event.key === "ArrowLeft") return focusIn(list, Focus.Previous | Focus.WrapAround);
74
- if (event.key === "ArrowRight") return focusIn(list, Focus.Next | Focus.WrapAround);
75
- return FocusResult.Error;
76
- }
77
- });
78
- });
79
- if (result === FocusResult.Success) {
80
- return event.preventDefault();
81
- }
82
- };
83
- let ready = $state(false);
84
- const handleSelection = () => {
85
- if (ready) return;
86
- ready = true;
87
- ref?.focus({ preventScroll: true });
88
- change(myIndex);
89
- microTask(() => {
90
- ready = false;
91
- });
92
- };
93
- const handleMouseDown = (event) => {
94
- event.preventDefault();
95
- };
96
- const { isHovered: hover, hoverProps } = $derived(
97
- useHover({
98
- get disabled() {
99
- return disabled;
13
+ type TabPropsWeControl = "aria-controls" | "aria-selected" | "role" | "tabIndex"
14
+
15
+ export type TabProps<TTag extends ElementType = typeof DEFAULT_TAB_TAG> = Props<
16
+ TTag,
17
+ TabRenderPropArg,
18
+ TabPropsWeControl,
19
+ {
20
+ id?: string
21
+ autofocus?: boolean
22
+ disabled?: boolean
100
23
  }
24
+ >
25
+ </script>
26
+
27
+ <script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_TAB_TAG">
28
+ import { useId } from "../hooks/use-id.js"
29
+ import { useStableCollectionIndex } from "../utils/StableCollection.svelte"
30
+ import { Focus, focusIn, FocusResult } from "../utils/focus-management.js"
31
+ import { getOwnerDocument } from "../utils/owner.js"
32
+ import { match } from "../utils/match.js"
33
+ import { microTask } from "../utils/microTask.js"
34
+ import { useActivePress } from "../hooks/use-active-press.svelte.js"
35
+ import { useFocusRing } from "../hooks/use-focus-ring.svelte.js"
36
+ import { useResolveButtonType } from "../hooks/use-resolve-button-type.svelte.js"
37
+ import type { MutableRefObject } from "../utils/ref.svelte.js"
38
+ import { onMount } from "svelte"
39
+ import { useHover } from "../hooks/use-hover.svelte.js"
40
+ import { mergeProps } from "../utils/render.js"
41
+ import ElementOrComponent from "../utils/ElementOrComponent.svelte"
42
+ import { useTabs } from "./context.svelte.js"
43
+
44
+ const internalId = useId()
45
+ let {
46
+ ref = $bindable(),
47
+ id = `headlessui-tabs-tab-${internalId}`,
48
+ disabled = false,
49
+ autofocus = false,
50
+ ...theirProps
51
+ }: { as?: TTag } & TabProps<TTag> = $props()
52
+
53
+ const context = useTabs("Tab")
54
+ const { orientation, activation, selectedIndex, tabs, panels, registerTab, change } = $derived(context)
55
+
56
+ const tabRef = $derived<MutableRefObject<HTMLElement | undefined>>({ current: ref })
57
+
58
+ onMount(() => registerTab(tabRef))
59
+
60
+ const mySSRIndex = useStableCollectionIndex("tabs")
61
+
62
+ const myIndex = $derived.by(() => {
63
+ const index = tabs.findIndex((tab) => tab === tabRef)
64
+ return index === -1 ? mySSRIndex : index
101
65
  })
102
- );
103
- const { pressed: active, pressProps } = $derived(
104
- useActivePress({
105
- get disabled() {
106
- return disabled;
66
+ const selected = $derived(myIndex === selectedIndex)
67
+
68
+ const activateUsing = $derived((cb: () => FocusResult) => {
69
+ let result = cb()
70
+ if (result === FocusResult.Success && activation === "auto") {
71
+ let newTab = getOwnerDocument(ref)?.activeElement
72
+ let idx = context.tabs.findIndex((tab) => tab.current === newTab)
73
+ if (idx !== -1) change(idx)
107
74
  }
75
+ return result
108
76
  })
109
- );
110
- const { isFocusVisible: focus, focusProps } = $derived(
111
- useFocusRing({
112
- get autofocus() {
113
- return autofocus;
77
+
78
+ const handleKeyDown = (event: KeyboardEvent) => {
79
+ let list = tabs.map((tab) => tab.current).filter(Boolean) as HTMLElement[]
80
+
81
+ if (event.key === " " || event.key === "Enter") {
82
+ event.preventDefault()
83
+ event.stopPropagation()
84
+
85
+ change(myIndex)
86
+ return
87
+ }
88
+
89
+ switch (event.key) {
90
+ case "Home":
91
+ case "PageUp":
92
+ event.preventDefault()
93
+ event.stopPropagation()
94
+
95
+ return activateUsing(() => focusIn(list, Focus.First))
96
+
97
+ case "End":
98
+ case "PageDown":
99
+ event.preventDefault()
100
+ event.stopPropagation()
101
+
102
+ return activateUsing(() => focusIn(list, Focus.Last))
103
+ }
104
+
105
+ let result = activateUsing(() => {
106
+ return match(orientation, {
107
+ vertical() {
108
+ if (event.key === "ArrowUp") return focusIn(list, Focus.Previous | Focus.WrapAround)
109
+ if (event.key === "ArrowDown") return focusIn(list, Focus.Next | Focus.WrapAround)
110
+ return FocusResult.Error
111
+ },
112
+ horizontal() {
113
+ if (event.key === "ArrowLeft") return focusIn(list, Focus.Previous | Focus.WrapAround)
114
+ if (event.key === "ArrowRight") return focusIn(list, Focus.Next | Focus.WrapAround)
115
+ return FocusResult.Error
116
+ },
117
+ })
118
+ })
119
+
120
+ if (result === FocusResult.Success) {
121
+ return event.preventDefault()
114
122
  }
115
- })
116
- );
117
- const slot = $derived({
118
- selected,
119
- hover,
120
- active,
121
- focus,
122
- autofocus,
123
- disabled
124
- });
125
- const resolvedType = useResolveButtonType({
126
- get props() {
127
- return { type: theirProps.type, as: theirProps.as };
128
- },
129
- get ref() {
130
- return tabRef;
131
123
  }
132
- });
133
- const ourProps = $derived(
134
- mergeProps(
135
- {
136
- onkeydown: handleKeyDown,
137
- onmousedown: handleMouseDown,
138
- onclick: handleSelection,
139
- id,
140
- role: "tab",
141
- type: resolvedType.type,
142
- "aria-controls": panels[myIndex]?.current?.id,
143
- "aria-selected": selected,
144
- tabIndex: selected ? 0 : -1,
145
- disabled: disabled || void 0,
146
- autofocus
124
+
125
+ let ready = $state(false)
126
+ const handleSelection = () => {
127
+ if (ready) return
128
+ ready = true
129
+
130
+ ref?.focus({ preventScroll: true })
131
+ change(myIndex)
132
+
133
+ microTask(() => {
134
+ ready = false
135
+ })
136
+ }
137
+
138
+ // This is important because we want to only focus the tab when it gets focus
139
+ // OR it finished the click event (mouseup). However, if you perform a `click`,
140
+ // then you will first get the `focus` and then get the `click` event.
141
+ const handleMouseDown = (event: MouseEvent) => {
142
+ event.preventDefault()
143
+ }
144
+
145
+ const { isHovered: hover, hoverProps } = $derived(
146
+ useHover({
147
+ get disabled() {
148
+ return disabled
149
+ },
150
+ })
151
+ )
152
+ const { pressed: active, pressProps } = $derived(
153
+ useActivePress({
154
+ get disabled() {
155
+ return disabled
156
+ },
157
+ })
158
+ )
159
+ const { isFocusVisible: focus, focusProps } = $derived(
160
+ useFocusRing({
161
+ get autofocus() {
162
+ return autofocus
163
+ },
164
+ })
165
+ )
166
+
167
+ const slot = $derived({
168
+ selected,
169
+ hover,
170
+ active,
171
+ focus,
172
+ autofocus,
173
+ disabled,
174
+ } satisfies TabRenderPropArg)
175
+
176
+ const resolvedType = useResolveButtonType({
177
+ get props() {
178
+ return { type: theirProps.type, as: theirProps.as }
179
+ },
180
+ get ref() {
181
+ return tabRef
147
182
  },
148
- focusProps,
149
- hoverProps,
150
- pressProps
183
+ })
184
+
185
+ const ourProps = $derived(
186
+ mergeProps(
187
+ {
188
+ onkeydown: handleKeyDown,
189
+ onmousedown: handleMouseDown,
190
+ onclick: handleSelection,
191
+ id,
192
+ role: "tab",
193
+ type: resolvedType.type,
194
+ "aria-controls": panels[myIndex]?.current?.id,
195
+ "aria-selected": selected,
196
+ tabIndex: selected ? 0 : -1,
197
+ disabled: disabled || undefined,
198
+ autofocus,
199
+ },
200
+ focusProps,
201
+ hoverProps,
202
+ pressProps
203
+ )
151
204
  )
152
- );
153
205
  </script>
154
206
 
155
207
  <ElementOrComponent {ourProps} {theirProps} {slot} defaultTag={DEFAULT_TAB_TAG} name="Tab" bind:ref />
@@ -18,9 +18,8 @@ declare class __sveltets_Render<TTag extends ElementType = typeof DEFAULT_TAB_TA
18
18
  props(): {
19
19
  as?: TTag | undefined;
20
20
  } & (Exclude<keyof import("../utils/types.js").PropsOf<TTag>, ("slot" | "as" | "children" | "class" | "ref") | "disabled" | "autofocus" | "id" | TabPropsWeControl> extends infer T extends keyof import("../utils/types.js").PropsOf<TTag> ? { [P in T]: import("../utils/types.js").PropsOf<TTag>[P]; } : never) & {
21
- children?: import("svelte").Snippet<[{
22
- slot: TabRenderPropArg;
23
- props: Record<string, any>;
21
+ children?: import("svelte").Snippet<[TabRenderPropArg & {
22
+ props?: Record<string, any>;
24
23
  }]> | undefined;
25
24
  class?: string | ((bag: TabRenderPropArg) => string) | null | undefined;
26
25
  ref?: HTMLElement;
@@ -1,62 +1,92 @@
1
- <script lang="ts" module>import { sortByDomNode } from "../utils/focus-management.js";
2
- import FocusSentinel from "../internal/FocusSentinel.svelte";
3
- const DEFAULT_TABS_TAG = "div";
4
- </script>
1
+ <script lang="ts" module>
2
+ import type { ElementType, Props } from "../utils/types.js"
3
+ import { sortByDomNode } from "../utils/focus-management.js"
4
+ import FocusSentinel from "../internal/FocusSentinel.svelte"
5
+
6
+ const DEFAULT_TABS_TAG = "div" as const
7
+ type TabsRenderPropArg = {
8
+ selectedIndex: number
9
+ }
10
+ type TabsPropsWeControl = never
5
11
 
6
- <script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_TABS_TAG">import StableCollection from "../utils/StableCollection.svelte";
7
- import ElementOrComponent from "../utils/ElementOrComponent.svelte";
8
- import { createTabContext } from "./context.svelte.js";
9
- import { untrack } from "svelte";
10
- let {
11
- ref = $bindable(),
12
- defaultIndex = 0,
13
- vertical = false,
14
- manual = false,
15
- onchange,
16
- selectedIndex = void 0,
17
- ...theirProps
18
- } = $props();
19
- const _state = createTabContext({
20
- get vertical() {
21
- return vertical;
22
- },
23
- get manual() {
24
- return manual;
25
- },
26
- get selectedIndex() {
27
- return selectedIndex;
28
- },
29
- get defaultIndex() {
30
- return defaultIndex;
31
- },
32
- change: (index) => {
33
- if (realSelectedIndex !== index) {
34
- onchange?.(index);
12
+ export type TabGroupProps<TTag extends ElementType = typeof DEFAULT_TABS_TAG> = Props<
13
+ TTag,
14
+ TabsRenderPropArg,
15
+ TabsPropsWeControl,
16
+ {
17
+ defaultIndex?: number
18
+ onchange?: (index: number) => void
19
+ selectedIndex?: number
20
+ vertical?: boolean
21
+ manual?: boolean
35
22
  }
36
- if (!isControlled) {
37
- _state.selectedIndex = index;
23
+ >
24
+ </script>
25
+
26
+ <script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_TABS_TAG">
27
+ import StableCollection from "../utils/StableCollection.svelte"
28
+ import ElementOrComponent from "../utils/ElementOrComponent.svelte"
29
+ import { createTabContext } from "./context.svelte.js"
30
+ import { untrack } from "svelte"
31
+
32
+ let {
33
+ ref = $bindable(),
34
+ defaultIndex = 0,
35
+ vertical = false,
36
+ manual = false,
37
+ onchange,
38
+ selectedIndex = undefined,
39
+ ...theirProps
40
+ }: { as?: TTag } & TabGroupProps<TTag> = $props()
41
+ const _state = createTabContext({
42
+ get vertical() {
43
+ return vertical
44
+ },
45
+ get manual() {
46
+ return manual
47
+ },
48
+ get selectedIndex() {
49
+ return selectedIndex
50
+ },
51
+ get defaultIndex() {
52
+ return defaultIndex
53
+ },
54
+ change: (index: number) => {
55
+ if (realSelectedIndex !== index) {
56
+ onchange?.(index)
57
+ }
58
+
59
+ if (!isControlled) {
60
+ _state.selectedIndex = index
61
+ }
62
+ },
63
+ })
64
+ const isControlled = $derived(_state.info.isControlled)
65
+ const realSelectedIndex = $derived(isControlled ? selectedIndex! : _state.selectedIndex)
66
+
67
+ const slot = $derived({
68
+ selectedIndex: _state.selectedIndex,
69
+ } satisfies TabsRenderPropArg)
70
+ const stableTabs = $derived(_state.tabs)
71
+
72
+ $effect(() => {
73
+ const newSelectedIndex = selectedIndex ?? defaultIndex
74
+ untrack(() => (_state.selectedIndex = newSelectedIndex))
75
+ })
76
+
77
+ $effect(() => {
78
+ if (realSelectedIndex === undefined) return
79
+ if (_state.tabs.length <= 0) return
80
+
81
+ // TODO: Figure out a way to detect this without the slow sort on every render. Might be fine
82
+ // unless you have a lot of tabs.
83
+ let sorted = sortByDomNode(_state.tabs, (tab) => tab.current ?? null)
84
+ let didOrderChange = sorted.some((tab, i) => _state.tabs[i] !== tab)
85
+
86
+ if (didOrderChange) {
87
+ _state.change(sorted.findIndex((tab) => tab === _state.tabs[realSelectedIndex]))
38
88
  }
39
- }
40
- });
41
- const isControlled = $derived(_state.info.isControlled);
42
- const realSelectedIndex = $derived(isControlled ? selectedIndex : _state.selectedIndex);
43
- const slot = $derived({
44
- selectedIndex: _state.selectedIndex
45
- });
46
- const stableTabs = $derived(_state.tabs);
47
- $effect(() => {
48
- const newSelectedIndex = selectedIndex ?? defaultIndex;
49
- untrack(() => _state.selectedIndex = newSelectedIndex);
50
- });
51
- $effect(() => {
52
- if (realSelectedIndex === void 0) return;
53
- if (_state.tabs.length <= 0) return;
54
- let sorted = sortByDomNode(_state.tabs, (tab) => tab.current ?? null);
55
- let didOrderChange = sorted.some((tab, i) => _state.tabs[i] !== tab);
56
- if (didOrderChange) {
57
- _state.change(sorted.findIndex((tab) => tab === _state.tabs[realSelectedIndex]));
58
- }
59
- });
89
+ })
60
90
  </script>
61
91
 
62
92
  <StableCollection>
@@ -15,9 +15,8 @@ declare class __sveltets_Render<TTag extends ElementType = typeof DEFAULT_TABS_T
15
15
  props(): {
16
16
  as?: TTag | undefined;
17
17
  } & (Exclude<keyof import("../utils/types.js").PropsOf<TTag>, ("slot" | "as" | "children" | "class" | "ref") | "onchange" | "manual" | "vertical" | "selectedIndex" | "defaultIndex"> extends infer T extends keyof import("../utils/types.js").PropsOf<TTag> ? { [P in T]: import("../utils/types.js").PropsOf<TTag>[P]; } : never) & {
18
- children?: import("svelte").Snippet<[{
19
- slot: TabsRenderPropArg;
20
- props: Record<string, any>;
18
+ children?: import("svelte").Snippet<[TabsRenderPropArg & {
19
+ props?: Record<string, any>;
21
20
  }]> | undefined;
22
21
  class?: string | ((bag: TabsRenderPropArg) => string) | null | undefined;
23
22
  ref?: HTMLElement;
@@ -1,16 +1,36 @@
1
- <script lang="ts" module>const DEFAULT_LIST_TAG = "div";
1
+ <script lang="ts" module>
2
+ import type { ElementType, Props } from "../utils/types.js"
3
+
4
+ const DEFAULT_LIST_TAG = "div" as const
5
+ type ListRenderPropArg = {
6
+ selectedIndex: number
7
+ }
8
+ type ListPropsWeControl = "aria-orientation" | "role"
9
+
10
+ export type TabListProps<TTag extends ElementType = typeof DEFAULT_LIST_TAG> = Props<
11
+ TTag,
12
+ ListRenderPropArg,
13
+ ListPropsWeControl,
14
+ {
15
+ //
16
+ }
17
+ >
2
18
  </script>
3
19
 
4
- <script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_LIST_TAG">import ElementOrComponent from "../utils/ElementOrComponent.svelte";
5
- import { useTabs } from "./context.svelte.js";
6
- const context = useTabs("TabList");
7
- const { orientation, selectedIndex } = $derived(context);
8
- const slot = $derived({ selectedIndex });
9
- let { ref = $bindable(), ...theirProps } = $props();
10
- const ourProps = $derived({
11
- role: "tablist",
12
- "aria-orientation": orientation
13
- });
20
+ <script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_LIST_TAG">
21
+ import ElementOrComponent from "../utils/ElementOrComponent.svelte"
22
+ import { useTabs } from "./context.svelte.js"
23
+
24
+ const context = useTabs("TabList")
25
+ const { orientation, selectedIndex } = $derived(context)
26
+
27
+ const slot = $derived({ selectedIndex } satisfies ListRenderPropArg)
28
+
29
+ let { ref = $bindable(), ...theirProps }: { as?: TTag } & TabListProps<TTag> = $props()
30
+ const ourProps = $derived({
31
+ role: "tablist",
32
+ "aria-orientation": orientation,
33
+ })
14
34
  </script>
15
35
 
16
36
  <ElementOrComponent {ourProps} {theirProps} {slot} defaultTag={DEFAULT_LIST_TAG} name="TabList" bind:ref />
@@ -9,9 +9,8 @@ declare class __sveltets_Render<TTag extends ElementType = typeof DEFAULT_LIST_T
9
9
  props(): {
10
10
  as?: TTag | undefined;
11
11
  } & (Exclude<keyof import("../utils/types.js").PropsOf<TTag>, ("slot" | "as" | "children" | "class" | "ref") | ListPropsWeControl> extends infer T extends keyof import("../utils/types.js").PropsOf<TTag> ? { [P in T]: import("../utils/types.js").PropsOf<TTag>[P]; } : never) & {
12
- children?: import("svelte").Snippet<[{
13
- slot: ListRenderPropArg;
14
- props: Record<string, any>;
12
+ children?: import("svelte").Snippet<[ListRenderPropArg & {
13
+ props?: Record<string, any>;
15
14
  }]> | undefined;
16
15
  class?: string | ((bag: ListRenderPropArg) => string) | null | undefined;
17
16
  ref?: HTMLElement;