-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
173 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/bits-ui/src/lib/bits/utilities/portal/portal-consumer.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script lang="ts"> | ||
import type { Snippet } from "svelte"; | ||
const { children }: { children?: Snippet } = $props(); | ||
</script> | ||
|
||
{@render children?.()} |
52 changes: 41 additions & 11 deletions
52
packages/bits-ui/src/lib/bits/utilities/portal/portal.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,46 @@ | ||
<script lang="ts"> | ||
import { usePortal } from "./use-portal.svelte.js"; | ||
import { getAllContexts, mount, unmount } from "svelte"; | ||
import PortalConsumer from "./portal-consumer.svelte"; | ||
import type { PortalProps } from "./types.js"; | ||
import { readonlyBox, useId } from "$lib/internal/index.js"; | ||
import { isBrowser } from "$lib/internal/is.js"; | ||
let { id = useId(), to = "body", forceMount, portal }: PortalProps = $props(); | ||
let { to = "body", children }: PortalProps = $props(); | ||
const state = usePortal( | ||
readonlyBox(() => id), | ||
readonlyBox(() => to) | ||
); | ||
</script> | ||
const context = getAllContexts(); | ||
let target = $derived(getTarget()); | ||
function getTarget() { | ||
if (!isBrowser) return null; | ||
let target: HTMLElement | null; | ||
if (typeof to === "string") { | ||
target = document.querySelector(to); | ||
if (target === null) { | ||
throw new Error(`Target element "${to}" not found.`); | ||
} | ||
} else if (to instanceof HTMLElement) { | ||
target = to; | ||
} else { | ||
throw new TypeError( | ||
`Unknown portal target type: ${ | ||
to === null ? "null" : typeof to | ||
}. Allowed types: string (CSS selector) or HTMLElement.` | ||
); | ||
} | ||
return target; | ||
} | ||
{#if forceMount} | ||
{@render portal?.({ portalProps: state.props })} | ||
{/if} | ||
let instance: any; | ||
$effect(() => { | ||
if (!target) { | ||
return unmount(instance); | ||
} | ||
instance = mount(PortalConsumer, { target, props: { children }, context }); | ||
return () => { | ||
unmount(instance); | ||
}; | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.