Export autoload discover function and support shadow roots (#1236)

* Export autoload discover function and support shadow roots

* run prettier
This commit is contained in:
Jared White
2023-03-13 08:47:37 -07:00
committed by GitHub
parent db1bdfbf65
commit 0e6e2abd28

View File

@@ -13,16 +13,16 @@ const observer = new MutationObserver(mutations => {
/**
* Checks a node for undefined elements and attempts to register them.
*/
async function discover(root: Element) {
const rootTagName = root.tagName.toLowerCase();
const rootIsCustomElement = rootTagName.includes('-');
export async function discover(root: Element | ShadowRoot) {
const rootTagName = root instanceof Element ? root.tagName.toLowerCase() : '';
const rootIsCustomElement = rootTagName?.includes('-');
const tags = [...root.querySelectorAll(':not(:defined)')]
.map(el => el.tagName.toLowerCase())
.filter(tag => tag.startsWith('sl-'));
// If the root element is an undefined custom element, add it to the list
if (rootIsCustomElement && !customElements.get(rootTagName)) {
tags.push(root.tagName.toLowerCase());
tags.push(rootTagName);
}
// Make the list unique