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

No access to host component properties within slotted content #214

Open
renekaesler opened this issue Aug 21, 2024 · 0 comments
Open

No access to host component properties within slotted content #214

renekaesler opened this issue Aug 21, 2024 · 0 comments

Comments

@renekaesler
Copy link

It is not possible to access the properties of the host component within a slot of another component. According to HTML behavior, slotted content belongs to the context of the host component. Properties should therefore also be available within slotted content.

version: 0.11.4
maybe related: Issue 205

Example

<!-- components/c-faq.webc -->
<c-section :@title="title">
  <details webc:for="question of questions">
    <summary @text="question.question"></summary>
    <p @text="question.answer"></p>
  </details>
</c-section>
Related Files
<!-- components/c-section.webc -->
<section>
  <h1 @text="title"></h1>
  <slot></slot>
</section>
<!-- index.webc -->
<c-faq 
  @title="Frequently Asked Questions" 
  :@questions="[
    { question: 'Question #1', answer: 'Answer #1' },
    { question: 'Question #2', answer: 'Answer #2' },
    { question: 'Question #3', answer: 'Answer #3' },
  ]"
></c-faq>
// index.js
import { WebC } from "@11ty/webc";

const page = new WebC();

page.setBundlerMode(true);
page.setInputPath("index.webc");
page.defineComponents("components/*.webc");

const { html } = await page.compile();

console.log(html);

Expected Behavior

The Example should compile to:

<section>
  <h1>Frequently Asked Questions</h1>
  
  <details>
    <summary>Question #1</summary>
    <p>Answer #1</p>
  </details>
<details>
    <summary>Question #2</summary>
    <p>Answer #2</p>
  </details>
<details>
    <summary>Question #3</summary>
    <p>Answer #3</p>
  </details>

</section>

Actual Behavior

The example compiles to:

<section>
  <h1>Frequently Asked Questions</h1>
  
  [object Object]

</section>

Workaround

If we pass :@questions="questions to the component, we can achieve the expected result.

<!-- components/c-faq.webc -->
<c-section :@title="title" :@questions="questions">
  <details webc:for="question of questions">
    <summary @text="question.question"></summary>
    <p @text="question.answer"></p>
  </details>
</c-section>
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