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

Components don’t have access to props data within webc:setup script tags #196

Open
patrulea opened this issue Oct 19, 2023 · 4 comments
Open

Comments

@patrulea
Copy link

I can’t figure out why I can’t reference component props when trying to set up data with <script webc:setup>, as it just crashes the build.

[11ty] Original error stack trace: TypeError: Cannot read properties of undefined (reading 'ext')

index.webc

<some-component @ext="mp4"><some-component>

some-component.webc

<script webc:setup>

  let pictureFormats = ["apng", "avif", "gif", "jpg", "jpeg", "png", "svg", "webp"];
  let videoFormats = ["mp4", "webm"];
  let ext = this.ext; // data.ext, $data.ext, ext wouldn’t work either

  let type;
  if (pictureFormats.includes(ext)) {
    type = "picture";
  } else if (videoFormats.includes(ext)) {
    type = "video";
  }

</script>

How should I approach this?

@estherbrunner
Copy link

I had the same problem. I was expecting that the webc:setup should have access to attributes and properties of the component as well as in webc:root.

Anyhow, I figured out, I can pass the variable from evaluated attributes like @text="value(color)" and work around like this:

<script webc:setup>
    // const [l, c, h] = color.split(' '); // does not work!
    const value = (color) => {
        const [l, c, h] = color.split(' ');
        return `${Math.round(l * 1000) / 10}%, ${c}, ${h}°`
    };
    const css = (color) => `oklch(${color})`;
    const lightness = (color) => `${Math.round(color.split(' ')[0] * 1000) / 10}%`;
    const chroma = (color) => `${color.split(' ')[1]}`;
    const hue = (color) => `${color.split(' ')[2]}°`;
</script>

@alexnozer
Copy link

I have the same issue. I created a component and wanted to do some props processing in webc:setup before using it in a template. But it didn't work. I had to create a custom JavaScript function using eleventyConfig.addJavaScriptFunction API and call it inside the template.

@patrulea
Copy link
Author

@alexnozer can you provide some example code?

@tobystokes
Copy link

Oof, this got me too.

@patrulea in your example:

some-component.webc

You do have access to 'ext' in html, not setup: <span @text="ext"></span>
So pass it as an argument to a function: <span @text="type(ext)"></span>

<script webc:setup>

  let pictureFormats = ["apng", "avif", "gif", "jpg", "jpeg", "png", "svg", "webp"];
  let videoFormats = ["mp4", "webm"];

  const type = (ext) => {
    if (pictureFormats.includes(ext)) {
      type = "picture";
    } else if (videoFormats.includes(ext)) {
      type = "video";
    }
    return type;
  }

</script>

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

4 participants