CSS-21Optional
Give each template one root element
Make every template return exactly one root element that carries the module's root class. Callers never wrap it in an extra element to change how it looks.
How to do it
One root gives the component one place for its class, its custom properties and its container query. Spacing between components belongs to the layout that holds them, not to a wrapper added by each caller. Controls such as buttons come from the shared library in Build each control once. Name the root class as Use one naming pattern for every class describes.
Examples
JavaScript example
import { parseFragment } from 'parse5';
const roots = parseFragment(priceTag({ name: 'Rye loaf', price: '4.50' })).childNodes.filter(node => node.tagName);
assert.equal(roots.length, 1);
assert.match(roots[0].attrs.find(a => a.name === 'class').value, /^price-tag\b/);
Python example
from html.parser import HTMLParser
class Roots(HTMLParser):
def __init__(self):
super().__init__(); self.depth = 0; self.roots = []
def handle_starttag(self, tag, attrs):
if self.depth == 0: self.roots.append(dict(attrs).get("class", ""))
self.depth += 1
def handle_endtag(self, tag):
self.depth -= 1
parser = Roots(); parser.feed(price_tag("Rye loaf", "4.50"))
assert len(parser.roots) == 1 and parser.roots[0].startswith("price-tag")
How to check
A unit test parses each template's output. It finds exactly 1 top-level element, and that element's class list starts with the module prefix.
Related rules
- APP-3 Build each control once in Web app interface
- CSS-14 Use one naming pattern for every class in HTML and CSS
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/html-css/html-structure-and-reuse/one-root-per-template">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.