4

I have created a web component, and would like to access the elements from within the component.
Am using .attachMode({mode:'closed'}), so the parent has no access.

<template id='hello-world-template'>
  <span id='inside'>Unchanged,</span> <span id='outside'>Unchanged</span>
  <script>
    document.querySelector('#inside').innerHTML = 'Changed';
    // Ideal, but does not work - no such element 
  </script>
</template>
<hello-world></hello-world>

<script>
customElements.define('hello-world',
   class extends HTMLElement {
      constructor() {
         super();
         var template = document.getElementById('hello-world-template')
         var clone = template.content.cloneNode(true)
         const shadowRoot = this.attachShadow({mode: 'closed'}).appendChild(clone);
         }
      connectedCallback() {
        this.shadowRoot.querySelector('#outside').innerHTML = 'Changed';
        // Not ideal, and also does not work - this.shadowRoot has no querySelector
        }
      });
</script>

Some attempts:

  1. Within the document fragment - this, self, window, and document all refer to the parent window. And none can access the shadow root.
  2. Tried to store the shadowroot in a global variable and access it from inside the fragment or connectedCallback.
    Even if that worked, it would defeat the point of using {mode:'closed'}, but anyways it did not work.

I have a hack that works, but cannot imagine that I have to use it.
The whole point of encapsulation is that things can be self contained, but what good does that do us if the JS cannot act on the other items in its container?

If this is the solution, would love a tip to explain the logic of the way components where implemented.
Here's the hack, though: include an image that runs the JS onload.

<template id='hello-world-template'>
  <span id='inside'>Unchanged,</span> <span id='outside'>Unchanged</span>
  <script>
    function runner(img){
       let doc = img.parentNode;
       doc.querySelector('#inside').innerHTML = 'Changed';
       }
  </script>
  <img src='data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=' onload="runner(this)">
</template>
<hello-world></hello-world>

Note regarding similar questions (25048359, 16633057, 55101967, etc.) - those answers will not work when mode is closed, which I need.

SamGoody
  • 13,758
  • 9
  • 81
  • 91

1 Answers1

2

Looks like you have errors in both Element references and this Scope (in <script>)

const shadowRoot = this.attachShadow({mode: 'closed'}).appendChild(clone);

appendChild is your Nemesis here.

It returns the inserted element... not a shadow-root but a #document-fragment (cloned template)

fixed with:

 const shadowRoot = this.attachShadow({mode: 'closed'});
 shadowRoot.appendChild(clone);

Then:

  • mode:closed will not assign this.shadowRoot ..
    which you can not re-use because it still is a read-only property

fixed with:

 this.Root = this.attachShadow({mode: 'closed'});
 this.Root.appendChild(clone);

You can now do:

  connectedCallback() {
    this.Root.querySelector('#outside').innerHTML = 'Changed';
  }

I do not understand why you consider this not ideal
this.Root is accessible from all/any methods inside the Component

A good resource for all DOM methods is: https://javascript.info/modifying-document


<script> inside a <template> (this scope)

<template id='hello-world-template'>
  <span id='inside'>Unchanged</span>
  <script>
    document.querySelector('#inside').innerHTML = 'Changed';
    // Ideal, but does not work - no such element 
  </script>
</template>

You injected the template into a Component
document can not access elements inside any Component shadowDOM
doesn't matter if the shadowDOM is mode:closed or mode:open

The <script> scope will be window (since it wasn't assigned a scope)

(I do not think you can set the this scope for a SCRIPT)

To get the Component scope inside that <script> you have to be creative ..
and use your img onload= 'hack'

With the onload on the <style> element makes it a bit less of a hack (IMHO)

<template id='hello-world-template'>
  <span id='inside'>Inside Unchanged,</span>
  <script>
    function templFunc() {
      // this scope is the shadowRoot, not the component!
      this.querySelector('#inside').innerHTML = 'Changed';
    }
  </script>
  <style onload="templFunc.apply(this.getRootNode())">
    #inside{
      color:green;
    }
  </style>
</template>

one major issue: will only execute for the first used <hello-world> element!!

I didn't write this of the top of my head,
(Work in progess) JSFiddle playground (also showing referencing Component methods) at:
https://jsfiddle.net/CustomElementsExamples/zpamx728/

Update #1

Chromium (Edge/Chrome) and Opera are fine, FireFox v72.0.2 misbehaves:

  • the onload on a <STYLE> element will only fire for the first element
    I changed the JSFiddle to use your first hack using an <img>, and it fires for every element

  • templFunc() is loaded/scoped in shadowDOM, so is callable from component methods (see JSFiddle)
    but.. only in FireFox is NOT defined/available for the first element
    For now I consider this a FireFox bug... will investigate further... (to boldly go where...)

!!! Update #2 !!!

OOPS! Played some more with it.

Turns out all variables and functions from a cloned and imported SCRIPT
are hoisted to the global window scope

So above code works, but has a major side-effect...

That is also why FireFox complains about re- declaring let variables

Danny '365CSI' Engelman
  • 16,526
  • 2
  • 32
  • 49
  • That was a very clear answer, thank you! What do you mean _"this.Root is accessible from all/any methods inside the Component"_? `console.log(this.Root)` inside the component returns undefined. Do you mean the parent can access all elements inside the component? – SamGoody Feb 06 '20 at 23:51
  • see the [JSFiddle](https://jsfiddle.net/CustomElementsExamples/zpamx728/) ``this.Root`` is set in the ``constructor`` then used in both ``connectedCallback`` and ``Amethod`` methods. – Danny '365CSI' Engelman Feb 07 '20 at 08:49
  • I saw the fiddle, but am still not clear. Both connectedCallback and Amethod are defined outside of the component (in Window). If Amethod was defined inside the component, it cannot access this.Root, correct? – SamGoody Feb 07 '20 at 09:53
  • _not ideal_ - because I want all of my component code to be together in the component (similar to the CSS which is inside the component). And because if I create a reference (this.Root) which has the same permissions as this.shadowRoot({mode:'open'}), then I might has well have just left shadowRoot with {mode:'open'}. If this is in error, then please do say! – SamGoody Feb 07 '20 at 09:58
  • Not quite sure what you are after.... is [this JSFiddle](https://jsfiddle.net/CustomElementsExamples/wb4q7sre/) 'closed' enough? No more ``connectedCallback`` The ``constructor`` loads the Template (*which you call the Component? I would say the Class is the Component*) Thus executes the ``render`` function from the Template Script in the component **instance** context/scope. Everything inside that Template Script is private; **optionally** you can add Public methods (declared **inside** the render function so you can access your element privates) – Danny '365CSI' Engelman Feb 07 '20 at 11:39