@open-wc/scoped-elements
Version:
Allows to auto define custom elements scoping them
41 lines (33 loc) • 1.5 kB
JavaScript
import { dedupeMixin } from '@open-wc/dedupe-mixin';
import { adoptStyles } from 'lit';
import { ScopedElementsMixin as BaseScopedElementsMixin } from './html-element.js';
/**
* @typedef {import('./types.js').ScopedElementsHost} ScopedElementsHost
* @typedef {import('./types.js').ScopedElementsMap} ScopedElementsMap
* @typedef {import('lit').CSSResultOrNative} CSSResultOrNative
* @typedef {import('lit').LitElement} LitElement
* @typedef {typeof import('lit').LitElement} TypeofLitElement
* @typedef {import('@open-wc/dedupe-mixin').Constructor<LitElement>} LitElementConstructor
* @typedef {import('@open-wc/dedupe-mixin').Constructor<ScopedElementsHost>} ScopedElementsHostConstructor
*/
/**
* @template {LitElementConstructor} T
* @param {T} superclass
* @return {T & ScopedElementsHostConstructor}
*/
const ScopedElementsMixinImplementation = superclass =>
/** @type {ScopedElementsHost} */
class ScopedElementsHost extends BaseScopedElementsMixin(superclass) {
createRenderRoot() {
const { shadowRootOptions, elementStyles } = /** @type {TypeofLitElement} */ (
this.constructor
);
const shadowRoot = this.attachShadow(shadowRootOptions);
// @ts-ignore
this.renderOptions.creationScope = shadowRoot;
adoptStyles(shadowRoot, elementStyles);
this.renderOptions.renderBefore ??= shadowRoot.firstChild;
return shadowRoot;
}
};
export const ScopedElementsMixin = dedupeMixin(ScopedElementsMixinImplementation);