lwc icon indicating copy to clipboard operation
lwc copied to clipboard

Deprecate `this.template` in favor of `this.shadowRoot`

Open nolanlawson opened this issue 2 years ago • 3 comments

When LWC was first released, this.template made sense – it referred to the (shadow DOM) template. Today it roughly means the same thing as this.shadowRoot, since it is undefined for light DOM components. (E.g. developers have to remember do to this.querySelector() in light DOM and this.template.querySelector() in shadow DOM.)

As part of hewing closer to the standards, I believe we should deprecate this.template in favor of this.shadowRoot. If you access this.template you should get a warning, and eventually it should be removed.

nolanlawson avatar Feb 13 '23 20:02 nolanlawson

the problem is that this.shadowRoot is null for closed mode. this is why we have a private field (template) that can always be around, even for closed shadows.

caridy avatar Feb 13 '23 21:02 caridy

@caridy In practice LWC almost always runs in open mode, but your point is well taken. In closed shadow environments, I think exposing ElementInternals somehow would make sense:

renderedCallback() {
  console.log(this.internals.shadowRoot) // or something. Maybe this.#internals
}

nolanlawson avatar Feb 13 '23 21:02 nolanlawson

yeah, I suspect that will happen at some point. keep in mind that this.template predates the notion of internals.

caridy avatar Feb 14 '23 15:02 caridy