Skip to content

UI Spacer

Updated

Invisible flexible gap that pushes siblings apart.

Type
ent-ui-spacer
Script class
UISpacerEntity
Family
UI

A UI Spacer is an invisible, flexible gap. Placed between elements in a row or a column, its UI Spacer component (comp-ui-spacer) takes the free space and pushes its neighbours apart: in a row Title · Spacer · Close, the title goes to the left edge and the close button to the right. It draws nothing and takes no pointer.

A spacer is always nested in a panel or layout; it only means something between siblings. Several spacers share the free space by their grow values, so two spacers around an element centre it, and a spacer with grow: 2 takes twice the share of one with grow: 1.

Use a spacer to push elements to opposite ends or to share leftover space. For an even gap between every element, set the layout’s gap instead. For a visible line, use a UI Divider.

The props that matter most:

  • grow: its share of the free space, relative to other growing siblings; 1 by default, 0 keeps it at size.
  • size: a minimum size in layout pixels (px) along the parent’s flow, kept even when there is no free space.
  1. Add a UI Spacer under a UI Panel or UI Layout, between the elements to push apart. See Scenes and hierarchy.
  2. Give the parent a fixed width (for a row) or height (for a column), so there is free space to take.
  3. Adjust grow and size if needed.

Goal: a title on the left and a close button on the right of a panel’s header row.

Header.jx
#pragma lifecycle(startup)
// Scene: a UI Panel named 'Window' (a column). The script adds a header row at its top.
function startup() {
const panel = findEntity('Window');
panel.getComponent(UILayoutComponent).setProps({ width: 400, padding: 12 });
const header = new UILayoutEntity();
header.getComponent(UILayoutComponent).setProps({ flexDirection: 'row', alignItems: 'center' });
const title = new UILabelEntity();
title.getComponent(UILabelComponent).setProps({ text: 'Inventory', fontSize: 20, fontWeight: 'bold' });
const spacer = new UISpacerEntity();
const close = new UIButtonEntity();
close.getComponent(UIButtonComponent).setProps({ text: 'Close', variant: 'subtle' });
close.addComponent(new SignalEmitterComponent({ signal: 'window:close' }));
header.add(title, spacer, close);
panel.add(header);
on('window:close', (event) => {
if (event.eventType === EventType.UIButton.Press) panel.setEnabled(false);
});
}

How it works:

  • The header row stretches to the panel’s 400 px width, so it has free space.
  • The spacer takes all of it, pushing the title to the left edge and the button to the right.

Goal: centre a label in a footer row, but keep at least 16 px on each side.

Footer.jx
#pragma lifecycle(startup)
// Scene: a UI Layout named 'Footer' (flexDirection 'row', width 360) holding,
// in order, a UI Spacer named 'Left Space', a UI Label, and a UI Spacer named 'Right Space'.
function startup() {
for (const name of ['Left Space', 'Right Space']) {
findEntity(name).getComponent(UISpacerComponent).setProps({ grow: 1, size: 16 });
}
}

How it works:

  • Two spacers with the same grow split the free space evenly, so the label sits in the middle.
  • size: 16 keeps a 16 px gap on each side even when the label’s text grows to fill the row.
  • Giving the left spacer grow: 2 instead would push the label two thirds of the way to the right.

What a new UI Spacer is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.

Component Type Role Per entity What it does
Layer comp-layer Required One Puts the entity on a render/raycast layer so cameras and rays can include or skip it.
Lock comp-lock Required One Editor-only flag that stops the entity from being selected or moved.
Enable comp-enable Required One Turns the entity and its children on or off: hides them and stops their behavior.
Transform comp-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
UI Spacer comp-ui-spacer Required One Invisible flexible gap that pushes siblings apart.

You can also add 1 optional components, such as Signal Emitter.

The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).

Prop Default Range
grow 1 0 ≤ x ≤ 1000
size 0 0 ≤ x ≤ 16384

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My UI Spacer.

UISpacerEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My UI Spacer');
if (!(entity instanceof UISpacerEntity)) return;
// Its UI Spacer component holds the settings listed under Props.
const component = entity.getComponent(UISpacerComponent);
log('grow is', component.props.grow);
component.setProps({ grow: 1.5 });
}