"""Offline Playwright Chromium lab for Tailwind CSS Ch 12: Reusing Styles."""
import json
from playwright.sync_api import sync_playwright
CSS = """
@layer base, components, utilities;
@layer base {
*, ::before, ::after { box-sizing: border-box; border: 0 solid #e5e7eb; }
}
@layer utilities {
.flex { display: flex; }
.space-x-4 > :not([hidden]) ~ :not([hidden]) { margin-left: 1rem; }
.w-16 { width: 4rem; }
.h-16 { height: 4rem; }
.rounded-full { border-radius: 9999px; }
.border-2 { border-width: 2px; border-style: solid; }
.border-white { border-color: #ffffff; }
.bg-emerald-600 { background-color: #059669; }
}
@layer components {
.avatar {
width: 4rem;
height: 4rem;
border-radius: 9999px;
border-width: 2px;
border-style: solid;
border-color: #ffffff;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
}
.button {
padding-left: 1rem;
padding-right: 1rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
background-color: #3b82f6;
color: #ffffff;
border-radius: 0.5rem;
}
.button:hover {
background-color: #2563eb;
}
.avatar-resp {
width: 4rem;
height: 4rem;
border-radius: 9999px;
border: 2px solid #ffffff;
}
@media (min-width: 768px) {
.avatar-resp {
width: 6rem;
height: 6rem;
}
}
}
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 600, "height": 400})
page.set_content(f"""
<!doctype html><html><head><style>{CSS}</style></head>
<body>
<div id="util-row" class="flex space-x-4">
<div id="u1" class="w-16 h-16 rounded-full border-2 border-white"></div>
<div id="u2" class="w-16 h-16 rounded-full border-2 border-white"></div>
</div>
<div id="comp-row" class="flex space-x-4">
<div id="c1" class="avatar"></div>
<div id="c2" class="avatar"></div>
</div>
<button id="btn-base" class="button">Primary</button>
<button id="btn-override" class="button bg-emerald-600">Override</button>
<div id="resp-av" class="avatar-resp"></div>
</body></html>
""")
# Case 1: Utility combination vs @apply compiled .avatar parity
act1 = page.evaluate("""() => {
function probe(id) {
const el = document.getElementById(id);
const r = el.getBoundingClientRect();
const s = getComputedStyle(el);
return {
w: Math.round(r.width),
h: Math.round(r.height),
radius: s.borderRadius,
borderW: s.borderTopWidth,
borderColor: s.borderTopColor,
ml: s.marginLeft,
};
}
return {util_second: probe('u2'), apply_second: probe('c2')};
}""")
exp1 = {
"util_second": {"w": 64, "h": 64, "radius": "9999px", "borderW": "2px", "borderColor": "rgb(255, 255, 255)", "ml": "16px"},
"apply_second": {"w": 64, "h": 64, "radius": "9999px", "borderW": "2px", "borderColor": "rgb(255, 255, 255)", "ml": "16px"},
}
cases.append({
"name": "utility_group_vs_apply_equivalence",
"passed": act1 == exp1,
"expected": exp1,
"actual": act1,
})
# Case 2: @layer components overridden by @layer utilities despite source order
act2 = page.evaluate("""() => {
const b1 = getComputedStyle(document.getElementById('btn-base'));
const b2 = getComputedStyle(document.getElementById('btn-override'));
return {
btn_padding: b1.paddingTop + ' ' + b1.paddingRight,
btn_radius: b1.borderRadius,
btn_bg: b1.backgroundColor,
override_bg: b2.backgroundColor,
};
}""")
exp2 = {
"btn_padding": "8px 16px",
"btn_radius": "8px",
"btn_bg": "rgb(59, 130, 246)",
"override_bg": "rgb(5, 150, 105)",
}
cases.append({
"name": "layer_components_cascade_override",
"passed": act2 == exp2,
"expected": exp2,
"actual": act2,
})
# Case 3: Responsive avatar component variant (600px vs 800px)
sm_box = page.evaluate("""() => {
const r = document.getElementById('resp-av').getBoundingClientRect();
return [Math.round(r.width), Math.round(r.height)];
}""")
page.set_viewport_size({"width": 800, "height": 400})
md_box = page.evaluate("""() => {
const r = document.getElementById('resp-av').getBoundingClientRect();
return [Math.round(r.width), Math.round(r.height)];
}""")
act3 = {"below_md": sm_box, "at_md_and_above": md_box}
exp3 = {"below_md": [64, 64], "at_md_and_above": [96, 96]}
cases.append({
"name": "responsive_component_variants",
"passed": act3 == exp3,
"expected": exp3,
"actual": act3,
})
# Case 4: Interactive hover variant on reused component
rest_bg = page.evaluate("() => getComputedStyle(document.getElementById('btn-base')).backgroundColor")
page.hover("#btn-base")
hover_bg = page.evaluate("() => getComputedStyle(document.getElementById('btn-base')).backgroundColor")
act4 = {"rest": rest_bg, "hover": hover_bg}
exp4 = {"rest": "rgb(59, 130, 246)", "hover": "rgb(37, 99, 235)"}
cases.append({
"name": "hover_state_variant_on_component",
"passed": act4 == exp4,
"expected": exp4,
"actual": act4,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/tailwindcss-reusing-styles-lab/explore-reusing-styles.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-reusing-styles.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a653.zip
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip