Initial commit with files
Still need to fix the imports
This commit is contained in:
29
src/lib/components/portal/Portal.svelte
Normal file
29
src/lib/components/portal/Portal.svelte
Normal file
@@ -0,0 +1,29 @@
|
||||
<script lang="ts">
|
||||
import { usePortalGroupContext } from "./PortalGroup.svelte";
|
||||
import { usePortalRoot } from "./ForcePortalRootContext.svelte";
|
||||
import { portal } from "./use-portal";
|
||||
let forceInRoot = usePortalRoot();
|
||||
let groupTarget = usePortalGroupContext();
|
||||
$: target = (() => {
|
||||
// Group context is used, but still null
|
||||
if (
|
||||
!(forceInRoot && $forceInRoot) &&
|
||||
groupTarget !== undefined &&
|
||||
$groupTarget !== null
|
||||
)
|
||||
return $groupTarget;
|
||||
|
||||
// No group context is used, let's create a default portal root
|
||||
if (typeof window === "undefined") return null;
|
||||
let existingRoot = document.getElementById("headlessui-portal-root");
|
||||
if (existingRoot) return existingRoot;
|
||||
|
||||
let root = document.createElement("div");
|
||||
root.setAttribute("id", "headlessui-portal-root");
|
||||
return document.body.appendChild(root);
|
||||
})();
|
||||
</script>
|
||||
|
||||
<div use:portal={target}>
|
||||
<slot />
|
||||
</div>
|
||||
18
src/lib/components/portal/PortalGroup.svelte
Normal file
18
src/lib/components/portal/PortalGroup.svelte
Normal file
@@ -0,0 +1,18 @@
|
||||
<script lang="ts" context="module">
|
||||
import { getContext, setContext } from "svelte";
|
||||
import { writable, Writable } from "svelte/store";
|
||||
const PORTAL_GROUP_CONTEXT_NAME = "headlessui-portal-group-context";
|
||||
|
||||
export function usePortalGroupContext():
|
||||
| Writable<HTMLElement | null>
|
||||
| undefined {
|
||||
return getContext(PORTAL_GROUP_CONTEXT_NAME);
|
||||
}
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
export let target: HTMLElement | null;
|
||||
setContext(PORTAL_GROUP_CONTEXT_NAME, writable(target));
|
||||
</script>
|
||||
|
||||
<slot />
|
||||
Reference in New Issue
Block a user