Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix/allow destructuring for scopedElements #2782

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/scoped-elements/html-element.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { dedupeMixin } from '@open-wc/dedupe-mixin';
// eslint-disable-next-line no-unused-vars, import/no-unresolved
import { ScopedElementsHost } from './types.js';

/**
* @typedef {import('./types.js').ScopedElementsHost} ScopedElementsHost
* @typedef {import('./types.js').ScopedElementsMap} ScopedElementsMap
*/

Expand All @@ -14,11 +15,10 @@ if (!versions.includes(version)) {
/**
* @template {import('./types.js').Constructor<HTMLElement>} T
* @param {T} superclass
* @return {T & import('./types.js').Constructor<ScopedElementsHost>}
* @return {T & import('./types.js').Constructor<ScopedElementsHost> & typeof ScopedElementsHost}
*/
const ScopedElementsMixinImplementation = superclass =>
/** @type {ScopedElementsHost} */
class ScopedElementsHost extends superclass {
class ScopedElementsHostImplementation extends superclass {
/**
* Obtains the scoped elements definitions map if specified.
*
Expand All @@ -39,7 +39,7 @@ const ScopedElementsMixinImplementation = superclass =>
* @returns {CustomElementRegistry=}
*/
get registry() {
return /** @type {typeof ScopedElementsHost} */ (this.constructor).__registry;
return /** @type {typeof ScopedElementsHostImplementation} */ (this.constructor).__registry;
}

/**
Expand All @@ -48,7 +48,8 @@ const ScopedElementsMixinImplementation = superclass =>
* @param {CustomElementRegistry} registry
*/
set registry(registry) {
/** @type {typeof ScopedElementsHost} */ (this.constructor).__registry = registry;
/** @type {typeof ScopedElementsHostImplementation} */ (this.constructor).__registry =
registry;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/scoped-elements/lit-element.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { dedupeMixin } from '@open-wc/dedupe-mixin';
import { adoptStyles } from 'lit';
import { ScopedElementsMixin as BaseScopedElementsMixin } from './html-element.js';
// eslint-disable-next-line no-unused-vars, import/no-unresolved
import { ScopedElementsHost } from './types.js';

/**
* @typedef {import('./types.js').ScopedElementsHost} ScopedElementsHost
* @typedef {import('./types.js').ScopedElementsMap} ScopedElementsMap
* @typedef {import('lit').CSSResultOrNative} CSSResultOrNative
* @typedef {import('lit').LitElement} LitElement
Expand All @@ -15,11 +16,10 @@ import { ScopedElementsMixin as BaseScopedElementsMixin } from './html-element.j
/**
* @template {LitElementConstructor} T
* @param {T} superclass
* @return {T & ScopedElementsHostConstructor}
* @return {T & ScopedElementsHostConstructor & typeof ScopedElementsHost}
*/
const ScopedElementsMixinImplementation = superclass =>
/** @type {ScopedElementsHost} */
class ScopedElementsHost extends BaseScopedElementsMixin(superclass) {
class ScopedElementsHostImplementation extends BaseScopedElementsMixin(superclass) {
createRenderRoot() {
const { shadowRootOptions, elementStyles } = /** @type {TypeofLitElement} */ (
this.constructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { LitElement } from 'lit';
import { ScopedElementsMixin } from '../../../lit-element.js';
import { MyButton1 } from './MyButton1.js';

export class FeatureA0 extends LitElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}

connectedCallback() {
super.connectedCallback();
this.shadowRoot.innerHTML = '<button></button>';
}
}

export class FeatureA extends ScopedElementsMixin(FeatureA0) {
static scopedElements = {
// Note this line does not generate errors anymore
...super.scopedElements,
'my-button': MyButton1,
};

constructor() {
super();
this.attachShadow({ mode: 'open' });
}

connectedCallback() {
this.shadowRoot.innerHTML = '<my-button></my-button>';
}
}
4 changes: 3 additions & 1 deletion packages/scoped-elements/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export declare class ScopedElementsHost {
/**
* Obtains the scoped elements definitions map
*/
static scopedElements: ScopedElementsMap;
static scopedElements: ScopedElementsMap | undefined;

/**
* Obtains the CustomElementRegistry
*/
registry?: CustomElementRegistry;

constructor(...args: any[]);
}

declare global {
Expand Down
2 changes: 1 addition & 1 deletion packages/testing-helpers/src/scopedElementsWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const getWrapperUniqueName = (counter = 0) => {
*
* @param {import('./renderable.js').LitHTMLRenderable} template
* @param {ScopedElementsMap} scopedElements
* @return {HTMLElement}
* @return {ScopedElementsTestWrapper}
*/
export function getScopedElementsTemplate(template, scopedElements) {
const wrapperTagName = getWrapperUniqueName();
Expand Down
Loading