Skip to main content

Toggle Group

A Toggle Group is a non visual component that orchestrates selection across multiple toggleables. It can manage events, and enforce selection options across the entire group. This component makes sure maximum one toggle of the group is always selected. This is useful for making selection based interactions

Switch Group

A Switch Group is essentially identical to a Toggle Group, except you attach Switches as opposed to Toggles.

Component Breakdown



Allow All Toggles OffAllows the group to have no toggles selected. The default is false.
Add CallbacksAdds the editor hooks for the event callbacks to the element.
On Toggle Selected CallbackFires a callback event any time one of the attached toggles is selected.
TogglesThe in panel interface for attaching Toggles to this group. Toggles added here are managed by this Toggle Group element.

Code Example

import { ToggleGroup } from 'SpectaclesUIKit.lspkg/Scripts/Components/Toggle/ToggleGroup';
import { RoundButton } from 'SpectaclesUIKit.lspkg/Scripts/Components/Button/RoundButton';

/**
* A simple programmatic toggle group example
*/

@component
export class ExampleToggleGroupScript extends BaseScriptComponent {
onAwake() {
const toggleGroup = this.sceneObject.createComponent(
ToggleGroup.getTypeName()
);

for (let i = 0; i < 8; i++) {
const toggle = this.createToggleButton(i);
toggleGroup.registerToggleable(toggle);
}

toggleGroup.onToggleSelected.add((args) => {
print(`Toggle selected: ${args.toggleable.sceneObject.name}`);
});
}

createToggleButton(index: number) {
const object = global.scene.createSceneObject('Toggle ' + index);
object.setParent(this.sceneObject);
const toggle = object.createComponent(RoundButton.getTypeName());
toggle.setIsToggleable(true);
toggle.width = 2;
toggle.initialize();
toggle.transform.setLocalPosition(new vec3(0, (index + 1) * 2, 0));
return toggle;
}
}
Was this page helpful?
Yes
No