sudovanilla-website/public/@shoelace-style/shoelace/cdn/shoelace-autoloader.js

41 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-05-13 22:34:06 -05:00
import {
getBasePath
} from "./chunks/chunk.3Y6SB6QS.js";
import "./chunks/chunk.KIILAQWQ.js";
// src/shoelace-autoloader.ts
var observer = new MutationObserver((mutations) => {
for (const { addedNodes } of mutations) {
for (const node of addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
discover(node);
}
}
}
});
async function discover(root) {
const rootTagName = root instanceof Element ? root.tagName.toLowerCase() : "";
const rootIsShoelaceElement = rootTagName == null ? void 0 : rootTagName.startsWith("sl-");
const tags = [...root.querySelectorAll(":not(:defined)")].map((el) => el.tagName.toLowerCase()).filter((tag) => tag.startsWith("sl-"));
if (rootIsShoelaceElement && !customElements.get(rootTagName)) {
tags.push(rootTagName);
}
const tagsToRegister = [...new Set(tags)];
await Promise.allSettled(tagsToRegister.map((tagName) => register(tagName)));
}
function register(tagName) {
if (customElements.get(tagName)) {
return Promise.resolve();
}
const tagWithoutPrefix = tagName.replace(/^sl-/i, "");
const path = getBasePath(`components/${tagWithoutPrefix}/${tagWithoutPrefix}.js`);
return new Promise((resolve, reject) => {
import(path).then(() => resolve()).catch(() => reject(new Error(`Unable to autoload <${tagName}> from ${path}`)));
});
}
discover(document.body);
observer.observe(document.documentElement, { subtree: true, childList: true });
export {
discover
};