diff --git a/src/element.ts b/src/element.ts index c8150f6..1ea98ba 100644 --- a/src/element.ts +++ b/src/element.ts @@ -22,7 +22,8 @@ export function noShadowDOM() { export function createElementType( BaseElement: typeof HTMLElement, - propDefinition: PropsDefinition + propDefinition: PropsDefinition, + { nestedCustomElements = window.customElements }: { nestedCustomElements?: CustomElementRegistry; } ) { const propKeys = Object.keys(propDefinition) as Array< keyof PropsDefinition @@ -59,7 +60,7 @@ export function createElementType( const props = propValues(this.props as PropsDefinition), ComponentType = this.Component as | Function - | { new (...args: any[]): any }, + | { new(...args: any[]): any }, outerElement = currentElement; try { currentElement = this; @@ -108,7 +109,13 @@ export function createElementType( } get renderRoot() { - return this.shadowRoot || this.attachShadow({ mode: "open" }); + return this.shadowRoot || (() => { + try { + return this.attachShadow({ mode: 'open', registry: nestedCustomElements } as any) + } catch (err) { + return this.attachShadow({ mode: 'open' }) + } + })(); } addReleaseCallback(fn: () => void) { diff --git a/src/index.ts b/src/index.ts index 50d34a1..9cc39f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ export type RegisterOptions = { BaseElement?: typeof HTMLElement; extension?: { extends: string }; customElements?: CustomElementRegistry; + nestedCustomElements?: CustomElementRegistry; }; export type PropDefinition = uPropDefinition; @@ -31,7 +32,8 @@ export function register( const { BaseElement = HTMLElement, extension, - customElements = window.customElements + customElements = window.customElements, + nestedCustomElements = customElements, } = options; return (ComponentType: ComponentType) => { if (!tag) throw new Error("tag is required to register a Component"); @@ -42,7 +44,7 @@ export function register( return ElementType; } - ElementType = createElementType(BaseElement, normalizePropDefs(props)); + ElementType = createElementType(BaseElement, normalizePropDefs(props),{nestedCustomElements}); ElementType.prototype.Component = ComponentType; ElementType.prototype.registeredTag = tag; customElements.define(tag, ElementType, extension);