pesde goatsoft / lucy

Toggle

A button that stays pressed until it is clicked again: mute, a filter, show the map. For one choice out of several, use a Toggle Group.

Toggle exampleToggle example
Lucy.Stack.new({
    direction = "horizontal",
    gap = "Space2",
    children = {
        Lucy.Toggle.new({
            text = "Crowd cheers",
            pressed = true,
            onPressedChange = function(on)
                app.toast(if on then "Let them cheer" else "Crowd muted")
            end,
        }),
        Lucy.Toggle.new({ text = "Lights", pressed = true, variant = "outline" }),
        Lucy.Toggle.new({ text = "Shuffle", variant = "outline" }),
    },
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
textstring""yesThe text on it
pressedbooleanfalseyesWhether it is on
variantstring"default"yesdefault, outline, or line: text only, with a line under it while pressed
sizestring"md"yessm, md, lg or xl: each as tall as a Button of the same size
disabledbooleanfalseyesGreys it out and ignores clicks
visiblebooleantrueyesShows or hides it
iconstringAn icon name drawn before the text (toggle.icon)
childrentableAnything after the text in the toggle's slot, such as a count. With no text, they show alone. They take the toggle's colours, on, hover and disabled included
onPressedChangefunctionCalled with (pressed, handle) when it changes

It also takes the props every component takes.

When the player turns it on or off, it reports toggle with the new value for useUiSound.

Methods

NameWhat it does
toggle:toggle()Turns it on if off, off if on, as if the player clicked it

Examples

Keep it in step with a setting:

Toggle exampleToggle example
local lights = app.state:ref("settings.lights")

Lucy.Toggle.new({ text = "Lights", pressed = lights, variant = "outline", Parent = hud })

Preview

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