@pzerelles/headlessui-svelte 2.1.2-next.3 → 2.1.2-next.5
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/checkbox/Checkbox.svelte +174 -120
- package/dist/close-button/CloseButton.svelte +12 -6
- package/dist/combobox/Combobox.svelte +50 -3
- package/dist/data-interactive/DataInteractive.svelte +57 -29
- package/dist/description/Description.svelte +32 -21
- package/dist/dialog/Dialog.svelte +69 -34
- package/dist/dialog/DialogBackdrop.svelte +29 -12
- package/dist/dialog/DialogPanel.svelte +49 -26
- package/dist/dialog/DialogTitle.svelte +38 -23
- package/dist/dialog/InternalDialog.svelte +263 -202
- package/dist/field/Field.svelte +49 -26
- package/dist/fieldset/Fieldset.svelte +50 -29
- package/dist/focus-trap/FocusTrap.svelte +419 -290
- package/dist/focus-trap/FocusTrap.svelte.d.ts +2 -14
- package/dist/focus-trap/FocusTrapFeatures.d.ts +14 -0
- package/dist/focus-trap/FocusTrapFeatures.js +15 -0
- package/dist/input/Input.svelte +85 -53
- package/dist/internal/FocusSentinel.svelte +16 -8
- package/dist/internal/ForcePortalRoot.svelte +7 -3
- package/dist/internal/FormFields.svelte +31 -20
- package/dist/internal/FormResolver.svelte +20 -15
- package/dist/internal/Hidden.svelte +44 -27
- package/dist/internal/Hidden.svelte.d.ts +2 -5
- package/dist/internal/HiddenFeatures.d.ts +5 -0
- package/dist/internal/HiddenFeatures.js +9 -0
- package/dist/internal/HoistFormFields.svelte +7 -4
- package/dist/internal/MainTreeProvider.svelte +89 -36
- package/dist/internal/Portal.svelte +18 -14
- package/dist/label/Label.svelte +91 -57
- package/dist/legend/Legend.svelte +18 -3
- package/dist/listbox/Listbox.svelte +588 -409
- package/dist/listbox/Listbox.svelte.d.ts +2 -12
- package/dist/listbox/ListboxButton.svelte +176 -127
- package/dist/listbox/ListboxOption.svelte +166 -125
- package/dist/listbox/ListboxOptions.svelte +340 -244
- package/dist/listbox/ListboxSelectedOption.svelte +38 -15
- package/dist/listbox/ListboxStates.d.ts +12 -0
- package/dist/listbox/ListboxStates.js +15 -0
- package/dist/menu/Menu.svelte +307 -218
- package/dist/menu/MenuButton.svelte +157 -115
- package/dist/menu/MenuHeading.svelte +34 -14
- package/dist/menu/MenuItem.svelte +145 -107
- package/dist/menu/MenuItems.svelte +298 -224
- package/dist/menu/MenuSection.svelte +26 -9
- package/dist/menu/MenuSeparator.svelte +20 -4
- package/dist/portal/InternalPortal.svelte +141 -85
- package/dist/portal/Portal.svelte +5 -2
- package/dist/portal/PortalGroup.svelte +30 -9
- package/dist/switch/Switch.svelte +179 -122
- package/dist/switch/Switch.svelte.d.ts +4 -4
- package/dist/switch/SwitchGroup.svelte +44 -31
- package/dist/tabs/Tab.svelte +195 -143
- package/dist/tabs/TabGroup.svelte +292 -205
- package/dist/tabs/TabList.svelte +31 -11
- package/dist/tabs/TabPanel.svelte +68 -43
- package/dist/tabs/TabPanels.svelte +18 -7
- package/dist/textarea/Textarea.svelte +83 -53
- package/dist/transition/InternalTransitionChild.svelte +259 -170
- package/dist/transition/Transition.svelte +96 -66
- package/dist/transition/TransitionChild.svelte +31 -11
- package/dist/utils/ElementOrComponent.svelte +44 -23
- package/dist/utils/Generic.svelte +29 -17
- package/dist/utils/StableCollection.svelte +54 -36
- package/package.json +10 -10
|
@@ -1,95 +1,151 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
target = groupTarget;
|
|
32
|
-
});
|
|
33
|
-
return {
|
|
34
|
-
get target() {
|
|
35
|
-
return target;
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import { usePortalRoot } from "../internal/portal-force-root.svelte.js"
|
|
3
|
+
import { getOwnerDocument } from "../utils/owner.js"
|
|
4
|
+
import { getContext, onMount, setContext } from "svelte"
|
|
5
|
+
import { env } from "../utils/env.js"
|
|
6
|
+
import type { ElementType, Props } from "../utils/types.js"
|
|
7
|
+
import type { PortalGroupContext } from "./PortalGroup.svelte"
|
|
8
|
+
|
|
9
|
+
function usePortalTarget(options: { element: HTMLElement | null }): { readonly target: HTMLElement | null } {
|
|
10
|
+
const { element } = $derived(options)
|
|
11
|
+
const forceInRoot = usePortalRoot()
|
|
12
|
+
const portalGroupContext = getContext<PortalGroupContext>("PortalGroupContext")
|
|
13
|
+
const { target: groupTarget } = $derived(portalGroupContext ?? {})
|
|
14
|
+
|
|
15
|
+
const ownerDocument = $derived(getOwnerDocument(element))
|
|
16
|
+
|
|
17
|
+
const initialTarget = () => {
|
|
18
|
+
// Group context is used, but still null
|
|
19
|
+
if (!forceInRoot?.force && groupTarget) return groupTarget ?? null
|
|
20
|
+
|
|
21
|
+
// No group context is used, let's create a default portal root
|
|
22
|
+
if (env.isServer) return null
|
|
23
|
+
let existingRoot = ownerDocument?.getElementById("headlessui-portal-root")
|
|
24
|
+
if (existingRoot) return existingRoot
|
|
25
|
+
|
|
26
|
+
if (ownerDocument === null) return null
|
|
27
|
+
|
|
28
|
+
let root = ownerDocument.createElement("div")
|
|
29
|
+
root.setAttribute("id", "headlessui-portal-root")
|
|
30
|
+
return ownerDocument.body.appendChild(root)
|
|
36
31
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
32
|
+
|
|
33
|
+
let target = $state(initialTarget())
|
|
34
|
+
|
|
35
|
+
// Ensure the portal root is always in the DOM
|
|
36
|
+
$effect(() => {
|
|
37
|
+
if (!target) return
|
|
38
|
+
|
|
39
|
+
if (!ownerDocument?.body.contains(target)) {
|
|
40
|
+
ownerDocument?.body.appendChild(target)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
$effect(() => {
|
|
45
|
+
if (forceInRoot?.force) return
|
|
46
|
+
if (!groupTarget) return
|
|
47
|
+
target = groupTarget
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
get target() {
|
|
52
|
+
return target
|
|
53
|
+
},
|
|
57
54
|
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
export const DEFAULT_PORTAL_TAG = "div";
|
|
63
|
-
</script>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// ---
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return ref ?? null;
|
|
59
|
+
type PortalParentContext = {
|
|
60
|
+
register: (portal: HTMLElement) => () => void
|
|
61
|
+
unregister: (portal: HTMLElement) => void
|
|
62
|
+
readonly portals: HTMLElement[]
|
|
70
63
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const parent = getContext("PortalParentContext")
|
|
74
|
-
$
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
64
|
+
|
|
65
|
+
export function useNestedPortals() {
|
|
66
|
+
const parent = getContext<PortalParentContext>("PortalParentContext")
|
|
67
|
+
const portals = $state<HTMLElement[]>([])
|
|
68
|
+
|
|
69
|
+
const register = (portal: HTMLElement) => {
|
|
70
|
+
portals.push(portal)
|
|
71
|
+
if (parent) parent.register(portal)
|
|
72
|
+
return () => unregister(portal)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const unregister = (portal: HTMLElement) => {
|
|
76
|
+
let idx = portals.indexOf(portal)
|
|
77
|
+
if (idx !== -1) portals.splice(idx, 1)
|
|
78
|
+
if (parent) parent.unregister(portal)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const context: PortalParentContext = {
|
|
82
|
+
register,
|
|
83
|
+
unregister,
|
|
84
|
+
get portals() {
|
|
85
|
+
return portals
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
setContext("PortalParentContext", context)
|
|
89
|
+
|
|
90
|
+
return context
|
|
79
91
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
92
|
+
|
|
93
|
+
// ---
|
|
94
|
+
|
|
95
|
+
export const DEFAULT_PORTAL_TAG = "div"
|
|
96
|
+
type PortalRenderPropArg = {}
|
|
97
|
+
type PortalPropsWeControl = never
|
|
98
|
+
|
|
99
|
+
export type PortalProps<TTag extends ElementType = typeof DEFAULT_PORTAL_TAG> = Props<
|
|
100
|
+
TTag,
|
|
101
|
+
PortalRenderPropArg,
|
|
102
|
+
PortalPropsWeControl,
|
|
103
|
+
{
|
|
104
|
+
enabled?: boolean
|
|
105
|
+
}
|
|
106
|
+
>
|
|
107
|
+
</script>
|
|
108
|
+
|
|
109
|
+
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_PORTAL_TAG">
|
|
110
|
+
import ElementOrComponent from "../utils/ElementOrComponent.svelte"
|
|
111
|
+
|
|
112
|
+
let { ref = $bindable(), ...theirProps }: { as?: TTag } & PortalProps<TTag> = $props()
|
|
113
|
+
|
|
114
|
+
const portalTarget = usePortalTarget({
|
|
115
|
+
get element() {
|
|
116
|
+
return ref ?? null
|
|
117
|
+
},
|
|
118
|
+
})
|
|
119
|
+
const { target } = $derived(portalTarget)
|
|
120
|
+
const parent = getContext<PortalParentContext>("PortalParentContext")
|
|
121
|
+
//const ready = useServerHandoffComplete()
|
|
122
|
+
|
|
123
|
+
$effect(() => {
|
|
124
|
+
if (!target || !ref) return
|
|
125
|
+
|
|
126
|
+
// Element already exists in target, always calling target.appendChild(element) will cause a
|
|
127
|
+
// brief unmount/remount.
|
|
128
|
+
if (ref.parentNode !== target) {
|
|
129
|
+
ref.setAttribute("data-headlessui-portal", "")
|
|
130
|
+
target.appendChild(ref)
|
|
87
131
|
}
|
|
88
|
-
|
|
89
|
-
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
onMount(() => {
|
|
135
|
+
if (parent) parent.register(ref!)
|
|
136
|
+
|
|
137
|
+
return () => {
|
|
138
|
+
if (!target || !ref) return
|
|
139
|
+
|
|
140
|
+
if (ref instanceof Node && target.contains(ref)) {
|
|
141
|
+
target.removeChild(ref)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (target.childNodes.length <= 0) {
|
|
145
|
+
target.parentElement?.removeChild(target)
|
|
146
|
+
}
|
|
90
147
|
}
|
|
91
|
-
}
|
|
92
|
-
});
|
|
148
|
+
})
|
|
93
149
|
</script>
|
|
94
150
|
|
|
95
151
|
{#if target}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_PORTAL_TAG">
|
|
2
|
-
|
|
1
|
+
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_PORTAL_TAG">
|
|
2
|
+
import type { ElementType } from "../utils/types.js"
|
|
3
|
+
import InternalPortal, { DEFAULT_PORTAL_TAG, type PortalProps } from "./InternalPortal.svelte"
|
|
4
|
+
|
|
5
|
+
let { ref = $bindable(), enabled = true, ...theirProps }: PortalProps<TTag> = $props()
|
|
3
6
|
</script>
|
|
4
7
|
|
|
5
8
|
{#if enabled}
|
|
@@ -1,14 +1,35 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { ElementType, Props } from "../utils/types.js"
|
|
3
|
+
|
|
4
|
+
const DEFAULT_GROUP_TAG = "svelte:fragment"
|
|
5
|
+
type GroupRenderPropArg = {}
|
|
6
|
+
type GroupPropsWeControl = never
|
|
3
7
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let { ref = $bindable(), target, ...theirProps } = $props();
|
|
7
|
-
setContext("PortalGroupContext", {
|
|
8
|
-
get target() {
|
|
9
|
-
return target;
|
|
8
|
+
export type PortalGroupContext = {
|
|
9
|
+
readonly target: HTMLElement | null
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
|
|
12
|
+
export type PortalGroupProps<TTag extends ElementType = typeof DEFAULT_GROUP_TAG> = Props<
|
|
13
|
+
TTag,
|
|
14
|
+
GroupRenderPropArg,
|
|
15
|
+
GroupPropsWeControl,
|
|
16
|
+
{
|
|
17
|
+
target: HTMLElement | null
|
|
18
|
+
}
|
|
19
|
+
>
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_GROUP_TAG">
|
|
23
|
+
import ElementOrComponent from "../utils/ElementOrComponent.svelte"
|
|
24
|
+
import { setContext } from "svelte"
|
|
25
|
+
|
|
26
|
+
let { ref = $bindable(), target, ...theirProps }: { as?: TTag } & PortalGroupProps<TTag> = $props()
|
|
27
|
+
|
|
28
|
+
setContext("PortalGroupContext", {
|
|
29
|
+
get target() {
|
|
30
|
+
return target
|
|
31
|
+
},
|
|
32
|
+
})
|
|
12
33
|
</script>
|
|
13
34
|
|
|
14
35
|
<ElementOrComponent {theirProps} defaultTag={DEFAULT_GROUP_TAG} name="PortalGroup" bind:ref />
|
|
@@ -1,133 +1,190 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { ElementType, Props, PropsOf } from "../utils/types.js"
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import { useResolveButtonType } from "../hooks/use-resolve-button-type.svelte.js";
|
|
14
|
-
import { useFocusRing } from "../hooks/use-focus-ring.svelte.js";
|
|
15
|
-
import { useHover } from "../hooks/use-hover.svelte.js";
|
|
16
|
-
import { useActivePress } from "../hooks/use-active-press.svelte.js";
|
|
17
|
-
import FormFields from "../internal/FormFields.svelte";
|
|
18
|
-
import ElementOrComponent from "../utils/ElementOrComponent.svelte";
|
|
19
|
-
const internalId = useId();
|
|
20
|
-
let providedId = useProvidedId();
|
|
21
|
-
let { value: providedDisabled } = $derived(useDisabled());
|
|
22
|
-
let {
|
|
23
|
-
ref = $bindable(),
|
|
24
|
-
id: ownId,
|
|
25
|
-
disabled: ownDisabled,
|
|
26
|
-
defaultChecked,
|
|
27
|
-
checked = $bindable(defaultChecked),
|
|
28
|
-
onchange,
|
|
29
|
-
name,
|
|
30
|
-
value,
|
|
31
|
-
form,
|
|
32
|
-
autofocus = false,
|
|
33
|
-
tabIndex,
|
|
34
|
-
...theirProps
|
|
35
|
-
} = $props();
|
|
36
|
-
const id = $derived(ownId || providedId || `headlessui-switch-${internalId}`);
|
|
37
|
-
const disabled = $derived(ownDisabled || providedDisabled || false);
|
|
38
|
-
const groupContext = getContext("GroupContext");
|
|
39
|
-
$effect(() => {
|
|
40
|
-
if (groupContext) groupContext.switchElement = ref ?? null;
|
|
41
|
-
});
|
|
42
|
-
const d = useDisposables();
|
|
43
|
-
let changing = $state(false);
|
|
44
|
-
const toggle = () => {
|
|
45
|
-
changing = true;
|
|
46
|
-
checked = !checked;
|
|
47
|
-
onchange?.(checked);
|
|
48
|
-
d.nextFrame(() => {
|
|
49
|
-
changing = false;
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
const handleClick = (event) => {
|
|
53
|
-
event.preventDefault();
|
|
54
|
-
toggle();
|
|
55
|
-
};
|
|
56
|
-
const handleKeyUp = (event) => {
|
|
57
|
-
if (event.key === " ") {
|
|
58
|
-
event.preventDefault();
|
|
59
|
-
toggle();
|
|
60
|
-
} else if (event.key === "Enter") {
|
|
61
|
-
attemptSubmit(event.currentTarget);
|
|
4
|
+
const DEFAULT_SWITCH_TAG = "button" as const
|
|
5
|
+
type SwitchRenderPropArg = {
|
|
6
|
+
checked: boolean
|
|
7
|
+
hover: boolean
|
|
8
|
+
focus: boolean
|
|
9
|
+
active: boolean
|
|
10
|
+
autofocus: boolean
|
|
11
|
+
changing: boolean
|
|
12
|
+
disabled: boolean
|
|
62
13
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
14
|
+
type SwitchPropsWeControl = "aria-checked" | "aria-describedby" | "aria-labelledby" | "role"
|
|
15
|
+
|
|
16
|
+
export type SwitchProps<TTag extends ElementType = typeof DEFAULT_SWITCH_TAG> = Props<
|
|
17
|
+
TTag,
|
|
18
|
+
SwitchRenderPropArg,
|
|
19
|
+
SwitchPropsWeControl,
|
|
20
|
+
{
|
|
21
|
+
checked?: boolean
|
|
22
|
+
defaultChecked?: boolean
|
|
23
|
+
onchange?(checked: boolean): void
|
|
24
|
+
name?: string
|
|
25
|
+
value?: string
|
|
26
|
+
form?: string
|
|
27
|
+
autofocus?: boolean
|
|
28
|
+
disabled?: boolean
|
|
29
|
+
tabIndex?: number
|
|
78
30
|
}
|
|
31
|
+
>
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_SWITCH_TAG">
|
|
35
|
+
import { useId } from "../hooks/use-id.js"
|
|
36
|
+
import { useDisabled } from "../hooks/use-disabled.js"
|
|
37
|
+
import { useProvidedId } from "../internal/id.js"
|
|
38
|
+
import { getContext, tick } from "svelte"
|
|
39
|
+
import type { GroupContext } from "./SwitchGroup.svelte"
|
|
40
|
+
import { attemptSubmit } from "../utils/form.js"
|
|
41
|
+
import { useLabelledBy } from "../label/context.svelte.js"
|
|
42
|
+
import { useDescribedBy } from "../description/context.svelte.js"
|
|
43
|
+
import { mergeProps } from "../utils/render.js"
|
|
44
|
+
import { useResolveButtonType } from "../hooks/use-resolve-button-type.svelte.js"
|
|
45
|
+
import { useFocusRing } from "../hooks/use-focus-ring.svelte.js"
|
|
46
|
+
import { useHover } from "../hooks/use-hover.svelte.js"
|
|
47
|
+
import { useActivePress } from "../hooks/use-active-press.svelte.js"
|
|
48
|
+
import { useControllable } from "../hooks/use-controllable.svelte.js"
|
|
49
|
+
import FormFields from "../internal/FormFields.svelte"
|
|
50
|
+
import ElementOrComponent from "../utils/ElementOrComponent.svelte"
|
|
51
|
+
|
|
52
|
+
const internalId = useId()
|
|
53
|
+
const providedId = useProvidedId()
|
|
54
|
+
const providedDisabled = useDisabled()
|
|
55
|
+
let {
|
|
56
|
+
ref = $bindable(),
|
|
57
|
+
id = (providedId || `headlessui-switch-${internalId}`) as PropsOf<TTag>["id"],
|
|
58
|
+
disabled: theirDisabled = false,
|
|
59
|
+
checked: controlledChecked = $bindable(),
|
|
60
|
+
defaultChecked: _defaultChecked,
|
|
61
|
+
onchange: controlledOnChange,
|
|
62
|
+
name,
|
|
63
|
+
value,
|
|
64
|
+
form,
|
|
65
|
+
autofocus = false,
|
|
66
|
+
tabIndex,
|
|
67
|
+
...theirProps
|
|
68
|
+
}: { as?: TTag } & SwitchProps<TTag> = $props()
|
|
69
|
+
const disabled = $derived(providedDisabled?.value ?? theirDisabled)
|
|
70
|
+
const groupContext = getContext<GroupContext>("GroupContext")
|
|
71
|
+
$effect(() => {
|
|
72
|
+
if (groupContext) groupContext.switchElement = ref ?? null
|
|
79
73
|
})
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
74
|
+
|
|
75
|
+
const defaultChecked = _defaultChecked
|
|
76
|
+
const controllable = useControllable(
|
|
77
|
+
{
|
|
78
|
+
get controlledValue() {
|
|
79
|
+
return controlledChecked
|
|
80
|
+
},
|
|
81
|
+
set controlledValue(checked) {
|
|
82
|
+
controlledChecked = checked
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
controlledOnChange,
|
|
86
|
+
defaultChecked ?? false
|
|
87
|
+
)
|
|
88
|
+
const { value: checked, onchange } = $derived(controllable)
|
|
89
|
+
|
|
90
|
+
let changing = $state(false)
|
|
91
|
+
|
|
92
|
+
const toggle = () => {
|
|
93
|
+
changing = true
|
|
94
|
+
onchange?.(!checked)
|
|
95
|
+
tick().then(() => {
|
|
96
|
+
changing = false
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const handleClick = (event: MouseEvent) => {
|
|
101
|
+
//if (isDisabledReactIssue7711(event.currentTarget)) return event.preventDefault()
|
|
102
|
+
event.preventDefault()
|
|
103
|
+
toggle()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const handleKeyUp = (event: KeyboardEvent) => {
|
|
107
|
+
if (event.key === " ") {
|
|
108
|
+
event.preventDefault()
|
|
109
|
+
toggle()
|
|
110
|
+
} else if (event.key === "Enter") {
|
|
111
|
+
attemptSubmit(event.currentTarget as HTMLElement)
|
|
85
112
|
}
|
|
86
|
-
})
|
|
87
|
-
);
|
|
88
|
-
const slot = $derived({
|
|
89
|
-
checked,
|
|
90
|
-
disabled,
|
|
91
|
-
hover,
|
|
92
|
-
focus,
|
|
93
|
-
active,
|
|
94
|
-
autofocus,
|
|
95
|
-
changing
|
|
96
|
-
});
|
|
97
|
-
const buttonType = useResolveButtonType({
|
|
98
|
-
get props() {
|
|
99
|
-
return { type: theirProps.type, as: theirProps.as };
|
|
100
|
-
},
|
|
101
|
-
get ref() {
|
|
102
|
-
return { current: ref };
|
|
103
113
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
114
|
+
|
|
115
|
+
// This is needed so that we can "cancel" the click event when we use the `Enter` key on a button.
|
|
116
|
+
const handleKeyPress = (event: KeyboardEvent) => event.preventDefault()
|
|
117
|
+
|
|
118
|
+
let labelledBy = useLabelledBy()
|
|
119
|
+
let describedBy = useDescribedBy()
|
|
120
|
+
|
|
121
|
+
const { isFocusVisible: focus, focusProps } = $derived(
|
|
122
|
+
useFocusRing({
|
|
123
|
+
get autofocus() {
|
|
124
|
+
return autofocus
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
)
|
|
128
|
+
const { isHovered: hover, hoverProps } = $derived(
|
|
129
|
+
useHover({
|
|
130
|
+
get disabled() {
|
|
131
|
+
return disabled
|
|
132
|
+
},
|
|
133
|
+
})
|
|
134
|
+
)
|
|
135
|
+
const { pressed: active, pressProps } = $derived(
|
|
136
|
+
useActivePress({
|
|
137
|
+
get disabled() {
|
|
138
|
+
return disabled
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
const slot = $derived({
|
|
144
|
+
checked,
|
|
145
|
+
disabled,
|
|
146
|
+
hover,
|
|
147
|
+
focus,
|
|
148
|
+
active,
|
|
149
|
+
autofocus,
|
|
150
|
+
changing,
|
|
151
|
+
} satisfies SwitchRenderPropArg)
|
|
152
|
+
|
|
153
|
+
const buttonType = useResolveButtonType({
|
|
154
|
+
get props() {
|
|
155
|
+
return { type: theirProps.type, as: theirProps.as }
|
|
156
|
+
},
|
|
157
|
+
get ref() {
|
|
158
|
+
return { current: ref }
|
|
120
159
|
},
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
const ourProps = $derived(
|
|
163
|
+
mergeProps(
|
|
164
|
+
{
|
|
165
|
+
id,
|
|
166
|
+
role: "switch",
|
|
167
|
+
type: buttonType.type,
|
|
168
|
+
tabIndex: tabIndex === -1 ? 0 : (tabIndex ?? 0),
|
|
169
|
+
"aria-checked": checked,
|
|
170
|
+
"aria-labelledby": labelledBy.value,
|
|
171
|
+
"aria-describedby": describedBy.value,
|
|
172
|
+
disabled: disabled || undefined,
|
|
173
|
+
autofocus,
|
|
174
|
+
onclick: handleClick,
|
|
175
|
+
onkeyup: handleKeyUp,
|
|
176
|
+
onkeypress: handleKeyPress,
|
|
177
|
+
},
|
|
178
|
+
focusProps,
|
|
179
|
+
hoverProps,
|
|
180
|
+
pressProps
|
|
181
|
+
)
|
|
124
182
|
)
|
|
125
|
-
|
|
126
|
-
const reset = () => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
};
|
|
183
|
+
|
|
184
|
+
const reset = $derived(() => {
|
|
185
|
+
if (defaultChecked === undefined) return
|
|
186
|
+
return onchange?.(defaultChecked)
|
|
187
|
+
})
|
|
131
188
|
</script>
|
|
132
189
|
|
|
133
190
|
{#if name}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ElementType, Props } from "../utils/types.js";
|
|
1
|
+
import type { ElementType, Props, PropsOf } from "../utils/types.js";
|
|
2
2
|
declare const DEFAULT_SWITCH_TAG: "button";
|
|
3
3
|
type SwitchRenderPropArg = {
|
|
4
4
|
checked: boolean;
|
|
@@ -24,11 +24,11 @@ export type SwitchProps<TTag extends ElementType = typeof DEFAULT_SWITCH_TAG> =
|
|
|
24
24
|
declare class __sveltets_Render<TTag extends ElementType = typeof DEFAULT_SWITCH_TAG> {
|
|
25
25
|
props(): {
|
|
26
26
|
as?: TTag | undefined;
|
|
27
|
-
} & (Exclude<keyof
|
|
27
|
+
} & (Exclude<keyof PropsOf<TTag>, "form" | ("as" | "children" | "refName" | "class") | "disabled" | "autofocus" | "name" | "value" | "checked" | "onchange" | "tabIndex" | "defaultChecked" | SwitchPropsWeControl> extends infer T extends keyof PropsOf<TTag> ? { [P in T]: PropsOf<TTag>[P]; } : never) & {
|
|
28
28
|
children?: import("../utils/types.js").Children<SwitchRenderPropArg> | undefined;
|
|
29
29
|
ref?: HTMLElement;
|
|
30
|
-
} & (true extends (
|
|
31
|
-
class?:
|
|
30
|
+
} & (true extends (PropsOf<TTag> extends infer T_1 ? T_1 extends PropsOf<TTag> ? T_1 extends never ? never : "class" extends infer T_2 ? T_2 extends "class" ? T_2 extends keyof T_1 ? true : never : never : never : never : never) ? {
|
|
31
|
+
class?: PropsOf<TTag>["class"] | ((bag: SwitchRenderPropArg) => string) | undefined;
|
|
32
32
|
} : {}) & {
|
|
33
33
|
checked?: boolean;
|
|
34
34
|
defaultChecked?: boolean;
|