idemandsky/videstate

A state handler for Vide that lets you build UI components with states, and change their properties as the state changes.

VideState

A state handler for Vide. Build UI components with states, and let VideState change their properties whenever the state changes.

Documentation: https://idemandsky.github.io/VideState/

⚠️ Warning

VideState has only been benchmarked in Roblox Studio, and it has not been tested in production. Use it wisely: measure it in your own project, especially with many handlers or transitions running at once, before you depend on it.

Why use it

  • Mistakes are found early. A rule checks the values of every state and names the state and the field in the error. The Luau analyzer knows the type of every source, and checks an annotated table of states for a wrong value, a missing field, or a missing or misspelt state.
  • Motion is written once. Give a connection an easing, a duration, a speed, and a delay, and every component that uses it moves the same way. A transition that is interrupted continues from where it was, and it takes its own duration even when the frames are slow.
  • It costs little. With a static rule, changing state takes about as long as writing plain Vide sources by hand, and a dynamic rule adds about a microsecond to check its sources. A handler builds only the features it uses, keeps about 4 KB, and is collected when you let go of it. The numbers are on the Performance page.
  • It needs only Vide. There is no other dependency.

Features

  • Rule schemas with VideState.schemas.new: the fields of a component, built from field schemas (boolean, integer, float, string, enum, optional, union, withDefault, table, record, color3, numberRange, numberSequence, colorSequence, udim, udim2, vector2) and checked once. schemas.extend adds fields to a schema or merges two schemas, and withDefault and schemas.withDefaults give fields a default, so a state can leave them out. One schema can make several rules.
  • Rules (rules.dynamic and rules.static) take a rule schema and validate every state. allowedTransitions limits which state can follow which. A union field tweens only between two values of the same animatable type, such as two numbers. When the types differ, such as 100 and "Auto", it switches at the start of the transition.
  • Static rules with VideState.rules.static: each handler's states are plain values, checked once in new and fixed after that, so a state change checks nothing. Handlers that share one frozen table of states share one check. VideState.rules.extendStatic builds on a static rule.
  • Types for the analyzer: VideState.DynamicStates and VideState.StaticStates type a table of states from its rule, and take the state names, such as "Idle" | "Hover", to make each state required. VideState.InferRule gives the values of one state.
  • Rule inheritance with VideState.rules.extendDynamic: a new rule keeps every field, state name, and tag of its base and adds its own.
  • Handlers without a rule with VideState.handler, which takes the states as Vide sources, and VideState.staticHandler, which takes plain values that never change. Both skip the schema. The analyzer still infers each field's type, VideState.HandlerStates and VideState.StaticHandlerStates type the table from its values, and one warning lists any field that cannot tween.
  • Handlers hold one component's state, with sources that follow the active state and bind to Vide properties.
  • Events for state changes: onChange, onEnter, onExit.
  • Transitions with onTransition: each connection has its own easing, duration, speed, and delay, and can use other settings for particular state changes with tweenOverrides. Curves of your own come from VideState.bezier, VideState.easing, and VideState.spring, a spring-shaped curve that is not a physics spring, and VideState.bounce, a bouncing ball with settings for the height, number, and loss of its bounces. Each of these curves has lerp(from, to, alpha) to ease a value between two values or sources. VideState.tween checks a preset of tween settings where you write it, and debug.setFallbackWarnings warns when a setting source returns a bad value. An interrupted transition continues from its current value. animate gives the same transition as Vide sources, and progress gives its alpha as one source. playSequence plays several states in turn.
  • Transition controls: pause, resume, scaleSpeed, finish, onFinish, waitForFinish, and setState(name, { instant = true, delay = 0.2 }). everyHandler.setState takes a stagger to start handlers one after another.
  • Every handler at once: VideState.everyHandler has scaleSpeed, pause, resume, finish, setState, and destroy, plus count, isAnyRunning, and isAnyPaused. Chain withTags, withRule, and withStates, or their without forms, to pick the handlers.
  • Following a source: follow(source, toState) keeps the state in step with a Vide source, in place of an effect that calls setState.
  • Cleanup with destroy, which runs by itself when the handler was made inside a Vide scope, such as a component.
  • Debug tools: VideState.debug lists tracked handlers, and its view is a Vide component.
  • Testing: a manual clock (setManualClock, advanceClock) steps transitions by exact times.
  • Accessibility: setReducedMotion(true) makes every state change instant.

Install

With Ember:

embr add idemandsky/videstate

With pesde:

pesde add idemandsky/videstate

With Wally, in wally.toml:

[dependencies]
VideState = "idemandsky/[email protected]"

Its only dependency is Vide, which is installed with it.

Quick start

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

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

local PanelRule = rules.dynamic(schemas.new({
    visible = schemas.boolean(),
    order = schemas.integer({ min = 0 }),
}), { name = "Panel" })

local panel = PanelRule.new({
    Idle = { visible = Vide.source(false), order = Vide.source(0) },
    Active = { visible = Vide.source(true), order = Vide.source(1) },
}, "Idle")

panel.setState("Active")

Bind panel.sources.visible to any Vide property, and it follows the active state. The documentation covers every API.

License

MIT. See LICENSE.