Archived
Template
1
Fork 0
This repository has been archived on 2024-09-04. You can view files and clone it, but cannot push or open issues or pull requests.
Contour/public/@shoelace-style/shoelace/cdn/shoelace-autoloader.js
2024-01-30 10:59:28 -05:00

40 lines
1.4 KiB
JavaScript

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
};