Skip to content

Slider

Picks a number between a minimum and a maximum, for settings such as volume or camera speed. Drag the thumb or press anywhere on the track with a mouse or a finger. On keyboard and gamepad, focusing the slider rings the whole control and the arrows move focus freely; press A (or Enter) to grab the thumb so the arrows along it change the value (faster while held), then press B (or A again) to commit and return to normal navigation. It can lie flat or stand up, and grows on touch and console.

Slider exampleSlider example
Lucy.Stack.new({
    gap = "Space2",
    children = {
        Lucy.Text.new({ text = "Music volume", variant = "label" }),
        Lucy.Slider.new({
            value = app.state:ref("settings.music"),
            showValue = true,
            format = function(value)
                return `{value}%`
            end,
            onValueCommit = function(value)
                app.toast(`Music at {value}%`)
            end,
        }),
    },
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
valuenumber0yesThe current value
minnumber0yesThe lowest value
maxnumber100yesThe highest value
stepnumber1yesValues snap to min plus a whole number of steps
orientationstring"horizontal"yeshorizontal or vertical (up is more)
sizestring"md"yessm, md or lg: the thumb and track thickness
disabledbooleanfalseyesGreys it out and ignores input
visiblebooleantrueyesShows or hides it
showValuebooleanfalseShows the value beside the track, or under it when vertical
formatfunctionTurns the value into the shown text; whole numbers have no point by default
valueWidthnumber40Room for the shown value beside a flat slider, in pixels
lengthnumberLength in pixels. A flat slider fills its parent’s width without it; one standing up is 160
onValueChangefunctionCalled with (value, handle) on every change, while dragging too
onValueCommitfunctionCalled with (value, handle) when a drag or a held arrow ends and the value changed

It also takes the props every component takes.

onValueChange follows the value as it moves, for live previews. Use onValueCommit for anything costly, such as saving or telling the server: it runs once per drag. The select event (for UI sounds) is sent at the same time.

Methods

NameWhat it does
slider:setValue(value)Sets the value, snapped to the step and the range

Examples

Keep a setting in app state, preview it live and save it when the player lets go:

Slider exampleSlider example
local danceSpeed = app.state:ref("settings.danceSpeed")

Lucy.Slider.new({
    value = danceSpeed,
    min = 0.1,
    max = 2,
    step = 0.1,
    showValue = true,
    onValueCommit = function(value)
        remotes.SaveSetting:FireServer("danceSpeed", value)
    end,
    Parent = settings,
})

A small slider standing up, beside the Queue:

Slider exampleSlider example
Lucy.Slider.new({
    value = 50,
    orientation = "vertical",
    size = "sm",
    length = 120,
    Parent = queue,
})

Preview

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