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

How to access slot content in js template? #105

Open
awmottaz opened this issue Nov 12, 2024 · 1 comment
Open

How to access slot content in js template? #105

awmottaz opened this issue Nov 12, 2024 · 1 comment

Comments

@awmottaz
Copy link

Suppose I'm using a WebC component like this:

<!-- page.webc -->

<my-component>
  My content is here
</my-component>

I want to use JavaScript to generate the output of my-component. How do I access the default slot content (or any named slot for that matter)?

<!-- my-component.webc -->

<script webc:type="js" webc:root>
// How do I use the default slot here?
// This definitely won't work, but illustrates what I'm trying to do
// The output when used like above should be
//   <div>My content is here</div>
`<div>${slot.default}</div>`;
</script>

I could keep everything in attributes and props as a workaround, but for various reasons it would be more ergonomic in my case to use a slot (you get syntax highlighting there, for example).

@awmottaz
Copy link
Author

awmottaz commented Nov 12, 2024

Wow, I accidentally figured out that slots.text contains .default for the default slot, and I assume it will also have keys for named slots. So the above example works if I do this:

<!-- my-component.webc -->

<script webc:type="js" webc:root>
`<div>${slots.text.default}</div>`;
</script>

Can we get documentation for this, please? And what other keys could appear on the slots object besides text?

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

No branches or pull requests

1 participant