From 0e6e2abd2886042470b071461d5d4ca9c28dcd98 Mon Sep 17 00:00:00 2001 From: Jared White Date: Mon, 13 Mar 2023 08:47:37 -0700 Subject: [PATCH] Export autoload discover function and support shadow roots (#1236) * Export autoload discover function and support shadow roots * run prettier --- src/shoelace-autoloader.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/shoelace-autoloader.ts b/src/shoelace-autoloader.ts index c9d98913b..d5de39baf 100644 --- a/src/shoelace-autoloader.ts +++ b/src/shoelace-autoloader.ts @@ -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