Skip to content

first draft for LightningElement.CustomElement abstraction#7

Merged
jodarove merged 6 commits intomasterfrom
caridy/custom-elements
Jun 12, 2020
Merged

first draft for LightningElement.CustomElement abstraction#7
jodarove merged 6 commits intomasterfrom
caridy/custom-elements

Conversation

@caridy
Copy link
Contributor

@caridy caridy commented Jul 5, 2019

No description provided.


```js
import XFoo from 'x/foo';
customElements.define('x-foo', XFoo.CustomElement);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the meat of this proposal... the .CustomElement static accessor on every LWC constructor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be quite verbose, but what do you think about 'CustomElementConstructor'?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue that I have with CustomElement, is that the custom element represents a custom element instance while the custom elements registry accepts a constructor. I support @apapko into making this explicit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned a similar concern in salesforce/lwc#1395 (review)


# Summary

Facilitate the registration of LWC Constructor as Custom Elements.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registration with what?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registration of LWC Constructor as Custom Elements


# Motivation

As today, the way to generate a qualifying Custom Element from a LWC Constructor, is via an experimental API: `buildCustomElementConstructor()`. Since this is a primary use case for LWC, we want to make it more ergonomic, and simpler. Additionally, the experimental API allow you to generate more than one Custom Element per LWC Constructors, which does not align with the web components semantics.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of today?

# Motivation

As today, the way to generate a qualifying Custom Element from a LWC Constructor, is via an experimental API: `buildCustomElementConstructor()`. Since this is a primary use case for LWC, we want to make it more ergonomic, and simpler. Additionally, the experimental API allow you to generate more than one Custom Element per LWC Constructors, which does not align with the web components semantics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listing a use case would be useful


# Proposal

This proposal provides a high-level API (an abstraction layer) for authors to access the custom element associate to a LWC Constructor:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean: associated with LWC Constructor?


```js
import XFoo from 'x/foo';
customElements.define('x-foo', XFoo.CustomElement);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be quite verbose, but what do you think about 'CustomElementConstructor'?

This proposal provides a high-level API (an abstraction layer) for authors to access the custom element associate to a LWC Constructor:

```js
import XFoo from 'x/foo';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is allowing to import 'constructor' property an option?

import { constructor as XFooConstructor } from 'x/foo';

## Semantics and Invariants

* There is a 1-1 mapping between LWC Constructors and the corresponding Custom Element.
* Claiming the Custom Element for the abstract `LightningElement` must be disallow.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this mean- Claiming the Custom Element for the abstract LightningElement?


```js
import XFoo from 'x/foo';
customElements.define('x-foo', XFoo.CustomElement);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue that I have with CustomElement, is that the custom element represents a custom element instance while the custom elements registry accepts a constructor. I support @apapko into making this explicit.

@pmdartus
Copy link
Contributor

pmdartus commented Mar 4, 2020

Looking at this RFC again, I am now uncertain about attaching a CustomElementConstructor on the LightningElement.prototype.

We have started decoupling the LWC engine from the DOM (#23), what would the CustomElementConstructor getter returns when the engine is running in an environment without DOM. The current approach with the buildCustomElementConstructor method has the following benefits:

  • it can be removed from the @lwc/engine-core and only exposed from the @lwc/engine-dom.
  • it can be tree-shaken if it is not used by the application.

@mikebernard
Copy link

mikebernard commented Mar 20, 2020

This would be huge for nCino! We have a library of custom web components that we've built and use in many of our Salesforce applications. It would save us a lot of time and money if we could reuse them.

@gonzalocordero
Copy link
Contributor

@jodarove This was already implemented right? should we change the status then?

Copy link
Contributor

@pmdartus pmdartus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caridy @jodarove I updated the RFC, I will let you check if everything is right. Otherwise LGTM.

@jodarove
Copy link
Contributor

thanks @pmdartus!

@jodarove jodarove merged commit 216e69c into master Jun 12, 2020
@jodarove jodarove deleted the caridy/custom-elements branch June 12, 2020 15:13
gaurav-rk9 pushed a commit to gaurav-rk9/lwc-rfcs that referenced this pull request May 19, 2025
)

* first draft for LightningElement.CustomElement abstraction

* update meta for rfc app

* wip: change to CustomElementConstructor

* wip: review comments

* Update 0000-custom-element.md

* Update RFC status

Co-authored-by: Jose David Rodriguez Velasco <jodarove@gmail.com>
Co-authored-by: Pierre-Marie Dartus <p.dartus@salesforce.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants