mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-06 03:34:40 +00:00
23 lines
739 B
TypeScript
23 lines
739 B
TypeScript
import type { Action } from "svelte/action";
|
|
|
|
/**
|
|
* Svelte action that moves the element to a different DOM target.
|
|
* Defaults to the nearest ShadowRoot so styles remain intact when the app
|
|
* is rendered inside a shadow DOM. Falls back to document.body otherwise.
|
|
* Keeps all Svelte reactivity/events intact while escaping ancestor stacking contexts.
|
|
*/
|
|
export const portal: Action<HTMLElement, HTMLElement | ShadowRoot | undefined> = (node, target) => {
|
|
const root = node.getRootNode();
|
|
const dest = target ?? (root instanceof ShadowRoot ? root : document.body);
|
|
dest.appendChild(node);
|
|
|
|
return {
|
|
update(newTarget) {
|
|
(newTarget ?? dest).appendChild(node);
|
|
},
|
|
destroy() {
|
|
node.remove();
|
|
},
|
|
};
|
|
};
|