Skip to content

Panels and layout

Updated

Panels and layouts are the containers of a UI. A UI Layout arranges its children in a row or a column; a UI Panel is a layout with a visible surface: a fill, rounded corners and an optional border. Around them sit a few helpers: UI Screen Space pins a panel to the screen, UI Backdrop gives a world panel a solid back, UI Divider and UI Spacer separate and push elements apart, UI Selection Group makes toggles behave like radio buttons, and UI Handle shows a grip to drag a panel by.

  • Read the UI overview for roots, nested elements, layout pixels and the theme.
  • Scripts react to UI through a Signal Emitter and on(signal, …). See Scripting for hooks and signals.
  • A panel can be dragged with a Grabbable; see Input and interaction.

The UI Layout component (comp-ui-layout) makes its entity a flexbox container: its children flow along flexDirection, spaced by gap, inside padding. It draws nothing. The UI Layout entity (ent-ui-layout) carries only a Layout; the UI Panel and UI List entities carry one too.

Prop Type Default Description
flexDirection 'row' or 'column' 'column' Direction the children flow along.
alignItems 'center', 'flex-start', 'flex-end', 'stretch' 'stretch' How children line up across the flow.
justifyContent 'center', 'flex-start', 'flex-end', 'space-between' 'flex-start' How children spread along the flow.
width, height number or 'N%' root: 300 px wide; otherwise fits the content Fixed size in px, or a percentage of the parent.
minWidth, maxWidth, minHeight, maxHeight number or 'N%' none Floors and ceilings for a size that fits its content or flexes.
flexGrow number none Share of the parent’s leftover space: 1 on each of two children splits it evenly.
gap number 12 from the theme; 0 in a UI List Space between children, in px.
padding number or number[] 0 Space inside the box, in px. See Spacing.
margin number or number[] none Space outside the box, in px. See Spacing.
positionType 'relative' or 'absolute' 'relative' An absolute nested layout is placed against its parent’s edges and takes no room in the flow.
positionTop, positionRight, positionBottom, positionLeft number unset Insets from the parent’s edges, in px, for an absolute layout.
foreground boolean false Draw a nested layout above images in the normal flow, for badges and overlays.
pixelSize number 0.003 Metres per layout pixel. Root only; ignored under UI Screen Space.
stackOrder number automatic Root only. Among overlapping roots, higher draws in front and takes the pointer first.
renderMode 'world' or 'overlay' 'overlay' Root only. See Drawing order.
  • Root size. A root is 300 px wide by default and as tall as its content: 0.9 m wide at the default pixelSize. Scale the entity, or change pixelSize, to resize it in the world.
  • Rows and columns. A nested layout with no width fits its content. Give its children flexGrow: 1 to share a row, or put a UI Spacer between them to push them apart.

Root layouts and panels are drawn as an overlay by default: after the scene, so walls and objects never cut through them, while they keep their place and size in the world. Set renderMode: 'world' on a root when the panel should be hidden behind objects in front of it, like a real sign. Nested elements always follow their root.

When roots overlap, each gets a distinct order from the scene hierarchy. Set stackOrder when one must stay in front, such as a dialog over a menu: the higher number draws in front and receives the pointer first. Clearing it goes back to the automatic order.

The UI Panel component (comp-ui-panel) paints the box behind a layout’s children. The UI Panel entity (ent-ui-panel) carries a Layout and a Panel, and it is the entity that takes a panel’s extras: UI Backdrop, UI Handle, Grabbable, Billboard, Selectable, Outline, UI Tooltip, Enable on Signal and Don’t Unload. Moving or animating a panel moves all its controls with it.

Prop Type Default Description
backgroundColor string the theme’s surface colour Fill colour.
opacity number 1 Alpha of the fill, 0 to 1. 0 draws no fill, for an invisible grouping panel. Text and controls stay opaque.
borderRadius number or number[] 12 from the theme Corner radius in px, per corner. See Spacing.
borderColor string none Outline colour. The border is drawn only when this is set.
borderWidth number or number[] 2 from the theme, when borderColor is set Outline thickness in px, per side.

Every UI component that draws a box takes padding and margin as CSS does: one number, or an array of one to four numbers, in px.

Value Applies to
8 All four sides.
[4, 12] Top and bottom 4, left and right 12.
[4, 12, 16] Top 4, left and right 12, bottom 16.
[4, 8, 12, 16] Top, right, bottom, left, clockwise.

Padding is inside the box, under its fill and border; margin is outside, in the parent’s layout. Margin may be negative; padding may not. borderWidth takes the same one to four values, one per side. borderRadius takes one to four corners clockwise from the top-left: [16, 16, 0, 0] rounds only the top corners. A value that is not a number or an array of one to four numbers throws when you set it.

UI Panel, UI List, UI Menu Item, UI Screen Space, UI Selection Group and UI Render Scene draw no box of their own: space them with their entity’s UI Layout.

The UI Screen Space component (comp-ui-screen-space) pins a root panel to the viewport, for HUDs, docks and overlays on desktop and mobile. The panel draws one layout pixel per screen pixel, over the scene, and keeps its place through every window resize.

Prop Type Default Description
anchor 'top-left', 'top', 'top-right', 'left', 'center', 'right', 'bottom-left', 'bottom', 'bottom-right' 'center' The point of the screen, and of the panel, pinned together.
insetX number 0 Screen pixels in from the anchored edge horizontally. On a centred anchor, positive moves right.
insetY number 0 Screen pixels in from the anchored edge vertically: down from top, up from bottom, down on a centred anchor.
screenScale number 1 Size on screen: 1 is one layout pixel per screen pixel, 0.5 half size. Insets stay in screen pixels.
  • Always on top. Nothing in the scene hides or cuts a screen-space panel, and a pointer ray through it hits the panel, not the wall behind. Outlines and bloom never land on it.
  • Root only. On a nested element it does nothing.
  • Transform. The entity’s Transform has no effect while Screen Space places it; move it with anchor and the insets. Disabling or removing Screen Space puts the panel back at its transform in the world.
  • Editor and play. It places the panel in both, so you see the HUD where it will be.
  • Shared placement. A panel’s screen placement can be shared with everyone in a room through the ui-screen-space network trait (NetworkTraitType.UIScreenSpace). See Multiplayer.

The UI Backdrop component (comp-ui-backdrop) adds a solid plane behind a root UI Panel, so it is not see-through from behind. The plane follows the panel’s size and corners as its content changes. A nested panel has no backdrop.

Prop Type Default Description
color string the theme’s surface colour Colour of the back.
opacity number 0.5 0 to 1. Below 1 the scene shows through the back.
padding number or number[] 0 How far it bleeds past the panel, in px, per side.
offset number 0.002 Distance behind the panel, in metres.

The UI Divider component (comp-ui-divider) draws a thin line between elements. It stretches across its parent unless length is set and takes no pointer. Entity: UI Divider (ent-ui-divider).

Prop Type Default Description
orientation 'horizontal' or 'vertical' 'horizontal' horizontal separates stacked rows; vertical separates the items of a row.
color string the theme’s divider colour Line colour.
thickness number 1 Line thickness in px.
length number stretches Fixed length in px.
padding, margin number or number[] none See Spacing.

The UI Spacer component (comp-ui-spacer) is an invisible, flexible gap. Between siblings in a row or column it takes the free space and pushes them apart: A · Spacer · B sends A and B to opposite ends. Entity: UI Spacer (ent-ui-spacer).

Prop Type Default Description
grow number 1 Share of the free space it takes, relative to other growing siblings. 0 keeps it at size.
size number 0 Minimum size in px along the parent’s flow, kept even with no free space.
padding, margin number or number[] none See Spacing.

The UI Selection Group component (comp-ui-selection-group) makes toggles exclusive, like radio buttons: when one member turns on, every other member of the same group turns off. Add it to UI Checkbox, UI Switch, UI Toggle Button or UI List Item entities and give them the same group.

Prop Type Default Description
group string '' Members with the same name are exclusive. Empty: not grouped.
  • A group spans the whole scene, so its members can sit in different panels.
  • A member that starts checked turns the others off when it joins.
  • Turning a member on from a script with setChecked(true) applies the group too.
  • Turning a member off leaves the group with nothing selected; the group never forces one on.
  • A disabled or inactive member leaves the group.
  • Pair it with a checkbox’s radio: true for the round radio look.

The UI Handle component (comp-ui-handle) draws a pill-shaped grip just outside one edge of a UI Panel, to show the player where to grab it. It is a visual cue only: the drag comes from a Grabbable on the same panel, which treats the grip as part of the panel.

Prop Type Default Description
placement 'top', 'bottom', 'left', 'right' 'top' Edge the grip sits on.
length number 64 Length along the bar, in px.
thickness number 12 Thickness across the bar, in px.
color string the theme’s handle colour Fill colour.
radius number half the thickness Corner radius in px; the default is a full pill.
margin number or number[] 8 Gap between the panel edge and the bar, in px. Only the placement side is used.
BuildPanel.jx
#pragma lifecycle(startup)
function startup() {
const panel = new UIPanelEntity();
panel.getComponent(UILayoutComponent).setProps({ width: 360, padding: 20, gap: 12, alignItems: 'center' });
panel.getComponent(UIPanelComponent).setProps({ backgroundColor: '#111827', borderColor: '#334155', borderRadius: 18 });
const title = new UILabelEntity();
title.getComponent(UILabelComponent).setProps({ text: 'Welcome', fontSize: 28 });
const line = new UIDividerEntity();
const hint = new UILabelEntity();
hint.getComponent(UILabelComponent).setProps({ text: 'Walk to the portal to begin.', color: '#a5b4fc' });
panel.add(title, line, hint);
panel.position.set(0, 1.6, -2);
scene.add(panel);
}

How it works:

  • new UIPanelEntity() creates a panel that already carries a UI Layout and a UI Panel; getComponent reaches each one to set its props.
  • The labels and the divider are added under the panel with panel.add(...), in the order they should appear. Because they are nested, their own transforms are ignored and the column layout places them.
  • scene.add(panel) puts the root in the scene. Its position sets where it stands in the world.
Toolbar.jx
#pragma lifecycle(startup)
// Scene: a UI Panel named 'Toolbar' whose UI Layout is a row
// (flexDirection 'row', alignItems 'center').
// The script fills it: a title on the left, two buttons on the right.
function startup() {
const toolbar = findEntity('Toolbar');
toolbar.getComponent(UILayoutComponent).setProps({ width: 480, padding: [8, 12], gap: 8 });
const title = new UILabelEntity();
title.getComponent(UILabelComponent).setProps({ text: 'Gallery', fontSize: 20 });
const spacer = new UISpacerEntity();
const share = new UIButtonEntity();
share.getComponent(UIButtonComponent).setProps({ text: 'Share', variant: 'subtle' });
share.addComponent(new SignalEmitterComponent({ signal: 'toolbar/share' }));
const close = new UIButtonEntity();
close.getComponent(UIButtonComponent).setProps({ text: 'Close' });
close.addComponent(new SignalEmitterComponent({ signal: 'toolbar/close' }));
toolbar.add(title, spacer, share, close);
on('toolbar/close', (event) => {
if (event.eventType === EventType.UIButton.Press) toolbar.setEnabled(false);
});
on('toolbar/share', (event) => {
if (event.eventType === EventType.UIButton.Press) log('share pressed');
});
}

How it works:

  • The spacer between the title and the buttons takes all the free space in the row, so the title sits at the left edge and the buttons at the right.
  • padding: [8, 12] puts 8 px above and below and 12 px left and right.
  • Each button gets its own Signal Emitter, so each press arrives on its own signal. Disabling the toolbar entity hides it and everything in it.
HudCorner.jx
#pragma lifecycle(startup)
// Scene: a UI Panel named 'HUD' with a UI Screen Space component,
// containing a UI Button named 'Move' whose Signal Emitter uses 'hud/move'.
const CORNERS = ['top-left', 'top-right', 'bottom-right', 'bottom-left'];
let index = 0;
function startup() {
const screen = findEntity('HUD').getComponent(UIScreenSpaceComponent);
screen.setProps({ anchor: CORNERS[index], insetX: 16, insetY: 16 });
on('hud/move', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
index = (index + 1) % CORNERS.length;
screen.setProps({ anchor: CORNERS[index] });
});
}

How it works:

  • UI Screen Space pins the panel to an anchor of the screen; the insets keep it 16 screen pixels away from the edges whatever the window size.
  • Each press moves the anchor to the next corner. The panel’s Transform plays no part while Screen Space places it.

4. A draggable panel with a grip and a solid back

Section titled “4. A draggable panel with a grip and a solid back”
DraggablePanel.jx
#pragma lifecycle(startup)
function startup() {
const panel = findEntity('Inventory');
// Pointers and XR hands can drag it, by a handle along the bottom.
panel.addComponent(new GrabbableComponent());
panel.addComponent(new UIHandleComponent({ placement: 'bottom', length: 96 }));
// An opaque back, so the panel is not see-through from behind.
panel.addComponent(new UIBackdropComponent({ opacity: 1, padding: 4 }));
// Keep it in front of other overlapping panels.
panel.getComponent(UILayoutComponent).setProps({ stackOrder: 50 });
}

How it works:

  • A Grabbable on the panel lets the player drag it with a mouse, a finger or an XR controller. The UI Handle only draws the grip; the Grabbable treats the grip as part of the panel, so grabbing the grip drags the whole panel.
  • The UI Backdrop adds a solid plane behind the panel; padding: 4 lets it bleed 4 px past each edge.
  • A higher stackOrder keeps this panel in front of any other root it overlaps.
Difficulty.jx
#pragma lifecycle(startup)
const NAMES = ['Easy', 'Normal', 'Hard'];
let difficulty = 'Normal';
function startup() {
for (const name of NAMES) {
const entity = findEntity(name);
entity.getComponent(UICheckboxComponent).setProps({ text: name, radio: true });
entity.addComponent(new UISelectionGroupComponent({ group: 'difficulty' }));
}
findEntity(difficulty).getComponent(UICheckboxComponent).setChecked(true);
on('settings/difficulty', (event) => {
if (event.eventType !== EventType.UICheckbox.Change || !event.data.checked) return;
difficulty = findEntity(event.entityId).name;
log('difficulty is now', difficulty);
});
}

How it works:

  • radio: true draws each checkbox as a ring and dot. The UI Selection Group with the same group name makes them exclusive: checking one unchecks the others.
  • setChecked(true) from the script applies the group too, so the starting choice is clean.
  • Every checkbox sends UICheckbox::change on the same signal. Turning one on also turns another off, so the script only acts on checked: true and reads which entity sent it from event.entityId.
  • Moving a nested element with its Transform. A nested element is placed by its parent’s layout; its position, rotation and scale do nothing. Change the parent’s flexDirection, alignItems, gap or padding, or the element’s own margin and size.
  • A UI under a plain group is a separate root. Only the direct parent counts. A UI Label under a Group under a panel is not laid out by the panel; it becomes a root of its own.
  • Expecting a fill from a UI Layout. A layout draws nothing. Use a UI Panel, or add a UI Panel component, for a visible surface.
  • Screen-space HUDs in XR. They are head-locked in a headset. Use a world-space panel there.
  • Panels see-through from behind. Add a UI Backdrop to world panels players can walk around.
  • Padding on containers that draw no box. UI Panel, UI List and UI Screen Space are spaced through their entity’s UI Layout props, not their own.
  • Many separate roots. Each root is its own tree with its own layout and drawing. Many controls under one panel cost less than as many free-standing roots.