Skip to content

Non-class based example of customElement.define() #587

Closed

Description

@rektide

Hello,
I'd like for there to be an available, working examples of autonomous and customized Custom Elements made without use of the class syntax. The Mozilla MDN page for example shows a use of Object.create(HTMLElement.prototype) to create an autonomous custom element on it's Custom Elements page that satisfies this non-class based way of working, however that example doesn't work- it yields Uncaught TypeError: Failed to execute 'define' on 'CustomElementRegistry': The callback provided as parameter 2 is not a function. on customElement.define("my-tag", MyTag).

What is a valid syntax to use now, for creating autonomous and customized Custom Elements? Might we add some examples of such in to the spec?

Activity

  1. domenic commented on Oct 15, 2016

    @domenic
    Collaborator

    It's not possible to use custom elements without ES6 classes. That was a design decision necessary to achieve consensus at the January face-to-face meeting.

    Closing, since there's nothing actionable here, but happy to continue discussing in the closed thread.

  2. domenic commented on Oct 15, 2016

    @domenic
    Collaborator

    I've updated the linked documentation to at least be up to date with custom elements v1. It still is on old shadow DOM however, and in general https://developer.mozilla.org/en-US/docs/Web/Web_Components looks very, very outdated and confusing. If anyone has time to update all the web components docs to the latest specs, that would be a great help to developers everywhere, I am sure.

  3. rektide commented on Oct 15, 2016

    @rektide
    Author

    I'd like to see non-class based JS become possible, hopefully in v2. Please re-open this as a request. Classes are syntax, statically constructed, which means we can't create components on the fly with code. This is a serious and frankly scary limitation.

    For an example of use, if I wanted to generate components for, say, schema.org, the class based syntax means that I have to manually type out class definitions for ~650 components. Having normal, regular JS objects would have let me use code to generate new components. Please re-open this as an outstanding issue @domenic.

  4. domenic commented on Oct 15, 2016

    @domenic
    Collaborator

    Sorry, this was a condition of getting consensus, and is now fully built in to the architecture of the feature. It cannot be changed.

  5. domenic commented on Oct 15, 2016

    @domenic
    Collaborator

    I hope you're aware that you can generate classes dynamically just as easily as functions, so you certainly would not need to type those out. Classes are just as much syntax as functions are. If you don't know how do do this, please ask on StackOverflow, but not here.

  6. rniwa commented on Oct 15, 2016

    @rniwa
    Collaborator

    FWIW, you can use Reflect.construct to call HTMLElement's constructor. e.g.

    function CustomElement() {
        return Reflect.construct(HTMLElement, [], CustomElement);
    }
    Object.setPrototypeOf(CustomElement.prototype, HTMLElement.prototype);
    Object.setPrototypeOf(CustomElement, HTMLElement);
    
    customElements.define('custom-element', CustomElement);
  7. Mr0grog commented on Oct 16, 2016

    @Mr0grog

    (Apologies if this issue has already been discussed elsewhere; I had entirely failed to consider it before and I haven’t seen it mentioned…)

    Will this cause problems for existing JS codebases that use a WebComponents polyfill with a transpiler like Babel? For example, transpiling this code using Babel’s es2015 preset fails to work because the resulting JS doesn’t use Reflect.construct:

    class TestElement extends HTMLElement {
      constructor () {
        console.log('Constructin’');
        super();
      }
      connectedCallback () {
        console.log('Connectin’');
      }
      disconnectedCallback () {
        console.log('Disconnectin’');
      }
    }
    
    customElements.define('test-element', TestElement);
    
    const testInstance = document.createElement('test-element');
    document.body.appendChild(testInstance);

    I understand that native custom elements won’t be available in browsers that don’t already support ES-2015 class syntax, but if someone is using Babel + a polyfill for web components, it seems like they’d have a situation where their code works in older browsers (because the polyfill is active), but not in newer ones (because the polyfill just defers to the native implementation). That seems like a pretty big practical problem, but is it one you are concerned about here?

  8. rniwa commented on Oct 16, 2016

    @rniwa
    Collaborator

    It is true that if you're using Babel and polyfill, then the above code won't work out-of-box but that's true of any polyfill that got written before the standard is finalized.

    There are various ways to workaround such issues, and probably the simplest solution is to wrap the thing you pass to customElements.define with a class. e.g.

    function defineCustomElementInBabel(name, legacyConstructor) {
        var wrapperClass = class extends legacyConstructor {
            constructor() {
                var newElement = new Reflect.construct(HTMLElement, [], wrapperClass);
                legacyConstructor.call(newElement);
                return newElement;
            }
        };
        customElements.define(name, wrapperClass);
    }

    Obviously, this leaves new TestElement non-functional. An alternative approach is to replace super() call in TestElement by something special like:

    class TestElement extends HTMLElement {
        constructor () {
            constructCustomElement(TestElement);
        }

    with

    function constructCustomElement(newTarget) {
            Reflect.construct(HTMLElement, [], newTarget);
    }

    There are dozens of other ways to cope with this limitations and that's really up to framework and library authors.

    On a broader note, I don't think the standards process or API design in standards should be constrained by polyfills written before the general consensus on the API shape has been reached and at least two major browser engines have implemented it. Also, deploying a polyfill on production before the standards have become stable is almost always a bad idea.

  9. Mr0grog commented on Oct 17, 2016

    @Mr0grog

    It is true that if you're using Babel and polyfill, then the above code won't work out-of-box but that's true of any polyfill that got written before the standard is finalized.

    I suppose I was really most focused here on the impact to existing codebases. It’s not as if that hasn’t been a consideration in other web standards, though I do understand that current usage of polyfills for custom elements (and especially v1-esque polyfills) is quite small.

    On the other hand, there is a lot of Babel usage out there (the majority of non-trivial JS codebases I’ve worked on as a consultant over the past year have used it), and I hadn’t really expected that I’d need such an awkward and specialized method for creating a custom element with it. It may be further complicated in trying to find solutions that allow someone to inherit from a custom element provided as a third-party module, where the provider of the component may have solved the issue in their own way. As you noted, there are many ways to work around it.

    Also, deploying a polyfill on production before the standards have become stable is almost always a bad idea.

    I agree! I’ve just spent a lot of time shaking my head at bugs I’ve had to fix for clients because they shipped code that depends on an alpha/beta version of a library or a polyfill for a standard that hasn’t been finalized yet, so I’m sensitive to these kinds of decisions.

    At the end of the day, I’m just a little frustrated at realizing the API for custom elements is less friendly than I had thought (again, entirely my fault for not reading as closely as I should have). I also understand that this is well past the point where anyone is willing to rethink it.

    (I also want to be clear that I really appreciate the work being done here by everyone on the working group. Obviously I would have liked this issue to turn out differently, but I’m not complaining that this is some horrible travesty. The big picture is still an improvement for the web.)

  10. rniwa commented on Oct 17, 2016

    @rniwa
    Collaborator

    On the other hand, there is a lot of Babel usage out there (the majority of non-trivial JS codebases I’ve worked on as a consultant over the past year have used it), and I hadn’t really expected that I’d need such an awkward and specialized method for creating a custom element with it.

    Okay. If you don't like a method, you can also define a specialized super class shown below. Obviously, this particular version of BabelHTMLElement only works with a browser engine with both ES6 and custom elements support but you can make it work with whatever polyfill as well.

    function BabelHTMLElement()
    {
      const newTarget = this.__proto__.constructor;
      return Reflect.construct(HTMLElement, [], newTarget);
    }
    Object.setPrototypeOf(BabelHTMLElement, HTMLElement);
    Object.setPrototypeOf(BabelHTMLElement.prototype, HTMLElement.prototype);
    
    class MyElement extends BabelHTMLElement {
      constructor() {
        super();
        this._id = 1;
      }
    }
    
    customElements.define('my-element', MyElement);
  11. rniwa commented on Oct 17, 2016

    @rniwa
    Collaborator

    Note that you can be more sleek with something like this (although I highly discourage you to override the native HTMLElement interface like this but sooner or later someone is gonna realize and do it so I'm gonna leave it here).

    HTMLElement = (function (OriginalHTMLElement) {
      function BabelHTMLElement()
      {
        if (typeof Reflect == 'undefined' || typeof Reflect.construct != 'function' || typeof customElements == 'undefined') {
          // Use your favorite polyfill.
        }
        const newTarget = this.__proto__.constructor;
        return Reflect.construct(OriginalHTMLElement, [], newTarget);
      }
      Object.setPrototypeOf(BabelHTMLElement, OriginalHTMLElement);
      Object.setPrototypeOf(BabelHTMLElement.prototype, OriginalHTMLElement.prototype);
      return BabelHTMLElement;
    })(HTMLElement);
    
    class MyElement extends HTMLElement {
      constructor() {
        super();
        this._id = 1;
      }
    }
    
    customElements.define('my-element', MyElement);
  12. rniwa commented on Oct 17, 2016

    @rniwa
    Collaborator

    @WebReflection: In the case, you're still looking for a solution that works in both Babel + Polyfill and native ES6 + custom elements, see the comment above ^

  13. WebReflection commented on Oct 17, 2016

    @WebReflection

    @rniwa thanks for mentioning me but I'm not sure it's so easy.

    Babel is plain broken when it comes to super calls and my poly already patches HTMLELement, so does the one from googlers.

    I strongly believe this should be solved on Babel side, otherwise we're blocking and degrading native performance because of tooling on our way.

    Tooling should improve and help, not be a problem.

  14. rniwa commented on Oct 17, 2016

    @rniwa
    Collaborator

    I've verified that both the ES6 and the Babel transpiled version works. The key here is to directly invoke Reflect.construct in your polyfill and not rely on Babel's super() call which, as you pointed out, is broken.

  15. 72 remaining items

  16. thecodejack commented on Dec 18, 2018

    @thecodejack

    I am able to use Reflect.construct to migrate all my V0 components to V1. But now I am facing issues while I am creating new V1 components using ES6 classes(babelified).

    Seems like all children with ES6 classes custom components are getting initialized correctly by the time browser calls connectedCallback but non class based child components which are built using Reflect.construct technique are not initialized in the connectedCallback of parent(which is ES6 class based component). Anyone faced this issue?

  17. rniwa commented on Dec 18, 2018

    @rniwa
    Collaborator

    Note that in general custom elements are upgraded in the tree order (prefix DFS), and connected & disconnected callbacks are enqueued in the tree order as well so in connectedCallback, for example, you shouldn't expect to be able to talk to your child elements / nodes. The recommended paradigm is for each child custom element to notify its parent. If you had to observe other kinds of changes or have to accept non-custom element, the recommendation is to use MutationObserver.

  18. thecodejack commented on Dec 19, 2018

    @thecodejack

    The recommended paradigm is for each child custom element to notify its parent.

    Is there any implementation or wrapper to handle after my child components are ready and attached? MutationObserver is not something which completes my requirement. At present I am having setTimeOut in my base component class like following which runs ready method only once. But this doesn't seem to be correct way.

    connectedCallback() {
       if (!this.isCustomInitialized) setTimeOut(()=>{this.ready()}, 0);
       this.isCustomInitialized = true;
    }
    ready() {
    // write code which runs after children custom components are ready with shadowDOM
    }
    
  19. rniwa commented on Dec 20, 2018

    @rniwa
    Collaborator

    MutationObserver is not something which completes my requirement

    Why? MutationObserver lets you observe when child elements are inserted or removed. That's exactly when you should be running the logic to update your container element.

    In general, the idea of all children being ready is flawed since an element can be inserted or removed dynamically.

  20. thecodejack commented on Dec 20, 2018

    @thecodejack

    I got your point of element insertion or removal but there is high probability the MutationObserver runs multiple times if there are multiple children that are active. Anyways I don't think need for lifecycle event after children being ready is a flaw. Even many major frameworks react (componentDidMount), vue (mounted), angular (afterView/afterContent) provides you similar functionality.

  21. daKmoR commented on Dec 20, 2018

    @daKmoR

    Even many major frameworks react (componentDidMount), vue (mounted), angular (afterView/afterContent) provides you similar functionality.

    componentDidMount is the same as connectedCallback right? it has no concept of completeness of rendering.

    Consider the following example:

    // connected to dom =>
    <foo-bar></foo-bar>
    // is it ready?
    
    // no as it needs to load some language data from an api =>
    <foo-bar><p>hey foo</p></foo-bar>
    // new we could say it's ready
    

    It seems only you as a component author can make sure you are done done... and you can use whatever means necessary to do so (Promise, Callback, Event, ...)

    PS: even more "crazy" example... assuming we take the finished loading of translations as being ready: an element that loads a form from an api which has some translations but also special input elements which need to load their own translations => so you could end up with the "form" being already ready (e.g. translations loaded) but the child elements are not (e.g. translations are not yet loaded) ... so to be truly ready you would need to check for every child elements readiness as well... => we are going down the rabbits hole 🙈

  22. thecodejack commented on Dec 20, 2018

    @thecodejack

    I haven't seen how react works internally much 😁 . I just tried logging componentDidMount and found that children's componentDidMount are called before parent's. But I am assuming due to virtual DOM that comes in react life cycle events, componentDidMount is happening after child DOM ready and connected as well.

    Regarding example, true that it is getting complicated. I think that's where frameworks like react or vue won the game over custom elements/webcomponents.

  23. AndyOGo commented on Dec 20, 2018

    @AndyOGo

    @thecodejack
    You have to be aware of one major caveat of custom elements, they are asynchronous. React is synchronous.

    @daKmoR
    So you can't say connectedCallback is the same as componentDidMount, there is this big difference of asynchronicity and others like V-DOM vs. the DOM.

    I really recommend to carefully study the whole spec to become familiar with all the caveats involved:
    https://html.spec.whatwg.org/multipage/custom-elements.html

  24. daKmoR commented on Dec 20, 2018

    @daKmoR

    @AndyOGo thx for clearing that up... so for the rendering componentDidMount will be sync.

    However, the example with the loading of translations (via fetch to an external api server) is still valid right? or will componentDidMount wait until my xhr is done?

  25. AndyOGo commented on Dec 20, 2018

    @AndyOGo

    @daKmoR
    You are welcome.

    Regarding loading of translations, yes it's still valid.
    Normally the workflow is as follows on componentDidMount() -> fetch() some data -> then call setState() -> will trigger componentDidUpdate, etc.

    I really like this interactive diagram for React Lifecycle hooks and would wish similiar for custom elements:
    http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/

  26. xgqfrms commented on Jul 3, 2020

    @xgqfrms

    what's wrong with that?

    Uncaught SyntaxError: Failed to execute 'define' on 'CustomElementRegistry': "ufo" is not a valid custom element name

    https://codepen.io/xgqfrms/pen/MWKrXpe?editors=1111

  27. WebReflection commented on Jul 3, 2020

    @WebReflection
  28. xgqfrms commented on Jul 3, 2020

    @xgqfrms

    @WebReflection Thanks a lot, I had fixed that.

      <ufo-element>👍 customElements.define("ufo-element", UFO);</ufo-element>
      <ufo>👎 customElements.define("ufo", UFO);</ufo>

    👍 customElements.define("ufo-element", UFO);
    👎 customElements.define("ufo", UFO);

  29. sapphous commented on Dec 2, 2020

    @sapphous

    @domenic @morewry I just want to pop in and say that this is ridiculous. Javascript is a prototype-based language, and to get "consensus" it was necessary to block a critical feature from being possible using the prototype paradigm--and you refuse to fix it when it causes problems because of some sort of politics? It's nice that we have the option to use nice ES6 class syntax... but it's less powerful than raw prototyping and it's simply mind-boggling to me that my search for a solution led me to "there is none, because a committee banned it to force you to code their way even though the core features you're using aren't going anywhere".

    Sometimes features are deprecated and things aren't backwards compatible; sometimes poor design introduces obstacles; but "sometimes a committee wants everyone to code a certain way for no particular reason and introduces obstacles to using perfectly supported features in a given context on purpose"? Is Javascript deprecated? Or is there a plan to eliminate prototypes from the language and move to a purely class-based approach--then perhaps add in strong typing and C++ template syntax? If so, that's crazy; if not, then the lack of support for prototype-based custom components is a serious bug.

    Is there no way this can be brought up and addressed now that 4 years have passed? If the only reason there's no solution is that the design was intentionally crippled by bureaucratic fiat at a face-to-face meeting half a decade ago, perhaps it could be fixed now?

    I guess I will now proceed to implement a bizarre, unreadable, inefficient workaround or introduce some hideous class definitions into my otherwise class-free library, for no reason whatsoever except that "the owners of the internet don't like prototypes".

  30. bathos commented on Dec 2, 2020

    @bathos

    @sapphous Class syntax is itself part of the “prototype paradigm,” but as others have mentioned, you don’t actually have to use it:

    function NoClassSyntaxElement() {
      return Reflect.construct(HTMLElement, [], new.target);
      // or Reflect.construct(HTMLElement, [], NoClassSyntaxElement), I suppose, if you don’t care about subclassing
    }
    
    NoClassSyntaxElement.__proto__ = HTMLElement;
    NoClassSyntaxElement.prototype.__proto__ = HTMLElement.prototype;
    
    customElements.define('no-class-syntax', NoClassSyntaxElement);
    
    console.log(new NoClassSyntaxElement().matches(':defined')); // true

    This is useful if you need the super reference to be static but need to leave [[IsExtensible]] true (i.e., mimicking the behavior of platform interface constructors that inherit from others). That is pretty niche, mainly of interest for high-fidelity polyfilling stuff, but if for whatever reason you are averse to using class syntax to define your prototypes and object factories, it could serve you well too, and can be tucked away in a helper function.

    Class syntax is a mechanism for defining prototypes declaratively alongside any initialization work involved in minting new instances. It doesn’t prevent manipulating the prototype and its properties; you can still do everything you might do in its absence. There is one (fairly obscure) primitive capability that class syntax has that is not exposed any other way* and there is one (fairly obscure) primitive capability that function syntax has that class syntax doesn’t**, but apart from these two things, they are exactly equal in capability.

    Not trying to convince you to use em, just mentioning this because “less powerful than raw prototyping” seems like it might be a misconception (if you meant powerful in the sense of what fundamental capabilities they permit).

    Not super important, but if curious, the two capability disconnects are...
    • * Class syntax is the only way to define a function whose [[ConstructorKind]] is "derived", which means you can skip or defer the initial access of new.target.prototype, which is technically observable. This is not possible with function syntax unless you count using a Proxy construct trap to achieve it.

    • ** Function syntax allows defining a function that has [[Construct]] but whose prototype property is initially writable, while class syntax does not.

    When private fields land, the situation will change — that’s a pretty major primitive capability which, at least initially, will only be exposed through class syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions