Re: [WICG/webcomponents] HTML Modules (#645)

This may require its own champion and proposal/explainer, but I feel like it falls well enough in line that it might be worth mentioning: Scoping of JavaScript references inside of `<template>`, potentially to their accompanying inline `<script type="module">` tags. This would alleviate under-utilization of inline JavaScript as it is right now. Being able to use `on*=` attributes, and others, while referencing the included class would be incredibly useful.

To elaborate slightly (before rushing to dinner), the actual mechanism may include waiting to evaluate JS references in attributes for children of a `template` tag until the ShadowDom is registered. Even if all that is done is that the context (`this`) is set to the currently running instance, that would be a huge win to Web Components, imho.

```javascript
<template id="myCustomElementTemplate">
    <!-- ... -->
</template>
<script type="module">
    /* ... */
    class myCustomElement extends HTMLElement {
        constructor() {
            super();
            // ShadowDOM is attached to instance of myCustomElement here
            let shadowRoot = this.attachShadow({ mode: "open" });
            let template = importDoc.getElementById("myCustomElementTemplate");
            // The `this` of any children of a ShadowRoot will be equal to the instance of the
            // element, allowing access to public **and private** methods
            shadowRoot.appendChild(template.content.cloneNode(true));
        }
    }
    /* ... */
```

Again, this may very well need to be in its own proposal

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/645#issuecomment-1114372075
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/645/1114372075@github.com>

Received on Monday, 2 May 2022 00:02:30 UTC