Skip to content

Recipes ​

Short solutions to jobs that come up in most interfaces. Each one stands on its own, so copy the one you need.

Follow a Vide source ​

A component often has its state decided by something else, such as a selected tab or a value from your game's store. Read that source in a Vide effect and call setState from it. The handler then changes state whenever the source does.

Call setState inside Vide.untrack. A Vide effect runs again whenever a source it reads changes, so keeping the handler's own work out of the effect means it only runs again when the sources you read yourself change.

luau
const VideState = require("@VideState")
const Vide = require("@packages/Vide")

const schemas = VideState.schemas
const rules = VideState.rules

const TabRule = rules.static(schemas.new({
    transparency = schemas.float({ min = 0, max = 1 }),
}), {
    name = "Tab",
    allowedStateNames = { "Idle", "Selected" },
})

-- `selectedTab` holds the name of the tab that is open. `name` is this tab's own name.
return function(name: string, selectedTab: Vide.Source<string>)
    const handler = TabRule.new({
        Idle = { transparency = 0.6 },
        Selected = { transparency = 0 },
    }, if Vide.untrack(selectedTab) == name then "Selected" else "Idle")

    const animated = handler.animate({
        tweenDefault = {
            duration = 0.2,
        },
    })

    -- Runs whenever `selectedTab` changes, and only then.
    Vide.effect(function()
        const nextState = if selectedTab() == name then "Selected" else "Idle"

        Vide.untrack(function()
            handler.setState(nextState)
        end)
    end)

    return Vide.create "TextButton" {
        Name = name,
        Text = name,
        Size = UDim2.fromOffset(120, 40),
        BackgroundTransparency = animated.transparency,
        Activated = function()
            selectedTab(name)
        end,
    }
end
  • The effect must run inside a Vide scope, such as a component passed to Vide.mount or a Vide.root, like any other effect.
  • A setState to the state that is already active does nothing, so the effect can call it on every change.
  • The initial state is read with Vide.untrack as well, because the handler must start in the right state before the effect first runs.

Stagger a list ​

To make the rows of a list appear one after another, give each row its own handler and a growing delay when its state changes. Keep the handlers of one list in an array, so only that list is staggered and the order is the order of the array.

luau
const VideState = require("@VideState")
const Vide = require("@packages/Vide")

const schemas = VideState.schemas
const rules = VideState.rules

const RowRule = rules.static(schemas.new({
    transparency = schemas.float({ min = 0, max = 1 }),
}), { name = "ListRow" })

-- One frozen table shared by every row, so the rule checks it once.
const ROW_STATES: VideState.StaticStates<typeof(RowRule), "Hidden" | "Shown"> = {
    Hidden = { transparency = 1 },
    Shown = { transparency = 0 },
}

for _, state in ROW_STATES do
    table.freeze(state)
end

table.freeze(ROW_STATES)

const STAGGER = 0.05

return function(labels: { string })
    const handlers = {}
    const rows = {}

    for index, label in labels do
        const handler = RowRule.new(ROW_STATES, "Hidden")
        const animated = handler.animate({
            tweenDefault = {
                duration = 0.2,
            },
        })

        handlers[index] = handler
        rows[index] = Vide.create "TextLabel" {
            Text = label,
            LayoutOrder = index,
            Size = UDim2.new(1, 0, 0, 32),
            BackgroundTransparency = animated.transparency,
            TextTransparency = animated.transparency,
        }
    end

    local function show()
        for index, handler in handlers do
            -- The first row starts at once, the second after 0.05 seconds, and so on.
            handler.setState("Shown", { delay = (index - 1) * STAGGER })
        end
    end

    local function hide()
        for _, handler in handlers do
            handler.setState("Hidden")
        end
    end

    return rows, show, hide
end

To stagger every handler of a rule at once, wherever it is, use everyHandler.withRule(name).setState(state, { stagger = seconds }). It orders the handlers by when they were created, and reaches every list made from that rule, so the loop above is the better fit for one list.

Follow the player's reduced motion setting ​

Roblox has a Reduce Motion setting in its own menu, which GuiService.ReducedMotionEnabled reads. setReducedMotion makes every following state change instant, so pass that setting to it once when the game starts, and again whenever the player changes it.

luau
const GuiService = game:GetService("GuiService")
const VideState = require("@VideState")

local function apply()
    VideState.setReducedMotion(GuiService.ReducedMotionEnabled)
end

apply()
GuiService:GetPropertyChangedSignal("ReducedMotionEnabled"):Connect(apply)

If your game has its own animation setting too, turn reduced motion on when either one asks for it. Here the game's setting is a Vide source, and the effect follows both:

luau
const GuiService = game:GetService("GuiService")
const VideState = require("@VideState")
const Vide = require("@packages/Vide")

-- Your game's own setting, such as a toggle in its settings menu.
const animationsOff = Vide.source(false)
const robloxReducedMotion = Vide.source(GuiService.ReducedMotionEnabled)

GuiService:GetPropertyChangedSignal("ReducedMotionEnabled"):Connect(function()
    robloxReducedMotion(GuiService.ReducedMotionEnabled)
end)

Vide.root(function()
    Vide.effect(function()
        VideState.setReducedMotion(animationsOff() or robloxReducedMotion())
    end)
end)

A transition that is already running when the setting changes keeps playing. Only the state changes that start after it are instant.

Choose a static or a dynamic rule ​

Both kinds of rule check their states, have the same handler functions, and transition the same way. They differ in where the values of a state come from.

Static ruleDynamic rule
A state holdsPlain valuesOne Vide source per field
Values can change after newNo. Writing to handler.sources raises an errorYes. Write to a state's source, or to handler.sources for the active state
Values are checkedOnce, in newIn new, and again for the state each setState changes to
Changing the state, with the fields bound0.8–1.0 µs, close to writing plain Vide sources≈2.1 µs, because it checks the values
Many handlers with the same statesShare one check when the states table is frozenEach handler has its own sources, checked for each
handler.defaultsEmpty. A left-out field takes its plain defaultOne source per defaulted field, which you can write to

Use a static rule when every state's look is known when the component is created, such as a button whose colours come from a design table or from props. Most components fit this.

Use a dynamic rule when a state's values change while the component exists, such as a theme the player can switch, a colour picked at runtime, or a size that follows the screen. Write the new value to the state's source and the bound properties update, without a state change.

Without a schema, the same choice applies: VideState.staticHandler takes plain values like a static rule, and VideState.handler takes Vide sources like a dynamic rule. Neither checks the values, so a mistake is a warning instead of an error.

If only one or two values change, you can still use a static rule and keep those values out of it. Bind them to your own Vide sources, and let the rule handle the rest. The cost of each kind is on the Performance page.