Class 处理
class 属性将类 Tailwind 工具类映射为原生 GPUI builder 方法。只要样式在编译期已知,请优先使用静态字面量或条件字面量数组,让所有样式在编译期零开销展开。
三种 class 写法对比
Section titled “三种 class 写法对比”| 写法 | 示例 | 展开时机 | 适用场景 |
|---|---|---|---|
| 静态字面量 | class="flex items-center gap-4 p-4" |
编译期(零开销) | 固定布局、颜色、状态变体、rsx.toml 令牌 |
| 条件字面量数组 / 分支 | class={["flex", active && "bg-blue-500"]} |
编译期(零开销) | 依据 Rust 布尔值或枚举切换样式 |
| 运行时动态表达式 | class={format!("w-[{}px]", width)} |
运行时(StyleRefinement 匹配器) |
运行时外部拼接的动态样式字符串 |
静态 Class
Section titled “静态 Class”静态字符串字面量在编译期直接展开为 GPUI 方法调用链:
rsx! { <div class="flex items-center gap-2 px-4 py-2 rounded-md" />}// 编译期展开为:// div().flex().items_center().gap(px(2.0)).px(px(4.0)).py(px(2.0)).rounded_md()宽松模式 vs 严格模式
Section titled “宽松模式 vs 严格模式”- 宽松模式(
rsx!、rsx_permissive!): 不支持的静态 class 产生编译警告(拼写相近时会附带建议)。未知的标识符风格 class 会警告并透传为 GPUI 方法;但纯 CSS 族(z-*、space-x-*、space-y-*、divide-*、transition*、duration-*、ease-*、delay-*、animate-*)始终警告并忽略。非法任意值(如w-[oops])仍直接报错。 - 严格模式(
rsx_strict!): 所有不支持的静态 class 均作为编译错误处理。调用第三方扩展方法(如h_flex)请使用显式标志属性(<div h_flex />)或base={...}。
编译期条件 Class
Section titled “编译期条件 Class”只要叶子节点都是字符串字面量,if、match 与 clsx 风格数组都会在编译期展开为条件方法链,不会生成运行时字符串解析器:
rsx! { <div class={[ "flex items-center p-2 rounded-md", if active { "bg-blue-500 text-white" } else { "bg-gray-100 text-gray-900" }, disabled && "opacity-50", selected.then_some("border-1 border-blue-600"), (compact, "gap-2"), ]} />}注意: 如果先把条件表达式赋给局部变量
let cls = ...;再传给class={cls},宏会将其视为动态字符串。请将条件直接写在class={...}内以保留编译期优化。
状态变体与状态 Class 属性
Section titled “状态变体与状态 Class 属性”静态字面量支持内联状态前缀:hover:、focus:、focus-visible:、active:、group-hover: 与 group-active:(含命名组,如 group-hover/menu:)。
也可以将状态样式拆分到独立属性中,提升长标签的可读性:
rsx! { <button class="px-4 py-2 rounded-md bg-blue-500 text-white" hoverClass="bg-blue-600" focusClass="border-blue-500" activeClass="opacity-75" />}hoverClass、focusClass、activeClass仅接受字符串字面量,且拒绝需要元素级状态的类(如overflow-scroll、debug-outline)。active:与activeClass会自动注入 GPUI ID(因为 GPUI 的active钩子是有状态的);hoverClass与focusClass不注入 ID。
动态 Class
Section titled “动态 Class”当 class 字符串在运行时组装(class={row_class})时,宏会生成紧凑的 StyleRefinement 匹配器:
let class_name = format!("w-[{}px] bg-{}", width, tone);rsx! { <div class={class_name} /> }- 宽松模式: 忽略未知动态 token;在 debug 构建中每个调用点仅输出一次警告(启用
dynamic-class-reportfeature 后汇总到进程退出时报告)。 - 严格模式: 遇到未知动态 token 默认 panic,可通过
onUnknownClass={|class: &str| ...}自定义处理。
静态与动态能力对比矩阵
Section titled “静态与动态能力对比矩阵”| 能力 | 静态 class | 动态 class |
|---|---|---|
| 布局与对齐 | 支持 | 支持常用子集 |
| 数值间距与尺寸 | 支持 | 支持 |
任意长度(w-[280px]、max-w-[40%]) |
支持 | 支持 |
分数尺寸(w-6/24、w-1/2) |
支持 | 支持 |
| 完整 Tailwind 色板 + hex / RGB / RGBA | 支持 | 支持 |
静态 rsx.toml 令牌(十六进制颜色、[radius]、[spacing]、[opacity]) |
支持 | 支持 |
渲染期 rsx.toml { expr = "cx..." } 令牌 |
支持 | 不支持 |
静态有状态滚动类(overflow-y-scroll) |
支持(自动 ID) | 不支持 |
状态变体(hover:、focus:、active:、group-*) |
支持 | 不支持 |
响应式 / dark: 前缀 |
警告 / 严格报错 | 调试报告 / 严格 panic |
支持的工具类家族
Section titled “支持的工具类家族”| 类别 | 示例 |
|---|---|
| 布局 | flex、flex-col、flex-row、flex-wrap、grid、hidden、absolute、relative、grow/shrink |
| 对齐 | items-*、justify-*、content-*、self-* |
| 间距 | gap-*、gap-x-*、gap-y-*、p-*、px-*、m-*、方向变体与负间距(-m-4、-mt-1.5) |
| 尺寸 | w-*、h-*、size-*、min-*、max-*、*-full、*-auto、百分比、分数、任意长度 |
| 颜色 | 完整 Tailwind 色板(slate..rose 50–950、black、white)、#hex、rgb(...)、rgba(...) |
| 排版 | 字号、字重、对齐、装饰、truncate、空白处理、行数截断 |
| 边框 | 宽度、单边宽度、颜色、rounded-*、虚线边框 |
| 其他 | 光标样式、debug-outline、阴影、透明度、网格跨列(col-span-full) |
长度语义与间距刻度
Section titled “长度语义与间距刻度”默认采用直接像素语义:w-64 gap-4 展开为 .w(px(64.0)).gap(px(4.0))。
- 若希望使用 Tailwind 的 4px 刻度(
gap-4→16px),可按块使用rsx_tw!、启用 Cargo featuretailwind-scale,或在rsx.toml中设置[options] spacing-scale = "tailwind"。 - 任意长度(
w-[280px]、h-[18rem]、max-w-[37.5%])与分数(w-6/24)始终保持声明值。 gap-[10%]等百分比间距会被拒绝,因为 GPUI 间距 API 要求确定长度。
GPUI 提供 *_neg_* 预设的间距与尺寸族支持前导负号(-m-2、-mt-1.5、-ml-[13px]、hover:-gap-1):
| 负值族 | GPUI 布局实际效果 |
|---|---|
-m-*、-mx-*、-my-*、-mt-*、-ml-* |
元素与后续兄弟向负方向偏移,形成重叠效果 |
-mb-*、-mr-* |
沿主轴将下一个兄弟元素拉近 |
-gap-*、-gap-x-*、-gap-y-* |
沿对应轴缩小兄弟间距 |
-p-*、-px-*、-py-*、-pt-*、-pl-* |
内容向外偏移超出容器边界 |
-w-*、-h-*、-size-*、-max-w-*、-max-h-* |
被 GPUI 钳制为 0(编译期发出警告) |
-min-w-*、-min-h-* |
被 GPUI 忽略(编译期发出警告) |
-auto、-full、rounded-* 与 leading-* 不支持取负。像 -mt-px 这类预设值仅在静态字面量中支持,动态匹配器不解析 px / 0p5 等预设名。
错误策略与间距刻度相互独立:
| 宏 | 错误策略 | 间距刻度 |
|---|---|---|
rsx! |
宽松模式 | 像素(或遵循 rsx.toml / feature) |
rsx_strict! |
严格编译报错 | 像素(或遵循 rsx.toml / feature) |
rsx_permissive! |
显式宽松模式 | 像素(或遵循 rsx.toml / feature) |
rsx_tw! |
宽松模式 | Tailwind 4px 刻度 |
rsx_expand! |
输出展开代码字符串(不做类型检查) | 像素(或遵循 rsx.toml / feature) |
需要“严格检查 + Tailwind 刻度”时,将 rsx_strict! 与 tailwind-scale feature 或 [options] spacing-scale = "tailwind" 组合使用即可。
更多渲染热路径优化与共享运行时数据请参阅性能优化。