| name | spectacles-ui-kit |
| description | Reference guide for Spectacles UI Kit (FastUI/UIKit) — covering programmatic UI generation without prefabs. Includes Frame configuration (Large/Small appearance, autoShowHide, setFollowing), GridLayout (rows, columns, cellSize vec2, cellPadding vec4), and programmatic Button creation (RectangleButton, CapsuleButton, RoundButton). Shows the correct creation lifecycle (createComponent → set size → initialize() → set properties → bind events). Use this skill when building complex 2D spatial layouts, grids, menus, dialogs, or galleries entirely in TypeScript instead of assembling them manually in the Lens Studio inspector. |
Spectacles UI Kit — Programmatic UI Guide
While Lens Studio provides UI prefabs, you can construct complex interfaces entirely in TypeScript using the Spectacles UI Kit. This approach (sometimes called FastUI) is ideal for dynamic grids, scalable menus, and data-driven galleries.
Import Note: Components belong to SpectaclesUIKit.lspkg.
Component Lifecycle (CRITICAL)
When creating UI Kit components via script, you must follow this exact lifecycle sequence or the layout will fail to render:
global.scene.createSceneObject()
createComponent('TypeName')
- Set physical size/dimensions
- Call
.initialize()
- Set visual properties (appearance, margins)
- Bind interaction events
1. Creating a Frame (The Root Container)
The Frame component holds panels, adds a background plate, and handles focus behaviors.
import { Frame } from "SpectaclesUIKit.lspkg/Scripts/Components/Frame/Frame"
const frameObj = global.scene.createSceneObject("MainFrame")
const frameComp = frameObj.createComponent(Frame.getTypeName()) as any
frameComp.initialize()
frameComp.innerSize = new vec2(24, 26)
frameComp.appearance = "Large"
frameComp.autoShowHide = true
if (frameComp.setFollowing) {
frameComp.setFollowing(false)
}
2. Creating a Grid Layout
GridLayout automatically arranges child objects in rows and columns.
import { GridLayout } from "SpectaclesUIKit.lspkg/Scripts/Components/GridLayout/GridLayout"
const gridObj = global.scene.createSceneObject("GridLayout")
gridObj.setParent(frameObj)
const grid = gridObj.createComponent(GridLayout.getTypeName())
grid.rows = 3
grid.columns = 3
grid.cellSize = new vec2(4, 4)
grid.cellPadding = new vec4(0.5, 0.5, 0.5, 0.5)
grid.layoutBy = 0
grid.initialize()
3. Creating Buttons
You can create RectangleButton, CapsuleButton, or RoundButton.
import { RectangleButton } from "SpectaclesUIKit.lspkg/Scripts/Components/Button/RectangleButton"
const btnObj = global.scene.createSceneObject("Button_1")
btnObj.setParent(gridObj)
const button = btnObj.createComponent(RectangleButton.getTypeName()) as any
button.size = new vec3(4, 4, 1)
button.initialize()
button.renderOrder = 0
button.hasShadow = true
button.onTriggerUp.add(() => {
print("Button clicked!")
})
const textObj = global.scene.createSceneObject("Label")
textObj.setParent(btnObj)
const textComp = textObj.createComponent("Component.Text")
textComp.text = "Click Me"
textComp.size = 12
textComp.horizontalAlignment = HorizontalAlignment.Center
textComp.verticalAlignment = VerticalAlignment.Center
4. Manual Layouts (Optional)
If GridLayout is too rigid, you can calculate positions manually for simple vertical or horizontal stacks:
const spacing = 1.5
const btnHeight = 2.5
for (let i = 0; i < 5; i++) {
const btnObj = createMyButton()
const yPos = -i * (btnHeight + spacing)
btnObj.getTransform().setLocalPosition(new vec3(0, yPos, 0))
btnObj.setParent(menuContainer)
}
Common Gotchas
- Initialization Order: Passing properties to a UI element before
.initialize() rarely works. Set base dimensions usually before, then init, then appearance.
- RoundButton Size: Uses
.width, not .size.
- Corner Radius: Automatically calculated by the component based on size constraints. Don't overwrite it directly.
LayoutDirection enum: If you lack the TS import for LayoutDirection.Row, just pass the integer 0.
- Text Sizing: Text components map
size arbitrarily based on their transform hierarchy. 12-16 is a good starting point for button labels in a Large frame.
Reference Examples
- UIKitPatternGenerator.ts - An official comprehensive programmatic UI builder demonstrating all component types and lifecycle requirements.