7

I am using ES5 to make a custom web component that extends HTMLAnchorElement. Since web components only work in ES6 and up I have included the ES5 polyfill located here.

Making a web component that extends HTMLElement and then using its tag on the page works fine. But extending any element other than HTMLElement does not seem to work.

I am using the latest version of Google Chrome to test this.

Below is my code for the element that is extending HTMLAnchorElement.

function CustomAnchor() {
    HTMLAnchorElement.call(this);
}

CustomAnchor.prototype.connectedCallback = function () {
    console.log('anchor added to dom');

    this.addEventListener('click', 
        function(e) {
            console.log('test');
            e.preventDefault();
            var result = confirm('Confirmed!');
        }
    );
};

window.customElements.define('custom-anchor', CustomAnchor, {extends: 'a'});

And here is the code that creates the element and adds it to the DOM:

var el1 = document.createElement("a")
el1.setAttribute("is", "custom-anchor")
el1.setAttribute("href", "http://www.google.com/")
el1.text = "Go to Google"
document.body.appendChild(el1)

And here is what shows up in the DOM, which looks correct:

<a is="custom-anchor" href="http://www.google.com/">Go to Google</a>

No errors are showing up in the console but when the element is appended to the DOM the connectedCallback is never fired.

The link works just like a normal anchor link as if it had never been extended.

What am I doing wrong here?

Graham
  • 5,488
  • 13
  • 57
  • 92
  • 1
    None of this is correct. 1st, you cannot extend specific elements, just HTMLElement. 2nd, why are you using ES 5 with the adapter in the latest chrome? Use classes like the spec requires. Use the shim you linked and babel for old browsers. Side note: I have trouble with the connectedCallback firing too early, I usually wrap the contents in a `setTimeout` of 0. See [this](https://github.com/shawnbot/custom-elements#class-definition) for more info. – Jared Smith Nov 28 '17 at 19:31
  • 1
    @JaredSmith I'm actually using Kotlin which is why I need the ES5 adapter. I'm showing you guys almost exactly what the Kotlin compiler outputted. Lastly, you **can** extend specific elements. It is part of the spec. I suggest you do some reading to bring yourself up to speed. – Graham Nov 28 '17 at 19:37
  • 1
    @JaredSmith here you go, this is what you need to read: https://w3c.github.io/webcomponents/spec/custom/#custom-elements-customized-builtin-example – Graham Nov 28 '17 at 19:43
  • 2
    I stand corrected. Have a +1. However its not implemented (or if it is it's behind a flag), doing `class Foo extends HTMLButtonElement` throws an illegal constructor error on the call to `super` in Chrome 62. I would also advise against trying to do bleeding-edge browser stuff in a language other than JavaScript unless its a POC: too hard to tell where in the process something went wrong. I can tell you from painful personal experience that the devtools error messages for brand new stuff (e.g. modules, native custom elements) are more opaque than their more well-worn counterparts. – Jared Smith Nov 28 '17 at 19:46
  • Thanks for the suggestion. I just tried running the example from the link I posted and it appears that you're right. Thanks for the help. – Graham Nov 28 '17 at 19:56
  • Possible duplicate of [How to create new instance of an extended class of custom elements](https://stackoverflow.com/questions/39986046/how-to-create-new-instance-of-an-extended-class-of-custom-elements) – Supersharp Nov 29 '17 at 21:31

1 Answers1

3

As of Nov 28, 2017 extending anything besides HTMLElement is not supported in any browser. The idea of <button is="my-button">Click Me</button> is not supported in Native V1 components.

For more info read the section Extending native HTML elements found here: https://developers.google.com/web/fundamentals/web-components/customelements

it states: "Warning: At time of writing, no browser has implemented customized built-in elements (status). This is unfortunate for accessibility and progressive enhancement. If you think extending native HTML elements is useful, voice your thoughts on Github."

UPDATE 1:

As of May 28 2018 Chrome 67 supports Customized built-in elements And Firefox 63 claims full support too.

Sometime in Sept or Oct 2018 MS started working on their support for Custom Elements and Shadow DOM in Edge. But there is no indication on when it will be finished.

Intervalia
  • 10,248
  • 2
  • 30
  • 60
  • Would you please provide a source for this? – Graham Nov 29 '17 at 20:34
  • in my answer here: https://stackoverflow.com/questions/47510957/how-do-you-decouple-web-components/47521377#47521377 I have provided the source for two simple Web Components. – Intervalia Nov 30 '17 at 03:23