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

Support dynamic :webc:is value #143

Open
m4rrc0 opened this issue Mar 25, 2023 · 5 comments
Open

Support dynamic :webc:is value #143

m4rrc0 opened this issue Mar 25, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@m4rrc0
Copy link

m4rrc0 commented Mar 25, 2023

Hey! Doesn't seem like an urgent fix but...
This does not work properly (or at least not as I expected):

<div :webc:is="'my-component'" @test="Dynamic comp name does not pass props"></div>

The component renders but the test prop is not passed.

The workaround is probably to use a script.

<script webc:type="js">
  `<${"my-component"} @test="Template string comp name works"></${"my-component"}>`
  + // OR
  `<div webc:is="${"my-component"}" @test="Template string comp name works"></div>`;
</script>

Both work as expected.

My use case was the following:
I wanted to loop over an array of data with each entry containing the name of the component to be used and the props to be passed to said component.
I was trying something like this:

<div
  webc:for="block of blocks"
  :webc:is="block.tag"
  @attributes="block"
></div>

It looks super neat and compact! (but it does not work 😭😄)

@m4rrc0
Copy link
Author

m4rrc0 commented Mar 26, 2023

Update: I currently landed on this syntax.

<script webc:type="js" webc:for="block of blocks">
  const { tag = "div", text, html, raw, ...attributes } = block;
  const a = JSON.stringify(attributes).replaceAll('"', "'");

  const t = text ? `@text="'${text}'"` : "";
  const h = html ? `@html="'${html}'"` : "";
  const r = raw ? `@raw="'${raw}'"` : "";

  `<${tag || "div"} @attributes="(${a})" ${t || h || r}></${tag || "div"}>`;
</script>

Which I must say is still cool and compact!
I am not sure how well it will hold for real world data but it is a start.

Note: As you can see, there is still some messing around to be done with the @ attributes but I guess it is another topic.

@zachleat
Copy link
Member

Related #148

@zachleat zachleat added the enhancement New feature or request label Mar 30, 2023
@zachleat
Copy link
Member

zachleat commented Mar 31, 2023

Note that the only current dynamic webc: attribute currently supported is webc:bucket #120. More to come!

@zachleat zachleat changed the title Props are not passed when using dynamic :webc:is value Support dynamic :webc:is value Mar 31, 2023
@m4rrc0
Copy link
Author

m4rrc0 commented Mar 31, 2023

Just to make sure I was clear... Note that it seems to work when we don't need props.

<div :webc:is="'my-component'"></div>

@mirisuzanne
Copy link

mirisuzanne commented Dec 3, 2023

Ran into this today, and it took me way too long to find my way through with a workaround:

<span 
  webc:if="this.logo" 
  webc:type="11ty" 
  11ty:type="liquid" 
  webc:nokeep 
  webc:raw
>{% render "logos/{{ logo }}.svg" %}</span>
  • If I use a <template> instead of <span>, I get a random-seeming fragment of page-layout code. I even tried passing in an empty layout variable, but that didn't help. Changing to a span fixed it. Not sure why?
  • If I leave off webc:raw, I get escaped svg code rather than a rendered svg. Again, I'm not sure I know why?

Hope that's helpful for someone. This would be an extremely useful feature in WebC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants