pesde goatsoft / lucy

Toggle Group

A row of toggles where the player picks one, or several: filter chips, a category bar, a vote from 1 to 5. Each item is a Toggle and looks like one.

Toggle Group exampleToggle Group example
Lucy.ToggleGroup.new({
    items = { "House", "Disco", "Techno", { value = "hiphop", text = "Hip hop" } },
    value = "House",
    onValueChange = function(genre)
        if genre then app.toast(`Queue: {genre} tracks`) end
    end,
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
itemstableThe items, in order (see below)
typestring"single"single picks one, multiple picks any number
valueanyfalse, or {} for multipleyesThe picked value, or the list of picked values
requiredbooleanfalseSingle only: picking the picked item again keeps it picked
variantstring"default"yesdefault, outline, line: text only, with a line under the picked items, or segmented: the group is a pill with the picked item raised in it (see below)
sizestring"md"yessm, md, lg or xl: each as tall as a Button of the same size
orientationstring"horizontal"horizontal for a row, vertical for a column
gapspacing"Space1"Space between items
scrollbooleanfalseA row too long for its room scrolls sideways and keeps the picked item in view, its ends fading out over ToggleGroup.fadeSize pixels (40) so players can see there is more. Tabs turn it on
hugbooleanfalseWith scroll: only as wide as its items, so what follows it in a row sits right after it (give it a UIFlexItem Shrink to scroll once room runs out). group.below() is the room kept under the items. Tabs hug line tabs
disabledbooleanfalseyesGreys every item out and ignores clicks
visiblebooleantrueyesShows or hides it
onValueChangefunctionCalled with (value, handle) when it changes

It also takes the props every component takes.

An item is a value (a string or a number, shown as its text) or a table:

FieldWhat it does
valueWhat the item stands for. Required
textIts text. Defaults to the value. label works too
iconAn icon name drawn before the text
childrenAnything after the text in the item's slot, such as a count or an icon. With an icon or children and no text, the item shows no text. They take the item's colours, picked and hover included
disabledGreys this item out
visibleShows or hides this item: true, false, a ref or a getter. A hidden item leaves the row and can't be picked. Defaults to true

In a single group, value is false until something is picked, and picking the picked item again clears it unless required is set. In a multiple group, value is a list, in the order of the items.

When the player picks an item, it reports select with that item's value for useUiSound.

handle.items holds the item buttons, in order.

Segmented

With variant = "segmented" the group draws a pill round its items, and the whole pill is as tall as a control of its size: a lg segmented group lines up with a lg Button, Select or Input beside it. The items fill the pill inside its padding, muted until picked; the picked one sits raised on the background colour. Use it for a mode switch in a header or toolbar, such as BASIC and ADVANCED. Tabs take it too.

Methods

NameWhat it does
group:select(value)Picks the item, as if the player clicked it

Examples

Genre filters the player can combine:

Toggle Group exampleToggle Group example
local genres = Lucy.ref({})

Lucy.ToggleGroup.new({
    type = "multiple",
    items = { "House", "Techno", "Pop", "Disco" },
    value = genres,
    size = "sm",
    variant = "outline",
    Parent = filters,
})

Rate the DJ's set from 1 to 5, with a star in each item:

Toggle Group exampleToggle Group example
local items = {}
for stars = 1, 5 do
    table.insert(items, { value = stars, children = { Lucy.Icon.new({ icon = "lucide-extended:star-filled" }) } })
end

Lucy.ToggleGroup.new({
    items = items,
    required = true,
    onValueChange = function(stars)
        Remotes.RateSet:FireServer(stars)
    end,
    Parent = ratePanel,
})

A VIP ROOM tab that shows up once the player has the pass. The group stays the same group: the tab just appears, and goes again if the pass does:

Toggle Group exampleToggle Group example
local hasPass = Lucy.ref(true)

Lucy.ToggleGroup.new({
    items = {
        { value = "tonight", text = "TONIGHT" },
        { value = "week", text = "THIS WEEK" },
        { value = "vip", text = "VIP ROOM", visible = hasPass },
    },
    value = "tonight",
    required = true,
    variant = "line",
    Parent = parent,
})

A BASIC / ADVANCED switch in an editor's header, as tall as the SAVE button beside it:

Toggle Group exampleToggle Group example
Lucy.Stack.new({
    direction = "horizontal",
    gap = "Space2",
    align = "center",
    children = {
        Lucy.ToggleGroup.new({
            items = { { value = "basic", text = "BASIC" }, { value = "advanced", text = "ADVANCED" } },
            value = "basic",
            required = true,
            variant = "segmented",
            size = "lg",
        }),
        Lucy.Button.new({ text = "SAVE", size = "lg" }),
    },
    Parent = parent,
})

Preview

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