Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build web components custom elements to work with both specs

I need to build a component that should need to work with both specs, custom elements spec v0 which got deprecated and custom elements spec v1, latest stable version.

If I build components with custom elements v0 spec some apps will face issues since they are using polymer 2 and above and the same problem with polymer 1 applications which will not work with custom elements v1 spec.

I do not have control over applications to change polyfills, some applications have to use polyfills supports old spec and some uses new polyfills.

I am looking for a solid solution to combine both the specs to run my custom elements in all the applications irrespective of polyfills version. I can add any piece of polyfill or snippet to my components so that they can run anywhere, I have not found any such library or polyfill which support both specs in my research.

I am planning to write an adapter that can combine both the specs like mapping mentioned below for attached callback, inputs on this thought will be much appreciated.

connectedCallback(){
    this.attachedCallback();
}

I tried to use stenciljs but it can work only with the latest version of custom elements spec. I have not found any way to tweak it to make it work with earlier spec.

Please suggest some viable alternatives and feasible solutions to the above-mentioned situation.

like image 523
kongaraju Avatar asked Oct 09 '19 06:10

kongaraju


People also ask

How do I create a custom website component?

Customized built-in elements inherit from basic HTML elements. To create one of these, you have to specify which element they extend (as implied in the examples above), and they are used by writing out the basic element but specifying the name of the custom element in the is attribute (or property).

How do custom elements work internally?

A custom element extends HTML by allowing you to define a tag whose content is created and controlled by JavaScript code. The browser maintains a CustomElementRegistry of defined custom elements, which maps an instantiable JavaScript class to an HTML tag.

Can custom elements be self closing?

Custom elements cannot be self-closing because HTML only allows a few elements to be self-closing. Always write a closing tag ( <app-drawer></app-drawer> ).


1 Answers

Basically your component has some dependencies which are defined in the polyfills either directly, or indirectly. If we consider these dependencies as nodes of a dependency graph, then we have the problem of the graphs being different. It's possible that a node exists in both graphs, but behaving different (older and newer implementation of the same function) and it's also possible that some nodes which are present in a graph are missing in another one. You can of course put in some polyfills of your own or something of the like, but then you would need to maintain polyfills, which might be less than helpful.

A better approach in my opinion is to implement a function, like

function getWebComponentsVersion() {
    //Return v1 if it's v1 and v0 if it's v0
}

I'm not sure how to implement this function, but if there is either a function which yields the proper version, or some evident differences between functionalities, then you can implement the function above accordingly. And then, run this code:

if (getWebComponentsVersion() === "v1") {
    //code for v1
} else {
    //code for v0
}
like image 138
Lajos Arpad Avatar answered Sep 19 '22 03:09

Lajos Arpad