Skip to content

Element

Refaltor77 edited this page Mar 29, 2026 · 1 revision

Element (Base Class)

All UI elements inherit from Element. It provides common properties shared by every control type.

Anchoring

9-point positioning system using anchor_from (origin on parent) and anchor_to (origin on self):

$element->setAnchorFrom(Element::ANCHOR_CENTER);
$element->setAnchorTo(Element::ANCHOR_CENTER);
Constant Value
ANCHOR_TOP_LEFT top_left
ANCHOR_TOP_MIDDLE top_middle
ANCHOR_TOP_RIGHT top_right
ANCHOR_LEFT_MIDDLE left_middle
ANCHOR_CENTER center
ANCHOR_RIGHT_MIDDLE right_middle
ANCHOR_BOTTOM_LEFT bottom_left
ANCHOR_BOTTOM_MIDDLE bottom_middle
ANCHOR_BOTTOM_RIGHT bottom_right

Sizing

$element->setSize(200, 100);                   // Fixed pixels
$element->setSizePercentage(50, 100);           // Percentage of parent
$element->setSizePixel(100, 50);                // Explicit pixel
$element->setCustomSize(["100%", "50px"]);      // Mixed / expressions
$element->setCustomSize(["fill", "100%"]);      // Fill remaining space
$element->setCustomSize(["100%-20px", 30]);     // Arithmetic
$element->setMaxSize([300, 200]);               // Maximum size
$element->setMinSize([50, 50]);                 // Minimum size

Positioning & Display

$element->setOffset(10, -5);      // X, Y offset from anchor
$element->setLayer(2);            // Z-layer (render order)
$element->setAlpha(0.8);          // Transparency (0.0 - 1.0)
$element->setEnabled(false);      // Disable element
$element->setIgnored(true);       // Hide without removing
$element->setClipsChildren(true); // Clip overflowing children

Children

$element->addChild($otherElement);          // Add one child
$element->addChilds([$el1, $el2, $el3]);    // Add multiple children

Bindings

Attach data bindings to any element. See Binding for full details.

use refaltor\ui\components\Binding;

$element->addBinding(Binding::global("#title_text"));
$element->addBindings([$binding1, $binding2]);
$element->setVisible("(#title_text = 'MY_TITLE')");  // Shorthand visibility

Animations

Reference animations declared on the Root. See Animation for full details.

$element->addAnim("@my_fade_in");
$element->addAnims(["@anim1", "@anim2"]);

Variables

Attach conditional variables. See Variable for full details.

use refaltor\ui\components\Variable;

$element->addVariable(Variable::when('$is_using_gamepad', ['$text' => 'Press A']));
$element->addVariables([$var1, $var2]);

Property Bag

$element->setPropertyBag([
    "#my_property" => "value",
]);

Inheritance (Extend)

Extend existing Bedrock UI elements using the @ notation:

$element = new Label("my_label", "text", "common.label");
// Generates: "my_label@common.label": { ... }

Button Factory

Enable the button factory system on a parent panel (required for Button elements):

$panel->enableFactoryButton($root);

Clone this wiki locally