蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线 Chromium 验证自定义色板/间距/字体/阴影、xs/xxl 断点与组件状态变体

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-custom-styles.py
explore-custom-styles.py166 行
"""Offline Playwright Chromium lab for Tailwind CSS Ch 13: Custom Styles."""
import json
from playwright.sync_api import sync_playwright

CSS = """
@layer base, components, utilities;

@layer base {
  *, ::before, ::after { box-sizing: border-box; }
}

@layer components {
  .avatar { width: 4rem; height: 4rem; border-radius: 9999px; border: 2px solid #ffffff; }
  .btn { padding: 0.5rem 1rem; background-color: #3b82f6; color: #ffffff; font-weight: 600; border-radius: 0.5rem; }
  .btn-danger { padding: 0.5rem 1rem; background-color: #ef4444; color: #ffffff; font-weight: 600; border-radius: 0.5rem; }
}

@layer utilities {
  .bg-cyan { background-color: #9cdbff; }
  .bg-custom-blue { background-color: #1c64f2; }
  .bg-custom-gray { background-color: #3a3a3a; }
  .text-white { color: #ffffff; }
  .text-gray-900 { color: #111827; }
  .p-128 { padding: 32rem; }
  .p-144 { padding: 36rem; }
  .font-custom { font-family: "Open Sans", Arial, sans-serif; }
  .shadow-custom { box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.1); }
  .w-32 { width: 8rem; }
  .disabled\\:opacity-50:disabled { opacity: 0.5; }
  .group:hover .group-hover\\:text-blue-600 { color: #2563eb; }
  @media (min-width: 475px) {
    .xs\\:w-64 { width: 16rem; }
  }
  @media (min-width: 1600px) {
    .xxl\\:w-96 { width: 24rem; }
  }
}
"""


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": 400, "height": 400})
        page.set_content(f"""
        <!doctype html><html><head><style>{CSS}</style></head>
        <body>
          <div id="cyan" class="bg-cyan">Cyan</div>
          <div id="cblue" class="bg-custom-blue text-white">Blue</div>
          <div id="cgray" class="bg-custom-gray text-white">Gray</div>
          <div id="sp128" class="p-128">128</div>
          <div id="sp144" class="p-144">144</div>
          <div id="fnt" class="font-custom">Font</div>
          <div id="shd" class="shadow-custom">Shadow</div>
          <div id="bp" class="w-32 xs:w-64 xxl:w-96">BP</div>
          <button id="b1" class="btn">Primary</button>
          <button id="b2" class="btn-danger disabled:opacity-50" disabled>Danger</button>
          <div id="grp" class="group" style="padding:16px">
            <span id="grp-txt" class="text-gray-900 group-hover:text-blue-600">Child</span>
          </div>
        </body></html>
        """)

        # Case 1: Custom theme colors and spacing tokens
        act1 = page.evaluate("""() => ({
            cyan: getComputedStyle(document.getElementById('cyan')).backgroundColor,
            custom_blue: getComputedStyle(document.getElementById('cblue')).backgroundColor,
            custom_gray: getComputedStyle(document.getElementById('cgray')).backgroundColor,
            p_128: getComputedStyle(document.getElementById('sp128')).paddingTop,
            p_144: getComputedStyle(document.getElementById('sp144')).paddingTop,
        })""")
        exp1 = {
            "cyan": "rgb(156, 219, 255)",
            "custom_blue": "rgb(28, 100, 242)",
            "custom_gray": "rgb(58, 58, 58)",
            "p_128": "512px",
            "p_144": "576px",
        }
        cases.append({
            "name": "custom_theme_colors_and_spacing",
            "passed": act1 == exp1,
            "expected": exp1,
            "actual": act1,
        })

        # Case 2: Custom fontFamily and boxShadow
        act2 = page.evaluate("""() => {
            const ff = getComputedStyle(document.getElementById('fnt')).fontFamily;
            const sh = getComputedStyle(document.getElementById('shd')).boxShadow;
            return {
                has_open_sans: ff.includes('Open Sans') && ff.includes('Arial'),
                shadow: sh,
            };
        }""")
        exp2 = {
            "has_open_sans": True,
            "shadow": "rgba(0, 0, 0, 0.1) 0px 10px 15px 0px, rgba(0, 0, 0, 0.1) 0px 4px 6px 0px",
        }
        cases.append({
            "name": "custom_fonts_and_box_shadow",
            "passed": act2 == exp2,
            "expected": exp2,
            "actual": act2,
        })

        # Case 3: Custom breakpoints xs (475px) and xxl (1600px)
        w400 = page.evaluate("() => Math.round(document.getElementById('bp').getBoundingClientRect().width)")
        page.set_viewport_size({"width": 500, "height": 400})
        w500 = page.evaluate("() => Math.round(document.getElementById('bp').getBoundingClientRect().width)")
        page.set_viewport_size({"width": 1650, "height": 400})
        w1650 = page.evaluate("() => Math.round(document.getElementById('bp').getBoundingClientRect().width)")
        act3 = {"vw_400": w400, "vw_500_xs": w500, "vw_1650_xxl": w1650}
        exp3 = {"vw_400": 128, "vw_500_xs": 256, "vw_1650_xxl": 384}
        cases.append({
            "name": "custom_breakpoints_xs_and_xxl",
            "passed": act3 == exp3,
            "expected": exp3,
            "actual": act3,
        })

        # Case 4: @layer components (.btn / .btn-danger) & state variants (disabled, group-hover)
        page.set_viewport_size({"width": 800, "height": 400})
        grp_rest = page.evaluate("() => getComputedStyle(document.getElementById('grp-txt')).color")
        page.hover("#grp")
        grp_hover = page.evaluate("() => getComputedStyle(document.getElementById('grp-txt')).color")
        btn_props = page.evaluate("""() => {
            const s1 = getComputedStyle(document.getElementById('b1'));
            const s2 = getComputedStyle(document.getElementById('b2'));
            return {
                btn_bg: s1.backgroundColor,
                btn_weight: s1.fontWeight,
                danger_bg: s2.backgroundColor,
                danger_disabled_opacity: s2.opacity,
            };
        }""")
        act4 = {**btn_props, "group_rest": grp_rest, "group_hover": grp_hover}
        exp4 = {
            "btn_bg": "rgb(59, 130, 246)",
            "btn_weight": "600",
            "danger_bg": "rgb(239, 68, 68)",
            "danger_disabled_opacity": "0.5",
            "group_rest": "rgb(17, 24, 39)",
            "group_hover": "rgb(37, 99, 235)",
        }
        cases.append({
            "name": "layer_components_and_state_variants",
            "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-custom-styles-lab/explore-custom-styles.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-custom-styles.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Tailwind CSS

课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a639.zip

下载课程关联工程包:Tailwind CSS

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a644.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习