Skip to content

Button

A button the player can click, tap or select with a gamepad. It comes in six variants and five sizes.

Button exampleButton example
Lucy.Stack.new({
    direction = "horizontal",
    gap = "Space2",
    children = {
        Lucy.Button.new({
            text = "Hit the floor",
            icon = "lucide-extended:music-filled",
            onClick = function()
                app.toast("See you on the dance floor")
            end,
        }),
        Lucy.Button.new({ text = "Emote Shop", icon = "lucide-extended:shopping-bag-filled", variant = "secondary" }),
        Lucy.Button.new({ text = "Settings", variant = "outline" }),
    },
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
textstring"Button", or "" with an iconyesThe label
iconstringyesAn icon name drawn before the text, in the text’s colour. See below
childrentableAnything after the text in the button’s slot (a price, a second icon), before the key hint. They take the button’s colours, hover and disabled included
variantstring"default"yesdefault, destructive, outline, secondary, ghost or link
sizestring"md"yessm, md, lg, xl (a game’s HUD) or icon
disabledbooleanfalseyesGreys the button out and ignores clicks
visiblebooleantrueyesShows or hides the button
hinttable or stringA key to show after the text: the props of a Kbd, or an action’s name. See below
onClickfunctionCalled with the handle when the button is clicked

It also takes the props every component takes.

Methods and events

NameWhat it does
button.ActivatedA signal that fires on every click
button:click()Runs a click from code, as if the player clicked

A disabled button ignores clicks, and a gamepad skips over it. Clicks within 50 ms of the last one are ignored too, so one press of Enter never counts twice.

Icons

icon draws an icon before the text, coloured like the text in every variant, and sized with the button: sm for sm and md buttons, md for lg and icon, xl for xl. With no text, the icon shows alone; use size = "icon" for a square button.

Button exampleButton example
Lucy.Button.new({ text = "Queue", icon = "lucide-extended:list-music-filled", variant = "outline", Parent = bar })
Lucy.Button.new({ icon = "lucide-extended:settings-filled", size = "icon", variant = "ghost", Parent = bar })

button.icon is the Icon, and button:set("icon", name) changes it. A button made without icon has no icon to change. The name has to be registered first, like any icon; see Icons.

Key hints

hint puts a key cap after the text, showing the right key for the player’s input: the keyboard key, the controller’s button, and nothing on touch.

Button exampleButton example
Lucy.Button.new({
    text = "Dance",
    hint = { keyboard = Enum.KeyCode.E, gamepad = Enum.KeyCode.ButtonX },
    onClick = dance,
    Parent = hud,
})

A hint needs text beside it: an icon button (size = "icon") is a fixed square with no room for the key cap, so give it text when it has a hint.

It takes the same fields as Kbd: keyboard, gamepad and touch, each a value, a ref or a getter. The hint only shows the key; binding the key to the button’s action is up to your game.

When the button does the same thing as one of your actions, give hint the action’s name instead. The key cap then shows that action’s keys, and follows them when a player changes them:

app.actions:on("dance", dance)

Lucy.Button.new({
    text = "Dance",
    hint = "dance",
    onClick = dance,
    Parent = hud,
})

Define the action before you make the button. For other Kbd fields as well, such as variant, pass a table: hint = { action = "dance", variant = "mono" }.

With a hint, the text sits in button.label and the key cap is button.hint (a Kbd handle). Both follow the button’s variant, size and disabled state. Face buttons keep their controller colours on any button; pass variant = "mono" in the hint to draw them in the button’s text colour. Without one, the button is a single TextButton as usual.

Slot

A button is a box with a slot. Its icon, text, children and key hint sit in a row, and everything in it takes the button’s colours:

Lucy.Button.new({
    text = "UNLOCK",
    children = {
        Lucy.Icon.new({ icon = "lucide-extended:coins-filled" }),
        "199",
    },
    Parent = card,
})

A string becomes text in the button’s font and size. A Text or Icon in the slot takes the button’s colour over its own variant, and fades with it when disabled; give a full colour picture the ui-icon-untinted tag to keep its own colours.

Examples

Every variant:

Button exampleButton example
for _, variant in { "default", "secondary", "outline", "ghost", "destructive", "link" } do
    Lucy.Button.new({ text = variant, variant = variant, size = "sm", Parent = row })
end

A buy button for an emote that follows the player’s coins and turns off while the purchase runs (buyEmote is your game’s own function):

Button exampleButton example
local coins = app.state:ref("player.coins")
local buying = Lucy.ref(false)

Lucy.Button.new({
    text = function() return `Buy Moonwalk for 300 ({coins()} left)` end,
    disabled = function() return buying() or coins() < 300 end,
    onClick = function()
        buying(true)
        buyEmote("Moonwalk")
        buying(false)
    end,
    Parent = shop,
})

Disable a button from anywhere by its id:

Button exampleButton example
Lucy.Button.new({ id = "startSet", text = "Start your set", Parent = booth })

app.state:set("ui.startSet.disabled", true) -- someone else is on the decks

A close button, the one dialogs and sheets put in their top right corner: a ghost icon button with a large ×. It takes the same props as Button.new.

local close = Lucy.Button.close({ onClick = closeQueue })

Preview

In Studio, from the command bar: Lucy.Preview.show("Button").