Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove implied webc:raw on <template> and <noscript> #105

Closed
darthmall opened this issue Feb 14, 2023 · 2 comments
Closed

Remove implied webc:raw on <template> and <noscript> #105

darthmall opened this issue Feb 14, 2023 · 2 comments
Labels
breaking-change bug Something isn't working
Milestone

Comments

@darthmall
Copy link

I’m not sure if this is a bug, but it took me by surprise.

WebC doesn’t seem to be processing the contents of <template> tags as WebC, which means if I have something like an HTML-only component that I want to land in a <template> tag in the final output, I have to do a little dance with webc:is (more on that in a minute).

Example

test-p.webc

<p>I am an HTML-only component</p>

test-component

<template>
  <test-p></test-p>
</template>

Expected Output

<template>
  <p>I am an HTML-only component</p>
</template>

Actual Output

<template>
  <test-p></test-p>
</template>

I would’ve expected <test-p> to only survive the WebC process if I’d used <template webc:raw> to signal that I didn’t want the template processed by WebC.

Workaround

You can achieve the desired output using webc:is like this:

test-component.webc

<div webc:is=template>
  <test-p></test-p>
</div>

Maybe this is actually desirable, but it took a little experimenting to figure this out.

Motivation

This came up because I was building a <theme-toggle> component for my website with WebC. I wanted the toggle buttons inside a <template> so that they’d only become visible in the DOM once the web component was upgraded (because they don’t do anything without JavaScript). I put some SVGs in .webc files for the icons in the toggle buttons.

theme-toggle.webc

<template>
  <button>
    <icon-moon></icon-moon>  <!-- icon-moon.webc contains SVG for a moon icon -->
    <span>Dark</span>
  </button>
  <!-- Buttons for system and light mode followed the same pattern -->
</template>
<script>
customElements.define(
  "theme-toggle",
  class extends HTMLElement {
    constructor() { /* Set up shadow DOM */ }
    connectedCallback() { /* Load template tag into shadow DOM */ }
  }
);
</script>
zachleat added a commit that referenced this issue Feb 16, 2023
…r declarative shadow dom

<template> and <noscript> will now process WebC content (unless webc:raw)

Related: #31 #103 #105

Fixes #108
@zachleat
Copy link
Member

I think this is sufficiently confusing that I’m going to push some changes. If you’re curious why this was added you can look at #31 but there is too much magic going on here.

So, moving forward in WebC v0.9.0 we’re going to have full WebC processing on <template> and <noscript> content (including slot resolution!), you can opt-out of this as normal with webc:raw.

@zachleat zachleat added this to the WebC v0.9.0 milestone Feb 16, 2023
@zachleat zachleat added the bug Something isn't working label Feb 16, 2023
@zachleat
Copy link
Member

zachleat commented Feb 16, 2023

Also notably, this change also means that for consistency webc:is="template" will operate the same as <template> in WebC v0.9.0—opting out of this processing will require webc:raw moving forward.

@zachleat zachleat changed the title Processing WebC components inside template tags Remove implied webc:raw on <template> Feb 22, 2023
@zachleat zachleat changed the title Remove implied webc:raw on <template> Remove implied webc:raw on <template> and <noscript> Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants