-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
46 additions
and
49 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
<script context="module" lang="ts"> | ||
import type { SvelteComponent } from "svelte"; | ||
import { writable } from 'svelte/store'; | ||
import { tick } from 'svelte'; | ||
type StrictSvelteComponent = SvelteComponent<unknown, unknown, unknown>; | ||
type Container = HTMLElement; | ||
const components: { [key: StrictSvelteComponent]: string } = { | ||
} | ||
export function teleport(component: StrictSvelteComponent, key: string) { | ||
components[component] = key; | ||
} | ||
// universal map to keep track of what portal a component wants to be in | ||
let components = new Map<Container, string>(); | ||
// dirty tracker - a Map isn't reactive, so we need to coerce Svelte to re-render | ||
let dirty = writable(false); | ||
export function hasComponent(component: StrictSvelteComponent) { | ||
return components[component] !== undefined; | ||
export async function teleport(component: Container, key: string) { | ||
components.set(component, key); | ||
// trigger a re-render | ||
dirty.set(true); | ||
await tick(); | ||
dirty.set(false); | ||
} | ||
</script> | ||
|
||
<script lang="ts"> | ||
export let key: string; | ||
export let component: SvelteComponent; | ||
export let component: Container; | ||
/* | ||
- component may be nil before mount | ||
- listen to dirty to force a re-render | ||
*/ | ||
$: if (component && $dirty == true && components.get(component) == key) { | ||
// appendChild forces a move, not a copy - we can safely use this as the DOM | ||
// handles ownership of the node for us | ||
container.appendChild(component); | ||
} | ||
let container: HTMLDivElement; | ||
</script> | ||
|
||
<div style="display: contents" bind:this={container} hidden={components[component] != key} /> | ||
<div style="display: contents;" bind:this={container} hidden={components.get(component) != key} /> |
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,4 +1,4 @@ | ||
import Limbo, { teleport } from "./Limbo.svelte"; | ||
import Portal from "./Portal.svelte"; | ||
import Limbo from './Limbo.svelte'; | ||
import Portal, { teleport } from './Portal.svelte'; | ||
|
||
export { Limbo, teleport, Portal }; | ||
export { Limbo, teleport, Portal }; |
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