In this example, we demonstrate how to dynamically add, move, and remove UI components in UI Builder. You can spawn any built-in or custom component, and set any of the properties, in this example we set the text and position of a button component. It is also possible to spawn components as children of other components, such as groups or tabs.
In this example UI, Users can manage the positioning and text of buttons through simple interactions, with updates immediately reflected in real-time. We also ensure that certain actions, such as moving or removing elements, are only possible when an item is selected, maintaining a smooth user experience.
Warning
Controls created from a script are not persisted with the UI. They exist only for the lifetime of the script run and are gone the next time the user interface is loaded.
Creating, moving and deleting controls
new Button({ name?, ...props }) creates a control and parents it to the root of the UI. Pass name to choose the identifier; a duplicate name throws control '<name>' already exists. Without a name, one is generated for you - that generated name is what btn.name returns below.
group.add(control) and tabs.add(tabNameOrIndex, control)move a control: it is removed from its previous parent. Both accept several controls in one call. tabs.add takes the tab title (case-insensitive) or the tab index, and does nothing for a tab that does not exist.
deleteElement(nameOrControl) removes the control from the script context and from the layout. Any variable still holding the control becomes stale, so do not keep references to deleted controls.
Info
This sample is TypeScript. Control in addChild(control: Control) is a type only - there is no runtime global by that name.
// Variable to keep track of toggle statelettoggle=false;/* Function to add a child control to a specific group or tab based on dropdown selection*/functionaddChild(control:Control){switch(dropdown.value){case'Group 1':returngroup1.add(control);case'Group 2':returngroup2.add(control);case'Tab 1':case'Tab 2':returntabs.add(dropdown.value,control);}}// Function to enable or disable buttons based on the selected list valuefunctionenableDisable(){btnMove.disabled=!list.value;btnRemove.disabled=!list.value;}// Event handler for the "Add" button click eventbtnAdd.onClick=()=>{// Create a new button with the specified text and positionconstbtn=newButton({text:text.value,top:vertex.value.y,left:vertex.value.x});// Add a click event listener to the new buttonbtn.onClick=e=>console.log("BUTTON",e.sender.text,"CLICK");/* Add the button's name to the list and set the list's value to the button's name*/list.addItem(btn.name);list.value=btn.name;// Add the button to the appropriate group or tabaddChild(btn);// Enable or disable the move and remove buttonsenableDisable();}// Event handler for the list's change eventlist.onChange=enableDisable;// Event handler for the "Move" button click eventbtnMove.onClick=()=>{// Find the element corresponding to the selected list valueconstelement=findElement(list.valueasstring);// Add the element to the appropriate group or tabaddChild(element);}// Event handler for the "Remove" button click eventbtnRemove.onClick=()=>{// Delete the element corresponding to the selected list valuedeleteElement(list.valueasstring);// Remove the element's name from the list and clear the list's valuelist.removeItem(list.value);list.value=null;// Nothing is selected any more, so disable the move and remove buttonsenableDisable();}// Event handler for the vertex change eventvertex.onChange=()=>{// Find the element corresponding to the selected list valueconstelement=findElement(list.valueasstring);if(!element)return;// Update the element's position based on the vertex valueselement.top=vertex.value.y;element.left=vertex.value.x;}// Event handler for the text change eventtext.onChange=()=>{// Find the button corresponding to the selected list valueconstelement:Button=findElement(list.valueasstring);if(!element)return;// Update the button's textelement.text=text.value;}
Tip
addItem is available on the Dropdown, Radio and List controls only. selectedIndex is derived from the control's current value, so two items with the same text always resolve to the first match.