UNPKG

@lit/reactive-element

Version:

A simple low level base class for creating fast, lightweight web components

44 lines 1.48 kB
/** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const legacyCustomElement = (tagName, clazz) => { window.customElements.define(tagName, clazz); // Cast as any because TS doesn't recognize the return type as being a // subtype of the decorated class when clazz is typed as // `Constructor<HTMLElement>` for some reason. // `Constructor<HTMLElement>` is helpful to make sure the decorator is // applied to elements however. // eslint-disable-next-line @typescript-eslint/no-explicit-any return clazz; }; const standardCustomElement = (tagName, descriptor) => { const { kind, elements } = descriptor; return { kind, elements, // This callback is called once the class is otherwise fully defined finisher(clazz) { window.customElements.define(tagName, clazz); }, }; }; /** * Class decorator factory that defines the decorated class as a custom element. * * ```js * @customElement('my-element') * class MyElement extends LitElement { * render() { * return html``; * } * } * ``` * @category Decorator * @param tagName The tag name of the custom element to define. */ export const customElement = (tagName) => (classOrDescriptor) => typeof classOrDescriptor === 'function' ? legacyCustomElement(tagName, classOrDescriptor) : standardCustomElement(tagName, classOrDescriptor); //# sourceMappingURL=custom-element.js.map