@agnos-ui/core
Version:
Framework-agnostic headless component library.
44 lines (43 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const utils_directive = require("../dom-CuBx1JPZ.cjs");
const portal = utils_directive.browserDirective((content, newArg) => {
let arg;
let replaceComment;
const removeReplaceComment = () => {
var _a;
if (replaceComment) {
(_a = replaceComment.parentNode) == null ? void 0 : _a.replaceChild(content, replaceComment);
replaceComment = null;
}
};
const update = (newArg2) => {
var _a, _b;
if (newArg2 !== arg && ((newArg2 == null ? void 0 : newArg2.container) !== (arg == null ? void 0 : arg.container) || (newArg2 == null ? void 0 : newArg2.insertBefore) !== (arg == null ? void 0 : arg.insertBefore))) {
arg = newArg2;
const container = (arg == null ? void 0 : arg.container) ?? ((_a = arg == null ? void 0 : arg.insertBefore) == null ? void 0 : _a.parentElement);
if (container) {
const insertBefore = (arg == null ? void 0 : arg.insertBefore) ?? null;
const moveNeeded = content.parentElement !== container || content.nextSibling !== insertBefore;
if (moveNeeded) {
if (!replaceComment) {
replaceComment = (_b = content.parentNode) == null ? void 0 : _b.insertBefore(content.ownerDocument.createComment("portal"), content);
}
container.insertBefore(content, insertBefore);
}
} else {
removeReplaceComment();
}
}
};
update(newArg);
return {
update,
destroy: () => {
var _a;
removeReplaceComment();
(_a = content.parentNode) == null ? void 0 : _a.removeChild(content);
}
};
});
exports.portal = portal;