forked from cms/tibi-svelte-starter
19 lines
436 B
TypeScript
19 lines
436 B
TypeScript
/**
|
|
* Portal action — teleports an element to document.body.
|
|
* Usage: <div use:portal>…</div>
|
|
* SSR-safe: only runs when document is available.
|
|
*/
|
|
export function portal(node: HTMLElement) {
|
|
if (typeof document === "undefined") return
|
|
|
|
document.body.appendChild(node)
|
|
|
|
return {
|
|
destroy() {
|
|
if (node.parentNode) {
|
|
node.parentNode.removeChild(node)
|
|
}
|
|
},
|
|
}
|
|
}
|