11ty / webc

Single File Web Components
MIT License
1.3k stars 36 forks source link

[Bug] Build error when slotting a component into another with `webc:setup` script #205

Open hasanhaja opened 2 months ago

hasanhaja commented 2 months ago

I was trying to create a reusable <select> component, and my naive approach was to use slots, but that didn't work. I've resorted to using a render function within my custom select WebC component and it works now. However, to be generic, I'm trying to pass data in as an object and dynamically render it on the other side. The reproduction and the workaround I've found are at the bottom of this issue.

Bug

<!-- Main component -->
<script webc:setup>
  function options() {
    return [
      "1",
      "2",
      "3",
      "4",
    ];
  }
</script>

<cr-fieldset>
  <cr-select
    name="motivation" 
    :@options="options()"
  ></cr-select>
</cr-fieldset>
<!-- cr-fieldset -->
<fieldset :data-required="required">
  <legend>
    <h2 @text="title"></h2>
    <p @text="label"></p> 
  </legend>
  <slot></slot>
</fieldset>
<!-- cr-select -->
<select
  :name="name"
>
  <option value="">Please select</option>
  <script webc:type="js" webc:root>
    options
      .map((value) => `<option value="${value}">${value}</option>`)
      .join("\n");
  </script>
</select>

This results in the follow error:

11ty-build-error

Reproduction

Reproduction commit: https://github.com/darthmall/webc-select-options-setup-func-repro/pull/1/commits/247df02bac1e017f935bdbde5f94cc61d7b66cee

I managed to get around the issue by pulling the setup script and <cr-select> to a separate file.

Workaround commit: https://github.com/darthmall/webc-select-options-setup-func-repro/pull/1/commits/df8ef9be1dcc2e94d8a84abcf89713e1b77b538a