Skip to content

Components ​

VideState lets you build UI components the way you would in Vue.js (at least from what I currently use for other projects).

A rule is the component's blueprint: the properties it has and the states it can be in.

A handler is one instance of that component, with its own state and values. Vide creates the Instances, and the handler decides what their properties are.

What we're building ​

A Play button for a game's main menu. It reacts to the mouse, and every reaction is a state:

StateWhenWhat the player sees
IdleNothing is happeningThe normal dark button
HoveredThe mouse is over itA lighter background and slightly larger text
PressedThe mouse button is held downA darker background
DisabledThe player cannot use it yetA greyed-out button that ignores the mouse

Every state sets the same three properties: backgroundColor, textColor, and textSize. The colors and the text size fade over 0.15 seconds when the state changes, instead of jumping.

The rule, the designs, and the button are shared by every button, so they live in three ModuleScripts in ReplicatedStorage: ButtonRule, ButtonDesigns, and Button. One LocalScript requires them and shows the result. Create them in the order below.

1. Describe the component ​

The rule lists the properties a button has and the states it can be in. allowedStateNames makes VideState reject any state that is not one of the four. Create a ModuleScript named ButtonRule in ReplicatedStorage that returns it:

luau
const VideState = require("@VideState")

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

return rules.dynamic(schemas.new({
    backgroundColor = schemas.color3(),
    textColor = schemas.color3(),
    textSize = schemas.integer({ min = 1 }),
}), {
    name = "Button",
    allowedStateNames = { "Idle", "Hovered", "Pressed", "Disabled" },
})

2. Design how each state looks ​

A design says what every state looks like. It is plain data, so it is easy to change or replace. The Look and Design types are read from the rule, so they always match it, and your editor flags a missing state, a misspelt one, or a property with the wrong type. InferRule gives the values of one state, and StaticStates with the state names gives a table where each of those states is required. Create a ModuleScript named ButtonDesigns in ReplicatedStorage. It holds the types and every design, so a look is changed in one place:

luau
const ReplicatedStorage = game:GetService("ReplicatedStorage")
const VideState = require("@VideState")
const ButtonRule = require(ReplicatedStorage.ButtonRule)

-- The values of one state, read from the rule.
export type Look = VideState.InferRule<typeof(ButtonRule)>
-- The four states of a design, each one required.
export type Design = VideState.StaticStates<typeof(ButtonRule), "Idle" | "Hovered" | "Pressed" | "Disabled">

local Designs: { [string]: Design } = {
    Default = {
        Idle = {
            backgroundColor = Color3.fromRGB(60, 60, 70),
            textColor = Color3.fromRGB(255, 255, 255),
            textSize = 18,
        },
        Hovered = {
            backgroundColor = Color3.fromRGB(80, 80, 95),
            textColor = Color3.fromRGB(255, 255, 255),
            textSize = 20,
        },
        Pressed = {
            backgroundColor = Color3.fromRGB(40, 40, 50),
            textColor = Color3.fromRGB(255, 255, 255),
            textSize = 18,
        },
        Disabled = {
            backgroundColor = Color3.fromRGB(35, 35, 35),
            textColor = Color3.fromRGB(120, 120, 120),
            textSize = 18,
        },
    },
}

return Designs

3. Build the button ​

Create a third ModuleScript named Button in ReplicatedStorage. It returns createButton, which makes one button. A handler needs a Vide source for each property in each state, so createStates builds them from a design. Calling ButtonRule.new creates the handler, which is this button's component instance. The button starts in Idle, or in Disabled when it is not enabled.

luau
const ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Point @Vide at the Vide package in your project.
const Vide = require("@Vide")
const ButtonRule = require(ReplicatedStorage.ButtonRule)
const ButtonDesigns = require(ReplicatedStorage.ButtonDesigns)

type Design = ButtonDesigns.Design

local function createStates(design: Design)
    local states = {}

    for stateName, look in design do
        states[stateName] = {
            backgroundColor = Vide.source(look.backgroundColor),
            textColor = Vide.source(look.textColor),
            textSize = Vide.source(look.textSize),
        }
    end

    return states
end

local function createButton(label: string, design: Design, enabled: boolean)
    local handler = ButtonRule.new(createStates(design), if enabled then "Idle" else "Disabled")

    -- The state sources change instantly. `animate` returns sources that fade between the states instead.
    local animated = handler.animate({
        tweenDefault = {
            duration = 0.15,
        },
    })

    local button = Vide.create "TextButton" {
        Name = label,
        Text = label,
        Size = UDim2.fromOffset(200, 50),
        AutoButtonColor = false,
        BackgroundColor3 = animated.backgroundColor,
        TextColor3 = animated.textColor,
        TextSize = animated.textSize,
    }

    -- The mouse events only change the state. The handler decides what each state looks like.
    button.MouseEnter:Connect(function()
        if handler.currentState() == "Idle" then
            handler.setState("Hovered")
        end
    end)

    button.MouseLeave:Connect(function()
        local state = handler.currentState()

        if state == "Hovered" or state == "Pressed" then
            handler.setState("Idle")
        end
    end)

    button.MouseButton1Down:Connect(function()
        local state = handler.currentState()

        if state == "Idle" or state == "Hovered" then
            handler.setState("Pressed")
        end
    end)

    button.MouseButton1Up:Connect(function()
        if handler.currentState() == "Pressed" then
            handler.setState("Hovered")
        end
    end)

    return button
end

return createButton

4. Show it ​

Create the Play button and mount it in the player's screen. This LocalScript requires everything it uses.

luau
const Players = game:GetService("Players")
const ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Point @Vide at the Vide package in your project.
const Vide = require("@Vide")
const ButtonDesigns = require(ReplicatedStorage.ButtonDesigns)
const createButton = require(ReplicatedStorage.Button)

const player = Players.LocalPlayer

Vide.mount(function()
    return Vide.create "ScreenGui" {
        Name = "Components",
        ResetOnSpawn = false,
        [1] = createButton("Play", ButtonDesigns.Default, true),
    }
end, player:WaitForChild("PlayerGui"))

Hover over the button and it lightens and grows. Hold the mouse down and it darkens. Pass false as the last argument to createButton and it starts greyed out and ignores the mouse.

To make another button, call createButton again. To make a different looking button, pass another design. See Inheritance. To keep the tween settings in one shared module instead of in each button, see Tween config.