Velaguides

Responsive and input variants

Every variant prefix, what each one matches, and what using one costs.

A variant applies a class only while a condition holds on the player’s client. Write it as a colon prefix, as in Tailwind:

src/client/Panel.tsx
<frame className="w-full md:w-1/2 lg:w-1/3" />

Chain prefixes to require every condition. The order inside a chain doesn’t matter.

Only on a touch device in portrait
<frame className="hidden touch:portrait:visible" />
VariantMatches when
sm md lg xl 2xlviewport width is at least 640, 768, 1024, 1280, 1536
max-sm … max-2xlviewport width is below that same threshold
attr-[Name=value]the styled instance carries that Roblox attribute
portraitviewport width is less than viewport height
landscapeviewport width is greater than or equal to viewport height
touchthe active input mode is touch
mousethe active input mode is mouse
gamepadthe active input mode is gamepad
hoverthe pointer is over this element
activethis element is being pressed
focusthis element holds focus or selection
darkthe local player carries VelaColorScheme = "dark"
group-hover group-active group-focusthe nearest ancestor marked group is in that state
peer-hover peer-active peer-focusa sibling marked peer is in that state

There is no not- form and no disabled:. An unknown prefix reports unknown-variant, and the token is dropped.

Which class wins

The order you write classes in doesn’t decide. Vela ranks them the way Tailwind’s stylesheet does, weakest first:

  1. Base classes.
  2. States: hover:, active:, focus:, attribute variants, group-*, peer-*.
  3. Breakpoints, narrowest first, so a wider one wins. A max-* ranks as a breakpoint with no minimum.
  4. portrait: / landscape:, dark:, and the input variants.

Within one rank, the class written last wins. So:

  • hover:bg-blue-500 bg-red-500 turns blue under the pointer.
  • lg:p-8 md:p-4 is p-8 on a wide screen.
  • dark:bg-blue-500 md:bg-red-500 is blue in dark mode, even on a wide screen.

Breakpoints, and their complements

VariantApplies whenComplement
sm:width >= 640max-sm: — width < 640
md:width >= 768max-md: — width < 768
lg:width >= 1024max-lg: — width < 1024
xl:width >= 1280max-xl: — width < 1280
2xl:width >= 1536max-2xl: — width < 1536

max-md: is the exact complement of md:. At exactly 768px, md: applies and max-md: doesn’t. Chain the two to target one range:

Only between 768 and 1024
<frame className="md:max-lg:w-1/2" />

A chain that matches no width, such as lg:max-md:, reports invalid-breakpoint-range.

Write the small-screen style unprefixed and layer wider screens on top. To rename or add breakpoints, use theme.screens. One entry defines both the bare prefix and its max- form.

Breakpoints follow the viewport live, so resizing the window re-applies them.

How orientation is derived

landscape means the viewport is at least as wide as it is tall. Anything else is portrait.

A square viewport counts as landscape. If your layout has a square case, test it.

How input mode is detected

Exactly one input mode is active at a time. Vela picks it by priority from UserInputService, not by the device used last:

  1. gamepad if GamepadEnabled is true.
  2. Otherwise touch if TouchEnabled is true.
  3. Otherwise mouse.

So a device with both a gamepad and a touchscreen matches gamepad:, not touch:. Plugging in a controller mid-session switches the mode right away.

Hover, active and focus are per-element

These three track the element itself rather than the screen:

VariantTracked through
hoverMouseEnter / MouseLeave
activeInputBegan / InputEnded, for mouse and touch. Also clears on MouseLeave, so dragging off a button doesn’t leave it stuck.
focusFocused / FocusLost on a textbox. Gamepad SelectionGained / SelectionLost elsewhere.

Vela only listens on elements that use the variant. Event handlers you wrote yourself keep working.

Add transition to tween the change instead of snapping it (see motion utilities):

A button with all three
<textbutton
className="w-28 h-10 rounded-md bg-sky-500 hover:bg-sky-600 active:bg-sky-700 focus:ring-2 transition duration-150"
Text="Play"
/>

Gamepad and touch players may never hover. Use hover: as extra feedback only. A controller player sees focus:.

Group and peer

You can style an element from another element’s state. Mark an ancestor group and read it with group-hover:, group-active: or group-focus:. Mark a sibling peer and read it with peer-*:

A row that highlights its label
<textbutton className="group flex gap-2 bg-slate-800">
<textlabel className="text-slate-300 group-hover:text-white" Text="Settings" />
</textbutton>

To pick one group out of several, name it. group/card marks it, and group-hover/card: reads the nearest group/card ancestor, skipping unnamed groups in between. peer/email and peer-focus/email: work the same way.

Groups and peers can also be read through attribute variants: group-selected: for a registered selected variant, group-attr-[State=open]/card:, or peer-attr-[Tier=2]:. A / inside the brackets is part of the value.

A few things differ from CSS:

  • Any marked sibling is a peer, not only an earlier one, because Roblox has no sibling order. Several peers combine with OR.
  • Group hover is geometric. A child drawn outside the group’s bounds doesn’t hover the group.
  • A marker can’t be conditional. hover:group warns conditional-marker.
  • Put the marker on a host element, not a component. On a component it warns marker-on-component.
  • Only element states can be read. group-md: warns unsupported-relational-variant.

A marker becomes a CollectionService tag, VelaGroup or VelaPeer, merged with any Tag you set: <frame Tag="Card" className="group/card"> emits Tag="Card VelaGroup/card". The marked element stays on the static path. Only the elements that read it use the runtime host. Because the lookup walks the instance tree, it works across components, across files, under Vide, and past a margin on either element.

Dark mode is state your app owns

Roblox doesn’t tell a game the player’s color scheme, so your app sets it. dark: matches when Players.LocalPlayer has a VelaColorScheme attribute set to "dark":

Somewhere your settings UI can reach
Players.LocalPlayer.SetAttribute("VelaColorScheme", prefersDark ? "dark" : "light");
And then, anywhere
<frame className="bg-white dark:bg-zinc-900" />

Any other value, or no attribute at all, means light. Changing the attribute restyles every dark: element immediately.

State your UI owns

For your own states, like a panel being open or a row being selected, style off a Roblox attribute on the element. Write it inline:

Inline
<frame className="rounded-sm attr-[State=open]:rounded-lg" />

Or register a name for it in vela.config.ts:

vela.config.ts
plugin(({ addVariant }) => {
addVariant("open", { attribute: "State", equals: "open" });
});
…and then
<frame className="rounded-sm open:rounded-lg" />

Set the attribute the way you set any other:

<frame ref={(f) => f?.SetAttribute("State", isOpen ? "open" : "closed")} />

equals takes a string, number or boolean. Custom variants chain with every other variant and get the same editor support as built-in ones. Attributes replicate from the server and show in Studio, so your styles read the same state the rest of the game does.

The names group and peer, and any name starting with group- or peer-, are reserved. addVariant throws on them.

The cost: any variant forces the runtime path

Any variant prefix moves that element onto the runtime path, even in a plain string literal like className="sm:w-full". The element renders through VelaRuntimeHost instead of getting plain props.

The cost is small:

  • The file imports @rbxts/vela-runtime, one ModuleScript the whole place shares, plus a few hundred bytes of config.
  • Each runtime-path element re-checks its rules when the viewport, input mode or color scheme changes. All hosts share one set of signal connections, made the first time one needs them.

The compiler still resolves and checks every variant class, so you keep diagnostics and the full utility set.

Use variants where they pay off: top-level containers that reflow between phone and desktop, a gamepad-only hint, hover feedback on controls. Keep leaf components on plain classes.

Driving reflow yourself

To keep a file off the runtime host entirely, read the viewport yourself and switch between two static class strings. Both branches stay on the static path:

src/client/ResponsiveShell.tsx
function useViewportWide(threshold: number) {
const [wide, setWide] = React.useState(false);
React.useEffect(() => {
const camera = Workspace.CurrentCamera;
if (!camera) return;
const update = () => setWide(camera.ViewportSize.X >= threshold);
const connection = camera.GetPropertyChangedSignal("ViewportSize").Connect(update);
update();
return () => connection.Disconnect();
}, [threshold]);
return wide;
}
export function Shell(props: { children?: React.Element }) {
const wide = useViewportWide(768);
return wide ? (
<frame className="flex gap-4 p-4 w-full h-full">{props.children}</frame>
) : (
<frame className="flex flex-col gap-2 p-2 w-full h-full">{props.children}</frame>
);
}

See also