@pzerelles/headlessui-svelte 2.1.2-next.2 → 2.1.2-next.4
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 +436 -290
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -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 +600 -409
- 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/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 +97 -0
- package/dist/textarea/Textarea.svelte.d.ts +47 -0
- package/dist/textarea/index.d.ts +1 -0
- package/dist/textarea/index.js +1 -0
- 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
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from "./listbox/index.js";
|
|
|
13
13
|
export * from "./menu/index.js";
|
|
14
14
|
export * from "./switch/index.js";
|
|
15
15
|
export * from "./tabs/index.js";
|
|
16
|
+
export * from "./textarea/index.js";
|
|
16
17
|
export * from "./transition/index.js";
|
|
17
18
|
export * from "./hooks/use-id.js";
|
|
18
19
|
export * from "./utils/types.js";
|
package/dist/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export * from "./listbox/index.js";
|
|
|
13
13
|
export * from "./menu/index.js";
|
|
14
14
|
export * from "./switch/index.js";
|
|
15
15
|
export * from "./tabs/index.js";
|
|
16
|
+
export * from "./textarea/index.js";
|
|
16
17
|
export * from "./transition/index.js";
|
|
17
18
|
export * from "./hooks/use-id.js";
|
|
18
19
|
export * from "./utils/types.js";
|
package/dist/input/Input.svelte
CHANGED
|
@@ -1,59 +1,91 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { ElementType, Props } from "../utils/types.js"
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
id = providedId?.value || `headlessui-input-${internalId}`,
|
|
20
|
-
disabled: theirDisabled = false,
|
|
21
|
-
autofocus = false,
|
|
22
|
-
invalid = false,
|
|
23
|
-
...theirProps
|
|
24
|
-
} = $props();
|
|
25
|
-
const disabled = $derived(providedDisabled?.value ?? theirDisabled);
|
|
26
|
-
const labelledBy = useLabelledBy();
|
|
27
|
-
const describedBy = useDescribedBy();
|
|
28
|
-
const { isHovered: hover, hoverProps } = $derived(
|
|
29
|
-
useHover({
|
|
30
|
-
get disabled() {
|
|
31
|
-
return disabled;
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
);
|
|
35
|
-
const { isFocusVisible: focus, focusProps } = $derived(
|
|
36
|
-
useFocusRing({
|
|
37
|
-
get autofocus() {
|
|
38
|
-
return autofocus;
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
);
|
|
42
|
-
const ourProps = $derived(
|
|
43
|
-
mergeProps(
|
|
4
|
+
const DEFAULT_INPUT_TAG = "input" as const
|
|
5
|
+
|
|
6
|
+
type InputRenderPropArg = {
|
|
7
|
+
disabled: boolean
|
|
8
|
+
hover: boolean
|
|
9
|
+
focus: boolean
|
|
10
|
+
autofocus: boolean
|
|
11
|
+
invalid: boolean
|
|
12
|
+
}
|
|
13
|
+
type InputPropsWeControl = "aria-labelledby" | "aria-describedby"
|
|
14
|
+
|
|
15
|
+
export type InputProps<TTag extends ElementType = typeof DEFAULT_INPUT_TAG, TValue = string> = Props<
|
|
16
|
+
TTag,
|
|
17
|
+
InputRenderPropArg,
|
|
18
|
+
InputPropsWeControl,
|
|
44
19
|
{
|
|
45
|
-
id
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
20
|
+
id?: string
|
|
21
|
+
value?: TValue
|
|
22
|
+
disabled?: boolean
|
|
23
|
+
invalid?: boolean
|
|
24
|
+
autofocus?: boolean
|
|
25
|
+
}
|
|
26
|
+
>
|
|
27
|
+
</script>
|
|
28
|
+
|
|
29
|
+
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_INPUT_TAG, TValue = string">
|
|
30
|
+
import { htmlid } from "../utils/id.js"
|
|
31
|
+
import { useDisabled } from "../hooks/use-disabled.js"
|
|
32
|
+
import { useProvidedId } from "../internal/id.js"
|
|
33
|
+
import { useLabelledBy } from "../label/context.svelte.js"
|
|
34
|
+
import { useDescribedBy } from "../description/context.svelte.js"
|
|
35
|
+
import { useHover } from "../hooks/use-hover.svelte.js"
|
|
36
|
+
import { useFocusRing } from "../hooks/use-focus-ring.svelte.js"
|
|
37
|
+
import { mergeProps } from "../utils/render.js"
|
|
38
|
+
import ElementOrComponent from "../utils/ElementOrComponent.svelte"
|
|
39
|
+
|
|
40
|
+
const internalId = htmlid()
|
|
41
|
+
const providedId = useProvidedId()
|
|
42
|
+
const providedDisabled = useDisabled()
|
|
43
|
+
|
|
44
|
+
let {
|
|
45
|
+
ref = $bindable(),
|
|
46
|
+
value = $bindable(),
|
|
47
|
+
id = providedId?.value || `headlessui-input-${internalId}`,
|
|
48
|
+
disabled: theirDisabled = false,
|
|
49
|
+
autofocus = false,
|
|
50
|
+
invalid = false,
|
|
51
|
+
...theirProps
|
|
52
|
+
}: { as?: TTag; value?: TValue } & InputProps<TTag, TValue> = $props()
|
|
53
|
+
const disabled = $derived(providedDisabled?.value ?? theirDisabled)
|
|
54
|
+
|
|
55
|
+
const labelledBy = useLabelledBy()
|
|
56
|
+
const describedBy = useDescribedBy()
|
|
57
|
+
|
|
58
|
+
const { isHovered: hover, hoverProps } = $derived(
|
|
59
|
+
useHover({
|
|
60
|
+
get disabled() {
|
|
61
|
+
return disabled
|
|
62
|
+
},
|
|
63
|
+
})
|
|
54
64
|
)
|
|
55
|
-
|
|
56
|
-
|
|
65
|
+
const { isFocusVisible: focus, focusProps } = $derived(
|
|
66
|
+
useFocusRing({
|
|
67
|
+
get autofocus() {
|
|
68
|
+
return autofocus
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
const ourProps = $derived(
|
|
74
|
+
mergeProps(
|
|
75
|
+
{
|
|
76
|
+
id,
|
|
77
|
+
"aria-labelledby": labelledBy?.value,
|
|
78
|
+
"aria-describedby": describedBy?.value,
|
|
79
|
+
"aria-invalid": invalid ? "" : undefined,
|
|
80
|
+
disabled: disabled || undefined,
|
|
81
|
+
autofocus,
|
|
82
|
+
},
|
|
83
|
+
focusProps,
|
|
84
|
+
hoverProps
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
const slot = $derived({ disabled, invalid, hover, focus, autofocus } satisfies InputRenderPropArg)
|
|
57
89
|
</script>
|
|
58
90
|
|
|
59
91
|
<ElementOrComponent {ourProps} {theirProps} {slot} defaultTag={DEFAULT_INPUT_TAG} name="Input" bind:ref bind:value />
|
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount } from "svelte"
|
|
3
|
+
import Hidden, { HiddenFeatures } from "./Hidden.svelte"
|
|
4
|
+
|
|
5
|
+
interface FocusSentinelProps {
|
|
6
|
+
onfocus: () => boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let { onfocus }: FocusSentinelProps = $props()
|
|
10
|
+
|
|
11
|
+
let enabled = $state(true)
|
|
12
|
+
let mounted = $state(false)
|
|
13
|
+
|
|
14
|
+
onMount(() => {
|
|
15
|
+
mounted = true
|
|
16
|
+
})
|
|
9
17
|
</script>
|
|
10
18
|
|
|
11
19
|
{#if enabled}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from "svelte"
|
|
3
|
+
import { createPortalRoot } from "./portal-force-root.svelte.js"
|
|
4
|
+
|
|
5
|
+
let { force, children }: { force: boolean; children: Snippet } = $props()
|
|
6
|
+
|
|
7
|
+
createPortalRoot({ force })
|
|
4
8
|
</script>
|
|
5
9
|
|
|
6
10
|
{#if children}{@render children()}{/if}
|
|
@@ -1,23 +1,34 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { disposables } from "../utils/disposables.js"
|
|
3
|
+
import { objectToFormEntries } from "../utils/form.js"
|
|
4
|
+
import FormResolver from "./FormResolver.svelte"
|
|
5
|
+
import HoistFormFields from "./HoistFormFields.svelte"
|
|
6
|
+
import Hidden, { HiddenFeatures } from "./Hidden.svelte"
|
|
7
|
+
import { compact } from "../utils/object.js"
|
|
8
|
+
|
|
9
|
+
let {
|
|
10
|
+
data,
|
|
11
|
+
form: formId,
|
|
12
|
+
disabled,
|
|
13
|
+
onReset,
|
|
14
|
+
overrides,
|
|
15
|
+
}: {
|
|
16
|
+
data: Record<string, any>
|
|
17
|
+
overrides?: Record<string, any>
|
|
18
|
+
form?: string
|
|
19
|
+
disabled?: boolean
|
|
20
|
+
onReset?: (e: Event) => void
|
|
21
|
+
} = $props()
|
|
22
|
+
|
|
23
|
+
let form = $state<HTMLFormElement | null>(null)
|
|
24
|
+
const d = disposables()
|
|
25
|
+
|
|
26
|
+
$effect(() => {
|
|
27
|
+
if (!onReset) return
|
|
28
|
+
if (!form) return
|
|
29
|
+
|
|
30
|
+
return d.addEventListener(form, "reset", onReset)
|
|
31
|
+
})
|
|
21
32
|
</script>
|
|
22
33
|
|
|
23
34
|
<HoistFormFields>
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount } from "svelte"
|
|
3
|
+
import Hidden, { HiddenFeatures } from "./Hidden.svelte"
|
|
4
|
+
|
|
5
|
+
let { setForm, formId }: { setForm: (form: HTMLFormElement) => void; formId?: string } = $props()
|
|
6
|
+
|
|
7
|
+
$effect(() => {
|
|
8
|
+
if (formId) {
|
|
9
|
+
const resolvedForm = document.getElementById(formId) as HTMLFormElement
|
|
10
|
+
if (resolvedForm) setForm(resolvedForm)
|
|
11
|
+
}
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
let element = $state<HTMLInputElement>()
|
|
15
|
+
|
|
16
|
+
onMount(() => {
|
|
17
|
+
if (!element) return
|
|
18
|
+
const resolvedForm = element.closest("form")
|
|
19
|
+
if (resolvedForm) setForm(resolvedForm)
|
|
20
|
+
})
|
|
16
21
|
</script>
|
|
17
22
|
|
|
18
23
|
{#if !formId}
|
|
@@ -1,32 +1,49 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import ElementOrComponent from "../utils/ElementOrComponent.svelte"
|
|
3
|
+
import type { ElementType, Props } from "../utils/types.js"
|
|
4
|
+
import { HiddenFeatures } from "./HiddenFeatures.js"
|
|
5
|
+
|
|
6
|
+
export * from "./HiddenFeatures.js"
|
|
7
|
+
|
|
8
|
+
const DEFAULT_VISUALLY_HIDDEN_TAG = "span" as const
|
|
9
|
+
|
|
10
|
+
type HiddenRenderPropArg = {}
|
|
11
|
+
type HiddenPropsWeControl = never
|
|
12
|
+
export type HiddenProps<TTag extends ElementType = typeof DEFAULT_VISUALLY_HIDDEN_TAG> = Props<
|
|
13
|
+
TTag,
|
|
14
|
+
HiddenRenderPropArg,
|
|
15
|
+
HiddenPropsWeControl,
|
|
16
|
+
{ features?: HiddenFeatures }
|
|
17
|
+
>
|
|
9
18
|
</script>
|
|
10
19
|
|
|
11
|
-
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_VISUALLY_HIDDEN_TAG">
|
|
12
|
-
let
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
<script lang="ts" generics="TTag extends ElementType = typeof DEFAULT_VISUALLY_HIDDEN_TAG">
|
|
21
|
+
let { ref = $bindable(), features = HiddenFeatures.None, ...theirProps }: { as?: TTag } & HiddenProps<TTag> = $props()
|
|
22
|
+
|
|
23
|
+
let ourProps = {
|
|
24
|
+
"aria-hidden":
|
|
25
|
+
(features & HiddenFeatures.Focusable) === HiddenFeatures.Focusable
|
|
26
|
+
? true
|
|
27
|
+
: (theirProps["aria-hidden" as keyof typeof theirProps] ?? undefined),
|
|
28
|
+
hidden: (features & HiddenFeatures.Hidden) === HiddenFeatures.Hidden ? true : undefined,
|
|
29
|
+
style: [
|
|
30
|
+
"position: fixed",
|
|
31
|
+
"top: 1px",
|
|
32
|
+
"left: 1px",
|
|
33
|
+
"width: 1px",
|
|
34
|
+
"height: 0",
|
|
35
|
+
"padding: 0",
|
|
36
|
+
"margin: -1px",
|
|
37
|
+
"overflow: hidden",
|
|
38
|
+
"clip: rect(0, 0, 0, 0)",
|
|
39
|
+
"whiteSpace: nowrap",
|
|
40
|
+
"borderWidth: 0",
|
|
41
|
+
...((features & HiddenFeatures.Hidden) === HiddenFeatures.Hidden &&
|
|
42
|
+
!((features & HiddenFeatures.Focusable) === HiddenFeatures.Focusable)
|
|
43
|
+
? ["display: none"]
|
|
44
|
+
: []),
|
|
45
|
+
].join("; "),
|
|
46
|
+
}
|
|
30
47
|
</script>
|
|
31
48
|
|
|
32
49
|
<ElementOrComponent {ourProps} {theirProps} defaultTag={DEFAULT_VISUALLY_HIDDEN_TAG} name="Hidden" bind:ref />
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import type { ElementType, Props } from "../utils/types.js";
|
|
2
|
+
import { HiddenFeatures } from "./HiddenFeatures.js";
|
|
3
|
+
export * from "./HiddenFeatures.js";
|
|
2
4
|
declare const DEFAULT_VISUALLY_HIDDEN_TAG: "span";
|
|
3
|
-
export declare enum HiddenFeatures {
|
|
4
|
-
None = 1,
|
|
5
|
-
Focusable = 2,
|
|
6
|
-
Hidden = 4
|
|
7
|
-
}
|
|
8
5
|
type HiddenRenderPropArg = {};
|
|
9
6
|
type HiddenPropsWeControl = never;
|
|
10
7
|
export type HiddenProps<TTag extends ElementType = typeof DEFAULT_VISUALLY_HIDDEN_TAG> = Props<TTag, HiddenRenderPropArg, HiddenPropsWeControl, {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export var HiddenFeatures;
|
|
2
|
+
(function (HiddenFeatures) {
|
|
3
|
+
// The default, no features.
|
|
4
|
+
HiddenFeatures[HiddenFeatures["None"] = 1] = "None";
|
|
5
|
+
// Whether the element should be focusable or not.
|
|
6
|
+
HiddenFeatures[HiddenFeatures["Focusable"] = 2] = "Focusable";
|
|
7
|
+
// Whether it should be completely hidden, even to assistive technologies.
|
|
8
|
+
HiddenFeatures[HiddenFeatures["Hidden"] = 4] = "Hidden";
|
|
9
|
+
})(HiddenFeatures || (HiddenFeatures = {}));
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, type Snippet } from "svelte"
|
|
3
|
+
import Portal from "./Portal.svelte"
|
|
4
|
+
|
|
5
|
+
let { children }: { children: Snippet } = $props()
|
|
6
|
+
|
|
7
|
+
const formFieldsContext = getContext<{ target?: HTMLElement }>("FormFieldsContext")
|
|
5
8
|
</script>
|
|
6
9
|
|
|
7
10
|
{#if !formFieldsContext}
|
|
@@ -1,42 +1,95 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
|
|
3
|
-
const { fallbackMainTreeNode = null } = $derived(options);
|
|
4
|
-
return getContext("MainTreeContext") ?? {
|
|
5
|
-
get node() {
|
|
6
|
-
return fallbackMainTreeNode;
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
</script>
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import { getContext, onMount } from "svelte"
|
|
11
3
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
4
|
+
type MainTreeContext = { node: HTMLElement | null }
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get the main tree node from context or fallback to the optionally provided node.
|
|
8
|
+
*/
|
|
9
|
+
export function useMainTreeNode(options: { fallbackMainTreeNode?: HTMLElement | null } = {}) {
|
|
10
|
+
const { fallbackMainTreeNode = null } = $derived(options)
|
|
11
|
+
|
|
12
|
+
// Prefer the main tree node from context, but fallback to the provided node.
|
|
13
|
+
return (
|
|
14
|
+
getContext<MainTreeContext>("MainTreeContext") ?? {
|
|
15
|
+
get node() {
|
|
16
|
+
return fallbackMainTreeNode
|
|
17
|
+
},
|
|
18
|
+
}
|
|
19
|
+
)
|
|
25
20
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<script lang="ts">
|
|
24
|
+
import { setContext, type Snippet } from "svelte"
|
|
25
|
+
import Hidden, { HiddenFeatures } from "./Hidden.svelte"
|
|
26
|
+
import { getOwnerDocument } from "../utils/owner.js"
|
|
27
|
+
/**
|
|
28
|
+
* A provider for the main tree node.
|
|
29
|
+
*
|
|
30
|
+
* When a component is rendered in a `Portal`, it is no longer part of the main
|
|
31
|
+
* tree. This provider helps to find the main tree node and pass it along to the
|
|
32
|
+
* components that need it.
|
|
33
|
+
*
|
|
34
|
+
* The main tree node is used for features such as outside click behavior, where
|
|
35
|
+
* we allow clicks in 3rd party containers, but not in the parent of the "main
|
|
36
|
+
* tree".
|
|
37
|
+
*
|
|
38
|
+
* In case of a `Popover`, we can use the `PopoverButton` as a marker in the
|
|
39
|
+
* "main tree", the `PopoverPanel` can't be used because it could be rendered in
|
|
40
|
+
* a `Portal` (e.g. when using the `anchor` props).
|
|
41
|
+
*
|
|
42
|
+
* However, we can't use the `PopoverButton` when it's nested inside of another
|
|
43
|
+
* `Popover`'s `PopoverPanel` component if the parent `PopoverPanel` is
|
|
44
|
+
* rendered in a `Portal`.
|
|
45
|
+
*
|
|
46
|
+
* This is where the `MainTreeProvider` comes in. It will find the "main tree"
|
|
47
|
+
* node and pass it on. The top-level `PopoverButton` will be used as a marker
|
|
48
|
+
* in the "main tree" and nested `Popover` will use this button as well.
|
|
49
|
+
*/
|
|
50
|
+
let { node, children }: { children: Snippet; node?: HTMLElement | null } = $props()
|
|
51
|
+
|
|
52
|
+
let mainTreeNode = $state<HTMLElement | null>(null)
|
|
53
|
+
|
|
54
|
+
// 1. Prefer the main tree node from context
|
|
55
|
+
// 2. Prefer the provided node
|
|
56
|
+
// 3. Create a new node at this point, and find the main tree node
|
|
57
|
+
const resolvedMainTreeNode = useMainTreeNode({
|
|
58
|
+
get fallbackMainTreeNode() {
|
|
59
|
+
return node ?? mainTreeNode
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
setContext("MainTreeContext", {
|
|
64
|
+
get node() {
|
|
65
|
+
return resolvedMainTreeNode.node
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* If no main tree node is found at this point, then we briefly render an
|
|
71
|
+
* element to find the main tree node and pass it along.
|
|
72
|
+
*/
|
|
73
|
+
let el = $state<HTMLElement>()
|
|
74
|
+
onMount(() => {
|
|
75
|
+
if (!el) return
|
|
76
|
+
|
|
77
|
+
// We will only render this when no `mainTreeNode` is found. This
|
|
78
|
+
// means that if we render this element and use it as the
|
|
79
|
+
// `mainTreeNode` that we will be unmounting it later.
|
|
80
|
+
//
|
|
81
|
+
// However, we can resolve the actual root container of the main
|
|
82
|
+
// tree node and use that instead.
|
|
83
|
+
for (let container of getOwnerDocument(el)?.querySelectorAll("html > *, body > *") ?? []) {
|
|
84
|
+
if (container === document.body) continue // Skip `<body>`
|
|
85
|
+
if (container === document.head) continue // Skip `<head>`
|
|
86
|
+
if (!(container instanceof HTMLElement)) continue // Skip non-HTMLElements
|
|
87
|
+
if (container?.contains(el)) {
|
|
88
|
+
mainTreeNode = container
|
|
89
|
+
break
|
|
90
|
+
}
|
|
37
91
|
}
|
|
38
|
-
}
|
|
39
|
-
});
|
|
92
|
+
})
|
|
40
93
|
</script>
|
|
41
94
|
|
|
42
95
|
{#if children}{@render children()}{/if}
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onDestroy, onMount, type Snippet } from "svelte"
|
|
3
|
+
|
|
4
|
+
let { target, children }: { target: HTMLElement; children: Snippet } = $props()
|
|
5
|
+
let ref = $state<HTMLDivElement>()
|
|
6
|
+
|
|
7
|
+
onMount(() => {
|
|
8
|
+
target.appendChild(ref!)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
onDestroy(() => {
|
|
12
|
+
const _ref = ref
|
|
13
|
+
setTimeout(() => {
|
|
14
|
+
if (_ref?.parentNode) {
|
|
15
|
+
_ref.parentNode?.removeChild(_ref)
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
})
|
|
15
19
|
</script>
|
|
16
20
|
|
|
17
21
|
<div bind:this={ref}>{@render children()}</div>
|