Velaguides

Dynamic class names

Computing className keeps every utility. What stays checked, and what an unreadable string costs.

You can compute className: ternaries, &&, arrays, template strings, props and state all work, and every utility still applies. What changes is whether the compiler can read your classes, and so whether it can check them.

Three degrees, not two

What you wroteEmitted asResolved byChecked
className="flex gap-2", or an array of literalsYour tag, plain propsThe compilerYes
A variant, a branch, a margin, a divide-*VelaRuntimeHost with __velaRulesThe compiler; the host only reads conditionsYes
A string the compiler cannot readVelaRuntimeHost with classNameThe host, in-gameNo

A const holding a string literal counts as the first row. A const holding anything else counts as the third.

Only the third row has a real cost. The middle row is where variants and branches normally land, including a plain literal like className="sm:w-full".

The two paths resolve the same utilities

Every utility in the reference works the same in-game as at compile time, including opacity modifiers and arbitrary values.

One difference: in-game, a utility the element can’t carry is dropped, because writing TextColor3 onto a Frame is a Roblox error. The static path warns and writes it anyway.

opacity-* always hands the class list to the host, which applies one alpha to the children. See opacity-*.

A branch lowers at compile time

When an expression spells out its classes, the compiler resolves every option, even though it can’t know which one applies:

Both sizes are resolved and checked
<textlabel className={big ? "text-lg" : "text-sm"} />

Inside a branch:

  • The full utility set applies.
  • A bad utility reports a diagnostic.
  • A variant applies only when both it and the branch’s condition hold. big && "hover:text-xl" needs big and the pointer.
  • Each condition is evaluated once, however many branches use it.

The compiler reads ternaries, &&, the literal on the right of ||, arrays, object maps, and a const holding a string literal. Branches combine with the classes around them, so ["w-40", tall && "h-10"] becomes one Size.

The element still renders through the runtime host, but the host only evaluates the conditions. Nothing is parsed in-game:

What the emit carries
<VelaRuntimeHost
__velaRules={[
{ condition: { kind: "test", index: 0, expected: true }, effects: { props: [{ name: "TextSize", value: "18" }] } },
{ condition: { kind: "test", index: 0, expected: false }, effects: { props: [{ name: "TextSize", value: "14" }] } },
]}
__velaTests={[big ? true : false]}
__velaTag={"textlabel"}
/>

What sends a branch back to the runtime resolver

Three things, and only the affected branch moves. ["w-4", on && "mt-4"] keeps w-4 resolved and hands the host on && "mt-4".

  • A token no rule can carry: m-*, divide-*, animate-*, transition*, the text transforms and opacity-*.
  • A value the source never spells out: `size-${n}`, a bare variable, a call, a spread.
  • The left side of ||, which is the class value itself when truthy.

When branches overlap:

  • Branches that set the same property apply in written order. [a && "bg-red-500", b && "bg-blue-500"] is blue when both hold.
  • Branches that set different halves of one property don’t merge. [a && "w-40", b && "h-10"] writes Size twice, and the later one wins.

What the runtime path still costs

This is the third row: a class value the compiler can’t read, like cn(...), a template string, or a prop passed through.

  • No diagnostics. A typo like bg-blu-600 resolves to nothing, and the element renders without a background. In a literal or a branch, it would warn unknown-theme-key.
  • Less editor help. Completions and hover work on literal text, even inside an expression. A token an interpolation cuts into, as in `w-[${width}]`, gets none.
  • Theme values are re-parsed in-game. The host accepts only Color3.fromRGB(r, g, b) and new UDim(a, b) with plain numbers. Any other theme value compiles, then silently breaks. See theme values.

Code size isn’t a cost. @rbxts/vela-runtime is one ModuleScript the whole place shares, so each file adds only an import and its config:

What a runtime-path module carries
import { createVelaRuntimeHost } from "@rbxts/vela-runtime";
const VelaRuntimeHost = createVelaRuntimeHost({ /* … */ });

How to tell which row an element landed in

There’s no diagnostic for it, so read the emitted roblox-ts:

  • The element kept its own tag and gained plain props: first row.
  • It became VelaRuntimeHost with __velaRules and __velaTests: middle row.
  • It became VelaRuntimeHost and still has a className prop: third row.

Writing for the checked rows

Use readable forms where a typo would hurt. Compute freely everywhere else.

Branch the classes, not the element

Put the branch inside the class value instead of in a template string:

Read, resolved and checked
<frame className={["flex flex-col items-center gap-2 p-4 rounded-lg", emphasized ? "bg-slate-700" : "bg-slate-800"]} />

Duplicating the whole element instead keeps it fully static, with no host at all. That’s a leaner emit but no better checked, so save it for elements where performance matters.

Or use a variant instead of computing

If the state is something Vela can see (pointer, press, focus, viewport, input device or color scheme), use a variant:

No computed string needed
<textbutton className="bg-slate-800 hover:bg-slate-700 active:bg-slate-600 transition" Text="Play" />

Computing is fine for the rest

A computed className for your own state is normal, and every token in it resolves:

Fine — resolves fully, just without compile-time checking
const tone = disabled ? "bg-slate-800 text-slate-500" : "bg-blue-600 text-white";
return <frame className={`flex items-center gap-2 p-2 rounded-md ${tone}`} />;

The interpolation is what loses the check, not the ternary. Move the ternary into an array to get it back:

Same rendering, checked
return (
<frame
className={[
"flex items-center gap-2 p-2 rounded-md",
disabled ? "bg-slate-800 text-slate-500" : "bg-blue-600 text-white",
]}
/>
);

Components take the same rows

<Panel className={classes} /> is lowered exactly like <frame className={classes} />, branches included. On the runtime host, __velaTag={Panel} holds the component itself, and the host renders it with the resolved props and helper children.

Your component must forward the props and children it doesn’t use, or the styling is lost with no diagnostic. See How it works.

On the runtime path, helper instances such as UICorner are built in-game rather than passed as JSX children.

See also