pesde goatsoft / lucy

Select

A button that opens a list of options to pick one from: a server region, a sort order, a game mode. It shows the option picked, or a placeholder until there is one.

Select exampleSelect example
Lucy.Stack.new({
    gap = "Space2",
    children = {
        Lucy.Text.new({ text = "Next set", variant = "label" }),
        Lucy.Select.new({
            id = "genre",
            placeholder = "Genre",
            value = "house",
            items = {
                { value = "house", label = "House" },
                { value = "disco", label = "Disco" },
                "Techno",
            },
            onValueChange = function(value)
                app.toast(`Next set: {value}`)
            end,
        }),
    },
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
itemstableThe options, see Items
valueanyfalseyesThe value picked, or false for none yet. With multiple, an array
typestring"default"default or check, see Types
multiplebooleanfalseLets the player pick several (a check select)
placeholderstring"Select"yesShown until something is picked
openbooleanfalseyesWhether the list is open
disabledbooleanfalseyesStops the player opening it, and closes it
sizestring or UDim2"md"yessm, md, lg or xl: the button fills the width at the height and text of a Button of the same size, so they line up in a row or a form. A UDim2 sets the button's box instead
sidestring"bottom"bottom, top, left or right: where the list opens. It flips when the other side has more room. auto opens below if it fits, then above, then beside
alignstring"start"start, center or end: how the list lines up with the button
onValueChangefunctionCalled with (value, handle) when the value changes
mountstring"shown"When the list and its options are built: "shown" the first time it opens, "immediate" when the select is made
keepbooleantruefalse takes the list away once it has closed and builds it again on the next open. With mount = "shown" only
onBuildfunctionCalled with the select once its list and options exist, each time they are built, before it shows
preloadlist or functionIts pictures, or a function returning them, read when needed. Warmed by warm(), and first in the queue when it opens. See Warming ahead
triggerbutton, handle or listWhat opens it. Hovered or focused, it warms (it does not open)
aheadoverlays or functionOverlays warmed when this one opens, such as a sheet opened from inside it
visiblebooleantrueyesShows or hides it

It also takes the props every component takes.

Items

An item is a value, shown as its text, or a table with value, and optionally label (or text; both work), icon, children and disabled. A disabled option shows faded and cannot be picked.

An option is a row with a slot: its icon, label and children (a flag, a count, a badge) sit in a row past the tick, in the option's colours. The label fills the row and truncates; the select's button shows the label alone, so give an option with children a label too.

Types

  • default: the picked option is highlighted in the list.
  • check: a tick in front of the picked option.

With multiple = true the select is a check select where the player can pick several. Each press ticks or unticks an option and the list stays open. The value is an array in the order of items, and the button shows the first label and how many more, such as "Pop +2".

Select exampleSelect example
Lucy.Select.new({
    multiple = true,
    placeholder = "Genres",
    items = { "Pop", "Hip hop", "House", "Techno" },
    onValueChange = function(genres)
        app.toast(`{#genres} genres in the mix`)
    end,
})

How it opens

The list and its options are built the first time it opens, before anything sees it open, so a form with many selects pays only for the buttons. The button shows the value from items without them. The first time the list shows, it waits one frame off the screen while its text is measured.

The list opens on the app's overlay layer, above everything opened before it:

  • Mouse and keyboard: under the button (or above it when there is more room there), at least as wide as the button.
  • Gamepad: the same, with the option already picked selected. When it closes, the button is selected again.
  • Touch: a drawer from the bottom of the screen, where a thumb can reach it. It slides up (it just appears with reduced motion) and leaves room above a phone's home bar.

A long list scrolls inside the space it has. B, Backspace or a press outside closes it without changing anything.

The button draws lucy:chevron-down, and a check select draws lucy:check. To use your own, see Icons Lucy draws.

Methods and events

NameWhat it does
select:select(value)Picks value as if the player had, and closes the list. With multiple, ticks or unticks it
select:open()Opens the list
select:close()Closes the list
select:toggle()Opens it if closed, closes it if open
select:build()Builds the list and its options now without opening it. Does nothing once they are built
select.itemsOne { root, label, check } per option, in order (nil until built)
select.content, select.listThe list and its scroll area (nil until built)
select.presentation()"popup" or "drawer", for this opening

When the player picks an option, it reports select with the value, for game sounds.

Examples

Read or change the value from anywhere by its id:

app.state:get("ui.genre.value")
app.state:set("ui.genre.value", "disco")

Keep it in step with a ref:

Select exampleSelect example
local sort = Lucy.ref("newest")
Lucy.Select.new({
    value = sort,
    items = {
        { value = "newest", label = "Newest" },
        { value = "popular", label = "Most played" },
    },
})

Preview

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