@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.
- package/dist/button/Button.svelte +84 -54
- package/dist/button/Button.svelte.d.ts +2 -3
- package/dist/checkbox/Checkbox.svelte +173 -120
- package/dist/checkbox/Checkbox.svelte.d.ts +2 -3
- package/dist/close-button/CloseButton.svelte +12 -6
- package/dist/close-button/CloseButton.svelte.d.ts +7 -8
- package/dist/combobox/Combobox.svelte +50 -3
- package/dist/data-interactive/DataInteractive.svelte +55 -29
- package/dist/data-interactive/DataInteractive.svelte.d.ts +2 -3
- package/dist/description/Description.svelte +31 -21
- package/dist/description/Description.svelte.d.ts +1 -2
- package/dist/dialog/Dialog.svelte +320 -228
- package/dist/dialog/Dialog.svelte.d.ts +2 -3
- package/dist/dialog/DialogBackdrop.svelte +30 -13
- package/dist/dialog/DialogBackdrop.svelte.d.ts +2 -3
- package/dist/dialog/DialogPanel.svelte +49 -26
- package/dist/dialog/DialogPanel.svelte.d.ts +2 -3
- package/dist/dialog/DialogTitle.svelte +38 -23
- package/dist/dialog/DialogTitle.svelte.d.ts +2 -3
- package/dist/field/Field.svelte +47 -25
- package/dist/field/Field.svelte.d.ts +1 -2
- package/dist/fieldset/Fieldset.svelte +50 -29
- package/dist/fieldset/Fieldset.svelte.d.ts +1 -2
- package/dist/focus-trap/FocusTrap.svelte +419 -283
- package/dist/focus-trap/FocusTrap.svelte.d.ts +1 -2
- package/dist/input/Input.svelte +84 -53
- package/dist/input/Input.svelte.d.ts +2 -3
- package/dist/internal/FloatingProvider.svelte +14 -9
- package/dist/internal/FocusSentinel.svelte +16 -8
- package/dist/internal/ForcePortalRoot.svelte +7 -3
- package/dist/internal/FormFields.svelte +47 -34
- package/dist/internal/FormFieldsProvider.svelte +9 -5
- package/dist/internal/FormResolver.svelte +20 -15
- package/dist/internal/Hidden.svelte +50 -29
- package/dist/internal/Hidden.svelte.d.ts +1 -2
- package/dist/internal/MainTreeProvider.svelte +89 -36
- package/dist/internal/Portal.svelte +18 -14
- package/dist/internal/floating.svelte.d.ts +2 -3
- package/dist/internal/floating.svelte.js +0 -1
- package/dist/label/Label.svelte +93 -58
- package/dist/label/Label.svelte.d.ts +1 -2
- package/dist/legend/Legend.svelte +12 -3
- package/dist/listbox/Listbox.svelte +525 -387
- package/dist/listbox/Listbox.svelte.d.ts +2 -3
- package/dist/listbox/ListboxButton.svelte +173 -127
- package/dist/listbox/ListboxButton.svelte.d.ts +2 -3
- package/dist/listbox/ListboxOption.svelte +170 -129
- package/dist/listbox/ListboxOption.svelte.d.ts +2 -3
- package/dist/listbox/ListboxOptions.svelte +400 -304
- package/dist/listbox/ListboxOptions.svelte.d.ts +2 -3
- package/dist/listbox/ListboxSelectedOption.svelte +38 -15
- package/dist/listbox/ListboxSelectedOption.svelte.d.ts +1 -2
- package/dist/menu/Menu.svelte +77 -51
- package/dist/menu/Menu.svelte.d.ts +2 -4
- package/dist/menu/MenuButton.svelte +157 -117
- package/dist/menu/MenuButton.svelte.d.ts +2 -3
- package/dist/menu/MenuHeading.svelte +32 -14
- package/dist/menu/MenuHeading.svelte.d.ts +1 -2
- package/dist/menu/MenuItem.svelte +142 -107
- package/dist/menu/MenuItem.svelte.d.ts +2 -3
- package/dist/menu/MenuItems.svelte +301 -229
- package/dist/menu/MenuItems.svelte.d.ts +2 -3
- package/dist/menu/MenuSection.svelte +24 -9
- package/dist/menu/MenuSection.svelte.d.ts +1 -2
- package/dist/menu/MenuSeparator.svelte +17 -4
- package/dist/menu/MenuSeparator.svelte.d.ts +1 -2
- package/dist/popover/Popover.svelte +216 -150
- package/dist/popover/Popover.svelte.d.ts +2 -3
- package/dist/popover/PopoverBackdrop.svelte +67 -41
- package/dist/popover/PopoverBackdrop.svelte.d.ts +2 -3
- package/dist/popover/PopoverButton.svelte +292 -212
- package/dist/popover/PopoverButton.svelte.d.ts +2 -3
- package/dist/popover/PopoverGroup.svelte +62 -35
- package/dist/popover/PopoverGroup.svelte.d.ts +1 -2
- package/dist/popover/PopoverPanel.svelte +311 -229
- package/dist/popover/PopoverPanel.svelte.d.ts +2 -3
- package/dist/portal/InternalPortal.svelte +141 -85
- package/dist/portal/InternalPortal.svelte.d.ts +1 -2
- package/dist/portal/Portal.svelte +5 -2
- package/dist/portal/PortalGroup.svelte +30 -9
- package/dist/portal/PortalGroup.svelte.d.ts +1 -2
- package/dist/select/Select.svelte +98 -68
- package/dist/select/Select.svelte.d.ts +2 -3
- package/dist/switch/Switch.svelte +179 -132
- package/dist/switch/Switch.svelte.d.ts +2 -3
- package/dist/switch/SwitchGroup.svelte +44 -31
- package/dist/switch/SwitchGroup.svelte.d.ts +1 -2
- package/dist/tabs/Tab.svelte +194 -142
- package/dist/tabs/Tab.svelte.d.ts +2 -3
- package/dist/tabs/TabGroup.svelte +86 -56
- package/dist/tabs/TabGroup.svelte.d.ts +2 -3
- package/dist/tabs/TabList.svelte +31 -11
- package/dist/tabs/TabList.svelte.d.ts +2 -3
- package/dist/tabs/TabPanel.svelte +67 -42
- package/dist/tabs/TabPanel.svelte.d.ts +2 -3
- package/dist/tabs/TabPanels.svelte +18 -7
- package/dist/tabs/TabPanels.svelte.d.ts +2 -3
- package/dist/textarea/Textarea.svelte +84 -53
- package/dist/textarea/Textarea.svelte.d.ts +2 -3
- package/dist/transition/InternalTransitionChild.svelte +259 -170
- package/dist/transition/InternalTransitionChild.svelte.d.ts +2 -3
- package/dist/transition/Transition.svelte +96 -66
- package/dist/transition/Transition.svelte.d.ts +2 -3
- package/dist/transition/TransitionChild.svelte +31 -11
- package/dist/transition/TransitionChild.svelte.d.ts +2 -3
- package/dist/utils/DisabledProvider.svelte +7 -3
- package/dist/utils/ElementOrComponent.svelte +46 -23
- package/dist/utils/ElementOrComponent.svelte.d.ts +8 -10
- package/dist/utils/Generic.svelte +30 -19
- package/dist/utils/Generic.svelte.d.ts +6 -7
- package/dist/utils/StableCollection.svelte +54 -36
- package/dist/utils/floating-ui/svelte/components/FloatingNode.svelte +27 -12
- package/dist/utils/floating-ui/svelte/components/FloatingTree.svelte +88 -44
- package/dist/utils/types.d.ts +4 -5
- package/package.json +1 -1
|
@@ -27,9 +27,8 @@ declare class __sveltets_Render<TTag extends ElementType = typeof DEFAULT_LISTBO
|
|
|
27
27
|
props(): {
|
|
28
28
|
as?: TTag | undefined;
|
|
29
29
|
} & (Exclude<keyof import("../utils/types.js").PropsOf<TTag>, "form" | ("slot" | "as" | "children" | "class" | "ref") | "invalid" | "disabled" | "value" | "name" | "onchange" | "multiple" | "__demoMode" | "horizontal" | "defaultValue" | "by" | "closeOnSelect"> extends infer T extends keyof import("../utils/types.js").PropsOf<TTag> ? { [P in T]: import("../utils/types.js").PropsOf<TTag>[P]; } : never) & {
|
|
30
|
-
children?: Snippet<[{
|
|
31
|
-
|
|
32
|
-
props: Record<string, any>;
|
|
30
|
+
children?: Snippet<[ListboxRenderPropArg<TType> & {
|
|
31
|
+
props?: Record<string, any>;
|
|
33
32
|
}]> | undefined;
|
|
34
33
|
class?: string | ((bag: ListboxRenderPropArg<TType>) => string) | null | undefined;
|
|
35
34
|
ref?: HTMLElement;
|
|
@@ -1,139 +1,185 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import { useId } from "../hooks/use-id.js"
|
|
3
|
+
import { useProvidedId } from "../utils/id.js"
|
|
4
|
+
import type { ElementType, Props, PropsOf } from "../utils/types.js"
|
|
5
|
+
import { ListboxStates, useActions, useData } from "./Listbox.svelte"
|
|
6
|
+
import { attemptSubmit } from "../utils/form.js"
|
|
7
|
+
import { Focus } from "../utils/calculate-active-index.js"
|
|
8
|
+
import { useFocusRing } from "../hooks/use-focus-ring.svelte.js"
|
|
9
|
+
import { useActivePress } from "../hooks/use-active-press.svelte.js"
|
|
10
|
+
import { useResolveButtonType } from "../hooks/use-resolve-button-type.svelte.js"
|
|
11
|
+
import { useFloatingReference, useFloatingReferenceProps } from "../internal/floating.svelte.js"
|
|
12
|
+
import { stateFromSlot } from "../utils/state.js"
|
|
13
|
+
import type { Snippet } from "svelte"
|
|
14
|
+
import { useLabelledBy } from "../label/context.svelte.js"
|
|
15
|
+
import { useDescribedBy } from "../description/context.svelte.js"
|
|
16
|
+
import { useHover } from "../hooks/use-hover.svelte.js"
|
|
17
|
+
import { mergeProps } from "../utils/render.js"
|
|
18
|
+
import ElementOrComponent from "../utils/ElementOrComponent.svelte"
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} = $props();
|
|
30
|
-
const { setReference } = useFloatingReference();
|
|
31
|
-
const { getReferenceProps: getFloatingReferenceProps } = useFloatingReferenceProps();
|
|
32
|
-
$effect(() => {
|
|
33
|
-
data.buttonElement = ref || null;
|
|
34
|
-
setReference(ref);
|
|
35
|
-
});
|
|
36
|
-
const disabled = $derived(data.disabled || ownDisabled);
|
|
37
|
-
const handleKeyDown = (event) => {
|
|
38
|
-
switch (event.key) {
|
|
39
|
-
case "Enter":
|
|
40
|
-
if (event.currentTarget) attemptSubmit(event.currentTarget);
|
|
41
|
-
break;
|
|
42
|
-
case " ":
|
|
43
|
-
case "ArrowDown":
|
|
44
|
-
event.preventDefault();
|
|
45
|
-
actions.openListbox();
|
|
46
|
-
if (!data.value) actions.goToOption(Focus.First);
|
|
47
|
-
break;
|
|
48
|
-
case "ArrowUp":
|
|
49
|
-
event.preventDefault();
|
|
50
|
-
actions.openListbox();
|
|
51
|
-
if (!data.value) actions.goToOption(Focus.Last);
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
const handleKeyUp = (event) => {
|
|
56
|
-
switch (event.key) {
|
|
57
|
-
case " ":
|
|
58
|
-
event.preventDefault();
|
|
59
|
-
break;
|
|
20
|
+
const DEFAULT_BUTTON_TAG = "button" as const
|
|
21
|
+
type ButtonRenderPropArg = {
|
|
22
|
+
disabled: boolean
|
|
23
|
+
invalid: boolean
|
|
24
|
+
hover: boolean
|
|
25
|
+
focus: boolean
|
|
26
|
+
autofocus: boolean
|
|
27
|
+
open: boolean
|
|
28
|
+
active: boolean
|
|
29
|
+
value: any
|
|
60
30
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
};
|
|
71
|
-
const handleKeyPress = (event) => event.preventDefault();
|
|
72
|
-
const labelledBy = useLabelledBy();
|
|
73
|
-
const describedBy = useDescribedBy();
|
|
74
|
-
const { isHovered: hover, hoverProps } = $derived(
|
|
75
|
-
useHover({
|
|
76
|
-
get disabled() {
|
|
77
|
-
return disabled;
|
|
31
|
+
type ButtonPropsWeControl = "aria-controls" | "aria-expanded" | "aria-haspopup" | "aria-labelledby" | "disabled"
|
|
32
|
+
|
|
33
|
+
export type ListboxButtonProps<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG> = Props<
|
|
34
|
+
TTag,
|
|
35
|
+
ButtonRenderPropArg,
|
|
36
|
+
ButtonPropsWeControl,
|
|
37
|
+
{
|
|
38
|
+
autofocus?: boolean
|
|
39
|
+
disabled?: boolean
|
|
78
40
|
}
|
|
41
|
+
>
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_BUTTON_TAG">
|
|
45
|
+
const data = useData("ListboxButton")
|
|
46
|
+
const actions = useActions("ListboxButton")
|
|
47
|
+
|
|
48
|
+
const internalId = useId()
|
|
49
|
+
const providedId = useProvidedId()
|
|
50
|
+
let {
|
|
51
|
+
ref = $bindable(),
|
|
52
|
+
id = (providedId || `headlessui-listbox-button-${internalId}`) as PropsOf<TTag>["id"],
|
|
53
|
+
disabled: ownDisabled = false,
|
|
54
|
+
autofocus = false,
|
|
55
|
+
...theirProps
|
|
56
|
+
}: { as?: TTag } & ListboxButtonProps<TTag> = $props()
|
|
57
|
+
const { setReference } = useFloatingReference()
|
|
58
|
+
const { getReferenceProps: getFloatingReferenceProps } = useFloatingReferenceProps()
|
|
59
|
+
$effect(() => {
|
|
60
|
+
data.buttonElement = ref || null
|
|
61
|
+
setReference(ref)
|
|
79
62
|
})
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
63
|
+
|
|
64
|
+
const disabled = $derived(data.disabled || ownDisabled)
|
|
65
|
+
|
|
66
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
67
|
+
switch (event.key) {
|
|
68
|
+
// Ref: https://www.w3.org/WAI/ARIA/apg/patterns/menubutton/#keyboard-interaction-13
|
|
69
|
+
|
|
70
|
+
case "Enter":
|
|
71
|
+
if (event.currentTarget) attemptSubmit(event.currentTarget as HTMLElement)
|
|
72
|
+
break
|
|
73
|
+
|
|
74
|
+
case " ":
|
|
75
|
+
case "ArrowDown":
|
|
76
|
+
event.preventDefault()
|
|
77
|
+
actions.openListbox()
|
|
78
|
+
if (!data.value) actions.goToOption(Focus.First)
|
|
79
|
+
break
|
|
80
|
+
|
|
81
|
+
case "ArrowUp":
|
|
82
|
+
event.preventDefault()
|
|
83
|
+
actions.openListbox()
|
|
84
|
+
if (!data.value) actions.goToOption(Focus.Last)
|
|
85
|
+
break
|
|
85
86
|
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const handleKeyUp = (event: KeyboardEvent) => {
|
|
90
|
+
switch (event.key) {
|
|
91
|
+
case " ":
|
|
92
|
+
// Required for firefox, event.preventDefault() in handleKeyDown for
|
|
93
|
+
// the Space key doesn't cancel the handleKeyUp, which in turn
|
|
94
|
+
// triggers a *click*.
|
|
95
|
+
event.preventDefault()
|
|
96
|
+
break
|
|
92
97
|
}
|
|
93
|
-
})
|
|
94
|
-
);
|
|
95
|
-
const slot = $derived({
|
|
96
|
-
open: data.listboxState === ListboxStates.Open,
|
|
97
|
-
active: active || data.listboxState === ListboxStates.Open,
|
|
98
|
-
disabled,
|
|
99
|
-
invalid: data.invalid,
|
|
100
|
-
value: data.value,
|
|
101
|
-
hover,
|
|
102
|
-
focus,
|
|
103
|
-
autofocus: autofocus ?? false
|
|
104
|
-
});
|
|
105
|
-
const buttonType = useResolveButtonType({
|
|
106
|
-
get props() {
|
|
107
|
-
return { type: theirProps.type, as: theirProps.as };
|
|
108
|
-
},
|
|
109
|
-
get ref() {
|
|
110
|
-
return { current: data.buttonElement };
|
|
111
98
|
}
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
{
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
99
|
+
|
|
100
|
+
const handleClick = (event: MouseEvent) => {
|
|
101
|
+
//if (isDisabledReactIssue7711(event.currentTarget)) return event.preventDefault()
|
|
102
|
+
if (data.listboxState === ListboxStates.Open) {
|
|
103
|
+
actions.closeListbox()
|
|
104
|
+
data.buttonElement?.focus({ preventScroll: true })
|
|
105
|
+
} else {
|
|
106
|
+
event.preventDefault()
|
|
107
|
+
actions.openListbox()
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// This is needed so that we can "cancel" the click event when we use the `Enter` key on a button.
|
|
112
|
+
const handleKeyPress = (event: KeyboardEvent) => event.preventDefault()
|
|
113
|
+
|
|
114
|
+
const labelledBy = useLabelledBy()
|
|
115
|
+
const describedBy = useDescribedBy()
|
|
116
|
+
|
|
117
|
+
const { isHovered: hover, hoverProps } = $derived(
|
|
118
|
+
useHover({
|
|
119
|
+
get disabled() {
|
|
120
|
+
return disabled
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
)
|
|
124
|
+
const { pressed: active, pressProps } = $derived(
|
|
125
|
+
useActivePress({
|
|
126
|
+
get disabled() {
|
|
127
|
+
return disabled
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
)
|
|
131
|
+
const { isFocusVisible: focus, focusProps } = $derived(
|
|
132
|
+
useFocusRing({
|
|
133
|
+
get autofocus() {
|
|
134
|
+
return autofocus
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
const slot = $derived({
|
|
140
|
+
open: data.listboxState === ListboxStates.Open,
|
|
141
|
+
active: active || data.listboxState === ListboxStates.Open,
|
|
142
|
+
disabled,
|
|
143
|
+
invalid: data.invalid,
|
|
144
|
+
value: data.value,
|
|
145
|
+
hover,
|
|
146
|
+
focus,
|
|
147
|
+
autofocus: autofocus ?? false,
|
|
148
|
+
} satisfies ButtonRenderPropArg)
|
|
149
|
+
|
|
150
|
+
const buttonType = useResolveButtonType({
|
|
151
|
+
get props() {
|
|
152
|
+
return { type: theirProps.type, as: theirProps.as }
|
|
153
|
+
},
|
|
154
|
+
get ref() {
|
|
155
|
+
return { current: data.buttonElement }
|
|
130
156
|
},
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const ourProps = $derived(
|
|
160
|
+
mergeProps(
|
|
161
|
+
{
|
|
162
|
+
...getFloatingReferenceProps(),
|
|
163
|
+
id,
|
|
164
|
+
type: buttonType.type,
|
|
165
|
+
"aria-haspopup": "listbox",
|
|
166
|
+
"aria-controls": data.optionsElement?.id,
|
|
167
|
+
"aria-expanded": data.listboxState === ListboxStates.Open,
|
|
168
|
+
"aria-labelledby": labelledBy.value,
|
|
169
|
+
"aria-describedby": describedBy.value,
|
|
170
|
+
disabled: disabled || undefined,
|
|
171
|
+
autofocus,
|
|
172
|
+
onkeydown: handleKeyDown,
|
|
173
|
+
onkeyup: handleKeyUp,
|
|
174
|
+
onkeypress: handleKeyPress,
|
|
175
|
+
onclick: handleClick,
|
|
176
|
+
},
|
|
177
|
+
focusProps,
|
|
178
|
+
hoverProps,
|
|
179
|
+
pressProps,
|
|
180
|
+
stateFromSlot(slot)
|
|
181
|
+
)
|
|
135
182
|
)
|
|
136
|
-
);
|
|
137
183
|
</script>
|
|
138
184
|
|
|
139
185
|
<ElementOrComponent {ourProps} {theirProps} {slot} defaultTag={DEFAULT_BUTTON_TAG} name="ListboxButton" bind:ref />
|
|
@@ -20,9 +20,8 @@ declare class __sveltets_Render<TTag extends ElementType = typeof DEFAULT_BUTTON
|
|
|
20
20
|
props(): {
|
|
21
21
|
as?: TTag | undefined;
|
|
22
22
|
} & (Exclude<keyof PropsOf<TTag>, ("slot" | "as" | "children" | "class" | "ref") | "autofocus" | ButtonPropsWeControl> extends infer T extends keyof PropsOf<TTag> ? { [P in T]: PropsOf<TTag>[P]; } : never) & {
|
|
23
|
-
children?: Snippet<[{
|
|
24
|
-
|
|
25
|
-
props: Record<string, any>;
|
|
23
|
+
children?: Snippet<[ButtonRenderPropArg & {
|
|
24
|
+
props?: Record<string, any>;
|
|
26
25
|
}]> | undefined;
|
|
27
26
|
class?: string | ((bag: ButtonRenderPropArg) => string) | null | undefined;
|
|
28
27
|
ref?: HTMLElement;
|
|
@@ -1,138 +1,179 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { ElementType, Props } from "../utils/types.js"
|
|
3
|
+
|
|
4
|
+
const DEFAULT_OPTION_TAG = "div" as const
|
|
5
|
+
type OptionRenderPropArg = {
|
|
6
|
+
/** @deprecated use `focus` instead */
|
|
7
|
+
active: boolean
|
|
8
|
+
focus: boolean
|
|
9
|
+
selected: boolean
|
|
10
|
+
disabled: boolean
|
|
11
|
+
|
|
12
|
+
selectedOption: boolean
|
|
13
|
+
}
|
|
14
|
+
type OptionPropsWeControl = "aria-disabled" | "aria-selected" | "role" | "tabIndex"
|
|
15
|
+
|
|
16
|
+
export type ListboxOptionProps<TTag extends ElementType = typeof DEFAULT_OPTION_TAG, TType = string> = Props<
|
|
17
|
+
TTag,
|
|
18
|
+
OptionRenderPropArg,
|
|
19
|
+
OptionPropsWeControl,
|
|
20
|
+
{
|
|
21
|
+
id?: string
|
|
22
|
+
disabled?: boolean
|
|
23
|
+
value: TType
|
|
24
|
+
}
|
|
25
|
+
>
|
|
2
26
|
</script>
|
|
3
27
|
|
|
4
|
-
<script lang="ts" generics="TType, TTag extends ElementType = typeof DEFAULT_OPTION_TAG">
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
<script lang="ts" generics="TType, TTag extends ElementType = typeof DEFAULT_OPTION_TAG">
|
|
29
|
+
import { useId } from "../hooks/use-id.js"
|
|
30
|
+
import {
|
|
31
|
+
ActivationTrigger,
|
|
32
|
+
ListboxStates,
|
|
33
|
+
useActions,
|
|
34
|
+
useData,
|
|
35
|
+
ValueMode,
|
|
36
|
+
type ListboxOptionDataRef,
|
|
37
|
+
} from "./Listbox.svelte"
|
|
38
|
+
import { disposables } from "../utils/disposables.js"
|
|
39
|
+
import { Focus } from "../utils/calculate-active-index.js"
|
|
40
|
+
import { getContext, onMount, type Snippet } from "svelte"
|
|
41
|
+
import { useTextValue } from "../hooks/use-text-value.svelte.js"
|
|
42
|
+
import { useTrackedPointer } from "../hooks/use-tracked-pointer.js"
|
|
43
|
+
import { stateFromSlot } from "../utils/state.js"
|
|
44
|
+
import ElementOrComponent from "../utils/ElementOrComponent.svelte"
|
|
45
|
+
|
|
46
|
+
const internalId = useId()
|
|
47
|
+
let {
|
|
48
|
+
ref = $bindable(),
|
|
49
|
+
id = `headlessui-listbox-option-${internalId}`,
|
|
50
|
+
disabled = false,
|
|
51
|
+
value,
|
|
52
|
+
...theirProps
|
|
53
|
+
}: { as?: TTag } & ListboxOptionProps<TTag, TType> = $props()
|
|
54
|
+
const usedInSelectedOption = getContext<boolean>("SelectedOptionContext") === true
|
|
55
|
+
const data = useData("ListboxOption")
|
|
56
|
+
const actions = useActions("ListboxOption")
|
|
57
|
+
|
|
58
|
+
const { activeOptionIndex, options } = $derived(data)
|
|
59
|
+
|
|
60
|
+
const active = $derived(activeOptionIndex !== null ? options[activeOptionIndex].id === id : false)
|
|
61
|
+
|
|
62
|
+
const selected = $derived(data.isSelected(value))
|
|
63
|
+
const getTextValue = useTextValue({
|
|
64
|
+
get element() {
|
|
65
|
+
return ref as HTMLElement
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
const bag: ListboxOptionDataRef<TType>["current"] = $derived({
|
|
69
|
+
disabled,
|
|
70
|
+
value,
|
|
71
|
+
domRef: { current: ref || null },
|
|
72
|
+
get textValue() {
|
|
73
|
+
return getTextValue()
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
$effect(() => {
|
|
78
|
+
if (usedInSelectedOption) return
|
|
79
|
+
if (!ref) {
|
|
80
|
+
data.listElements.delete(id)
|
|
81
|
+
} else {
|
|
82
|
+
data.listElements.set(id, ref)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return () => {
|
|
86
|
+
if (ref) data.listElements.delete(id)
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
$effect(() => {
|
|
91
|
+
if (data.__demoMode) return
|
|
92
|
+
if (data.listboxState !== ListboxStates.Open) return
|
|
93
|
+
if (!active) return
|
|
94
|
+
if (data.activationTrigger === ActivationTrigger.Pointer) return
|
|
95
|
+
return disposables().requestAnimationFrame(() => {
|
|
96
|
+
;(ref as HTMLElement)?.scrollIntoView?.({ block: "nearest" })
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
onMount(() => {
|
|
101
|
+
if (usedInSelectedOption) return
|
|
102
|
+
return actions.registerOption(id, {
|
|
103
|
+
get current() {
|
|
104
|
+
return bag
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
const handleClick = (event: { preventDefault: Function }) => {
|
|
110
|
+
if (disabled) return event.preventDefault()
|
|
111
|
+
actions.onChange(value)
|
|
112
|
+
if (data.closeOnSelect === true || (data.closeOnSelect === undefined && data.mode === ValueMode.Single)) {
|
|
113
|
+
actions.closeListbox()
|
|
114
|
+
data.buttonElement?.focus({ preventScroll: true })
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const handleFocus = () => {
|
|
119
|
+
if (disabled) return actions.goToOption(Focus.Nothing)
|
|
120
|
+
actions.goToOption(Focus.Specific, id)
|
|
36
121
|
}
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
122
|
+
|
|
123
|
+
const pointer = useTrackedPointer()
|
|
124
|
+
|
|
125
|
+
const handleEnter = (evt: PointerEvent) => {
|
|
126
|
+
pointer.update(evt)
|
|
127
|
+
if (disabled) return
|
|
128
|
+
if (active) return
|
|
129
|
+
actions.goToOption(Focus.Specific, id, ActivationTrigger.Pointer)
|
|
44
130
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
data.listElements.set(id, ref);
|
|
131
|
+
|
|
132
|
+
const handleMove = (evt: PointerEvent) => {
|
|
133
|
+
if (!pointer.wasMoved(evt)) return
|
|
134
|
+
if (disabled) return
|
|
135
|
+
if (active) return
|
|
136
|
+
actions.goToOption(Focus.Specific, id, ActivationTrigger.Pointer)
|
|
52
137
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (data.listboxState !== ListboxStates.Open) return;
|
|
60
|
-
if (!active) return;
|
|
61
|
-
if (data.activationTrigger === ActivationTrigger.Pointer) return;
|
|
62
|
-
return disposables().requestAnimationFrame(() => {
|
|
63
|
-
;
|
|
64
|
-
ref?.scrollIntoView?.({ block: "nearest" });
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
onMount(() => {
|
|
68
|
-
if (usedInSelectedOption) return;
|
|
69
|
-
return actions.registerOption(id, {
|
|
70
|
-
get current() {
|
|
71
|
-
return bag;
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
const handleClick = (event) => {
|
|
76
|
-
if (disabled) return event.preventDefault();
|
|
77
|
-
actions.onChange(value);
|
|
78
|
-
if (data.closeOnSelect === true || data.closeOnSelect === void 0 && data.mode === ValueMode.Single) {
|
|
79
|
-
actions.closeListbox();
|
|
80
|
-
data.buttonElement?.focus({ preventScroll: true });
|
|
138
|
+
|
|
139
|
+
const handleLeave = (evt: PointerEvent) => {
|
|
140
|
+
if (!pointer.wasMoved(evt)) return
|
|
141
|
+
if (disabled) return
|
|
142
|
+
if (!active) return
|
|
143
|
+
actions.goToOption(Focus.Nothing)
|
|
81
144
|
}
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
!usedInSelectedOption ? {
|
|
115
|
-
id,
|
|
116
|
-
role: "option",
|
|
117
|
-
tabIndex: disabled === true ? void 0 : -1,
|
|
118
|
-
"aria-disabled": disabled === true ? true : void 0,
|
|
119
|
-
// According to the WAI-ARIA best practices, we should use aria-checked for
|
|
120
|
-
// multi-select,but Voice-Over disagrees. So we use aria-checked instead for
|
|
121
|
-
// both single and multi-select.
|
|
122
|
-
"aria-selected": selected,
|
|
123
|
-
disabled: void 0,
|
|
124
|
-
// Never forward the `disabled` prop
|
|
125
|
-
onclick: handleClick,
|
|
126
|
-
onfocus: handleFocus,
|
|
127
|
-
onpointerenter: handleEnter,
|
|
128
|
-
onmouseenter: handleEnter,
|
|
129
|
-
onpointermove: handleMove,
|
|
130
|
-
onmousemove: handleMove,
|
|
131
|
-
onpointerleave: handleLeave,
|
|
132
|
-
onmouseleave: handleLeave,
|
|
133
|
-
...stateFromSlot(slot)
|
|
134
|
-
} : {}
|
|
135
|
-
);
|
|
145
|
+
|
|
146
|
+
const slot = $derived({
|
|
147
|
+
active,
|
|
148
|
+
focus: active,
|
|
149
|
+
selected,
|
|
150
|
+
disabled,
|
|
151
|
+
selectedOption: selected && usedInSelectedOption,
|
|
152
|
+
} satisfies OptionRenderPropArg)
|
|
153
|
+
const ourProps = $derived(
|
|
154
|
+
!usedInSelectedOption
|
|
155
|
+
? {
|
|
156
|
+
id,
|
|
157
|
+
role: "option",
|
|
158
|
+
tabIndex: disabled === true ? undefined : -1,
|
|
159
|
+
"aria-disabled": disabled === true ? true : undefined,
|
|
160
|
+
// According to the WAI-ARIA best practices, we should use aria-checked for
|
|
161
|
+
// multi-select,but Voice-Over disagrees. So we use aria-checked instead for
|
|
162
|
+
// both single and multi-select.
|
|
163
|
+
"aria-selected": selected,
|
|
164
|
+
disabled: undefined, // Never forward the `disabled` prop
|
|
165
|
+
onclick: handleClick,
|
|
166
|
+
onfocus: handleFocus,
|
|
167
|
+
onpointerenter: handleEnter,
|
|
168
|
+
onmouseenter: handleEnter,
|
|
169
|
+
onpointermove: handleMove,
|
|
170
|
+
onmousemove: handleMove,
|
|
171
|
+
onpointerleave: handleLeave,
|
|
172
|
+
onmouseleave: handleLeave,
|
|
173
|
+
...stateFromSlot(slot),
|
|
174
|
+
}
|
|
175
|
+
: {}
|
|
176
|
+
)
|
|
136
177
|
</script>
|
|
137
178
|
|
|
138
179
|
{#if selected || !usedInSelectedOption}
|
|
@@ -19,9 +19,8 @@ declare class __sveltets_Render<TType, TTag extends ElementType = typeof DEFAULT
|
|
|
19
19
|
props(): {
|
|
20
20
|
as?: TTag | undefined;
|
|
21
21
|
} & (Exclude<keyof import("../utils/types.js").PropsOf<TTag>, ("slot" | "as" | "children" | "class" | "ref") | "disabled" | "value" | "id" | OptionPropsWeControl> extends infer T extends keyof import("../utils/types.js").PropsOf<TTag> ? { [P in T]: import("../utils/types.js").PropsOf<TTag>[P]; } : never) & {
|
|
22
|
-
children?: Snippet<[{
|
|
23
|
-
|
|
24
|
-
props: Record<string, any>;
|
|
22
|
+
children?: Snippet<[OptionRenderPropArg & {
|
|
23
|
+
props?: Record<string, any>;
|
|
25
24
|
}]> | undefined;
|
|
26
25
|
class?: string | ((bag: OptionRenderPropArg) => string) | null | undefined;
|
|
27
26
|
ref?: HTMLElement;
|