m.dellanave
m.dellanave

Reputation: 71

ES6 Custom Elements Inheritance

I'm trying to use inheritance in custom elements.

That's my page

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title></title>
</head>

<body>
  <base-element></base-element>
  <language-drop-down></language-drop-down>

  <script type="module" src="./custom-elements/BaseHTMLElement.js"></script>
  <script type="module" src="./custom-elements/language-drop-down/js/LanguageDropDown.js"></script>
</body>

</html>

The base custom element

    export default class BaseHTMLElement extends HTMLElement {

    get currentDocument() { return document.currentScript.ownerDocument };

    constructor(params) {
        super();

        this.params = params;

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

        // Select the template and clone it. Finally attach the cloned node to the shadowDOM's root.
        // Current document needs to be defined to get DOM access to imported HTML
        if(this.params && this.params.templateName){
            this.template = this.currentDocument.querySelector(this.params.templateName);
            this.instance = this.template.content.cloneNode(true);

            this.shadowRoot.appendChild(this.instance);
        }else{
            let div = document.createElement("DIV");
            div.innerHTML = "element without template";
            this.shadowRoot.appendChild(div);
        }
    }
    connectedCallback() {
        this.loadLocalStrings();
    }
    childrenChangedCallback() {

    }
    disconnectedCallback() {

    }
    attributeChangedCallback(attrName, oldVal, newVal) {

    }

    loadLocalStrings() {

    }
   }

   window.customElements.define('base-element', BaseHTMLElement);

And the language drop-down custom element

import * as BaseHTMLElement from '../../BaseHTMLElement.js';

class LanguageDropDown extends BaseHTMLElement {

    constructor() {
        super({
            templateName: '#language-drop-down-template'
        });  
    }
    connectedCallback() {

        let dropdown = this.shadowRoot.querySelectorAll('.dropdown');        
        $(dropdown).dropdown();
    }
    childrenChangedCallback() {

    }
    disconnectedCallback() {

    }
    attributeChangedCallback(attrName, oldVal, newVal) {

    }
};

window.customElements.define('language-drop-down', LanguageDropDown);

with its template part

<template id="language-drop-down-template">
    <link href="./custom-elements/language-drop-down/css/languageDropDown.css" rel="stylesheet" />

    <div class="dropdown">
        <button class="btn dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Dropdown button
        </button>
        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
            <a class="dropdown-item" href="#">Action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <a class="dropdown-item" href="#">Something else here</a>
        </div>
    </div>
</template>

<script type="module" src="../BaseHTMLElement.mjs"></script>
<script type="module" src="./js/languageDropDown.mjs"></script>

Now, in chrome latest release it should work without compiling/using polyfill right? My problem is this message in the console

Uncaught TypeError: Class extends value [object Module] is not a constructor or null

I don't understand where I'm wrong... does someone have an idea?

All that because I would like to avoid using HTML import because it's deprecated and will be removed in M73.

If I use

<script src="./custom-elements/BaseHTMLElement.js"></script>
<link rel="import" href="./custom-elements/language-drop-down/LanguageDropDown.html">

And remove export default BaseHTMLElement ... import * as BaseHTMLElement from '../../BaseHTMLElement.js';

it works fine!

Upvotes: 4

Views: 1726

Answers (2)

m.dellanave
m.dellanave

Reputation: 71

I found that using

import BaseHTMLElement from '../../BaseHTMLElement.js';

makes it work

Upvotes: 1

Bergi
Bergi

Reputation: 665276

Your line

import * as BaseHTMLElement from '../../BaseHTMLElement.js';

is wrong. That script exports the class as its default export. You are importing all exports as a module namespace object here - and when trying to use it like a class in the class extends clause, you get "[object Module] is not a constructor or null".

You should use

import {default as BaseHTMLElement} from '../../BaseHTMLElement.js';

or

import BaseHTMLElement from '../../BaseHTMLElement.js';

Upvotes: 3

Related Questions