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.extendadds fields to a schema or merges two schemas, andwithDefaultandschemas.withDefaultsgive fields a default, so a state can leave them out. One schema can make several rules. - Rules (
rules.dynamicandrules.static) take a rule schema and validate every state.allowedTransitionslimits which state can follow which. Aunionfield tweens only between two values of the same animatable type, such as two numbers. When the types differ, such as100and"Auto", it switches at the start of the transition. - Static rules with
VideState.rules.static: each handler's states are plain values, checked once innewand fixed after that, so a state change checks nothing. Handlers that share one frozen table of states share one check.VideState.rules.extendStaticbuilds on a static rule. - Types for the analyzer:
VideState.DynamicStatesandVideState.StaticStatestype a table of states from its rule, and take the state names, such as"Idle" | "Hover", to make each state required.VideState.InferRulegives 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, andVideState.staticHandler, which takes plain values that never change. Both skip the schema. The analyzer still infers each field's type,VideState.HandlerStatesandVideState.StaticHandlerStatestype the table from its values, and one warning lists any field that cannot tween. - Handlers hold one component's state, with
sourcesthat 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, anddelay, and can use other settings for particular state changes withtweenOverrides. Curves of your own come fromVideState.bezier,VideState.easing, andVideState.spring, a spring-shaped curve that is not a physics spring, andVideState.bounce, a bouncing ball with settings for the height, number, and loss of its bounces. Each of these curves haslerp(from, to, alpha)to ease a value between two values or sources.VideState.tweenchecks a preset of tween settings where you write it, anddebug.setFallbackWarningswarns when a setting source returns a bad value. An interrupted transition continues from its current value.animategives the same transition as Vide sources, andprogressgives its alpha as one source.playSequenceplays several states in turn. - Transition controls:
pause,resume,scaleSpeed,finish,onFinish,waitForFinish, andsetState(name, { instant = true, delay = 0.2 }).everyHandler.setStatetakes astaggerto start handlers one after another. - Every handler at once:
VideState.everyHandlerhasscaleSpeed,pause,resume,finish,setState, anddestroy, pluscount,isAnyRunning, andisAnyPaused. ChainwithTags,withRule, andwithStates, or theirwithoutforms, 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 callssetState. - Cleanup with
destroy, which runs by itself when the handler was made inside a Vide scope, such as a component. - Debug tools:
VideState.debuglists tracked handlers, and itsviewis 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.