11ty / 11ty/webc

WebC: Cannot use "slot" content from parent component

Open
#167 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
1.4k
Forks
46
Avg merge
10m
Merged PRs (30d)
2

Description

I think this belongs here, but maybe it should go to the WebC plugin or WebC itself. Please advise if that's the case.

I want to create a code snippet component that will include the highlighted code and a Copy button. Ex:

<syntax-highlight :language="language" webc:import="npm:@11ty/eleventy-plugin-syntaxhighlight"><slot></slot></syntax-highlight>

<button class="Button Button--outline">Copy to Clipboard</button>
<script>// script to copy to clipboard</script>

I expect that I should be able to use it as such:

<code-snippet language="html"><h1>Hello World!</h1></code-snippet>

And that it should result in

<pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>Hello World<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span></code></pre>

<button class="Button Button--outline">Copy to Clipboard</button>
[... script stuff somewhere...]

Instead, the <slot></slot> is what is highlighted.

I'm hoping to be able to nest this <code-snippet> component in another component that would also render a real-life example of the code being shown. Ex:

<div class="DemoCode">
<h1>Hello World!</h1>
</div>

<div class="CodeView">
    <pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>Hello World<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span></code></pre>

    <button class="Button Button--outline">Copy to Clipboard</button>
    [...script stuff again...]
</div>

Is there a way I can pass the slot content of the parent component into this webc component? I tried @raw/@html/@text, and that didn't seem to work. The one thing I was able to do was essentially copy the content of the webc component definition in my component, but this seems less-than-ideal.

Thanks!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked syntax-highlight.webc component definition and the WebC slot handling described in the issue; compare how @raw, @html, and @text treat content passed from a parent component. Done means a code-snippet component can pass its parent slot content into the syntax-highlighting WebC component while preserving the expected highlighted output.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.