11ty / eleventy-plugin-webc

Adds support for WebC *.webc files to Eleventy
https://www.11ty.dev/docs/languages/webc/
120 stars 10 forks source link

Scoped styling in nested WebC layouts #89

Open hasanhaja opened 1 year ago

hasanhaja commented 1 year ago

I think I might be misunderstanding how WebC scoped layouts work. I've not had any issues building and nesting WebC components with scoped styles, but the moment I do this with a layout it breaks. This is what I'm trying to do:

Base layout: layout.webc

<!doctype html>
<html lang="en" class="scroll-smooth">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <meta name="generator" :content="eleventy.generator">
    <title @html="this.title ? `@hasanhaja | ${this.title}` : '@hasanhaja'"></title>
    <meta name="color-scheme" content="dark light">
    <link rel="stylesheet" href="layout.css">

    <style @raw="getBundle('css')" webc:keep></style>
    <script @raw="getBundle('js')" webc:keep></script>
  </head>
  <body>
    <site-header></site-header>
    <main @html="this.content"></main>
    <footer>
      <p>
        <copyright></copyright>
      </p>
    </footer>
  </body>
</html>

Nested layout: info.webc

---
layout: layouts/layout.webc
---
<div class="container">
  <h1 @html="this.displayTitle"></h1>
  <article @html="this.content"></article>
<div>

<style webc:scoped>
  .container {
    padding: 1rem;
  }
</style>

Content: picks.md

---
layout: layouts/info.webc
title: Picks
displayTitle: My top picks
---

## Title

Content goes here

Error:

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing to "_site/picks/index.html" from "./src/picks.md" (via EleventyTemplateError)
[11ty] 2. Could not find any top level <style webc:scoped> in component: ./src/_includes/layouts/info.webc (via Error)

This worked when I used to have styles scoped to layout.webc. It's only the nested version that doesn't want to play ball.

hasanhaja commented 1 year ago

Am I right in thinking that in normal webc usage, the style scoped to the parent is also applied to the children? If that's the case, this could just be something that's documented because I don't think it makes sense to have scoped styling at the level of layouts.