Skip to content

Checkbox

A box the player can check and uncheck, with an optional label. The whole row is the hit area, and it grows on touch and console so it is easy to hit with a thumb or a gamepad. It comes in the same sizes as the other controls, and a row is as tall as a Button of its size.

Checkbox exampleCheckbox example
Lucy.Stack.new({
    gap = "Space2",
    children = {
        Lucy.Checkbox.new({
            label = "Mute music",
            onCheckedChange = function(checked)
                app.state:set("settings.music", if checked then 0 else 80)
            end,
        }),
        Lucy.Checkbox.new({ label = "Light show", checked = true }),
        Lucy.Checkbox.new({ label = "Show my dance combo", checked = true }),
    },
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
checkedbooleanfalseyesWhether the box is checked
labelstring""yesText beside the box
childrentableAnything after the label in its slot, such as a “NEW” badge. They take the label’s look and fade when disabled
disabledbooleanfalseyesGreys it out and ignores clicks
size"sm" | "md" | "lg" | "xl""md"yesThe box, the tick, the label’s text and the row’s height, as a Button of the same size
visiblebooleantrueyesShows or hides it
onCheckedChangefunctionCalled with (checked, handle) when it changes

It also takes the props every component takes.

Methods

NameWhat it does
checkbox:toggle()Checks it if unchecked, unchecks it if checked

Examples

Keep a ref and a checkbox in step. Writing the ref checks the box, and checking the box writes the ref:

Checkbox exampleCheckbox example
local muted = Lucy.ref(false)

Lucy.Checkbox.new({ label = "Mute the crowd", checked = muted, Parent = settings })

muted(true) -- the box is now checked

Read it from app state by id:

Checkbox exampleCheckbox example
Lucy.Checkbox.new({ id = "rules", label = "I agree to the club rules", Parent = welcome })

if app.state:get("ui.rules.checked") then
    enterLobby()
end

A checkbox comes in the same sizes as the other controls, and each size is as tall as a Button of that size, so it lines up beside them. Go a size up where it sits next to big buttons, such as a “don’t show this again” beside a large ENTER THE CLUB:

Checkbox exampleCheckbox example
Lucy.Stack.new({
    gap = "Space3",
    children = {
        Lucy.Checkbox.new({ label = "Small", size = "sm", checked = true }),
        Lucy.Checkbox.new({ label = "Medium", size = "md", checked = true }),
        Lucy.Checkbox.new({ label = "Large", size = "lg", checked = true }),
        Lucy.Checkbox.new({ label = "Extra large", size = "xl", checked = true }),
    },
    Parent = parent,
})

Preview

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