State machine platform
Zag uses reusable state machines to model interactive behavior as a clear contract between events, state, data, side effects, and UI adapters. A machine definition can run as a service in different environments, while each framework adapter keeps the same behavior and exposes a service that consumers can read and drive.
Machine model
Machine definitions
A machine definition describes the behavior of one interaction without tying it to a rendering framework. It names the possible states, the events that can move the machine between states, and the work that runs when the machine enters, exits, or remains in a state. packages/…/types.ts defines this shared model, while packages/…/create-machine.ts validates the state tree when a machine is created.
The model keeps decisions separate from their effects. Guards choose whether a transition can run. Actions perform short work around a transition. Effects connect a state to an external resource and return cleanup work. This lets a component machine describe behavior in one place while its framework binding supplies the service lifecycle and update mechanism.
References: packages/…/types.ts• L141-241 packages/…/create-machine.ts• L4-40
Nested state model
Nested states express a broad mode and its active sub-mode as one state value. The platform tracks the full path, resolves initial child states, and can match either the full path or an ancestor such as open. Tags provide another way to ask whether the current state has a shared property such as an overlay role.
When a transition changes the state, the platform finds the common part of the old and new paths. It exits the old branch and enters the new branch in order. A transition can also re-enter the same state when the interaction must run its entry and exit behavior again.
References: packages/…/state.ts• L33-79 packages/…/state.ts• L92-140 packages/…/state.ts• L192-248 packages/…/nested-states.test.ts• L54-93
Service lifecycle
Service instances
A service is the live form of a machine definition. It provides the current state, context, computed values, element scope, refs, current event, and send operation to the adapter or component code. It also reports whether the service is not started, started, or stopped.
The service starts by entering the machine's initial state. It then accepts events and publishes changes to its consumers. Stopping the service removes active effects, runs machine exit work, and releases subscriptions. Framework adapters connect these steps to mount and unmount behavior, so a component does not need to manage the machine's resources directly.
References: packages/…/types.ts• L269-293 packages/…/machine.ts• L310-355 packages/…/machine.ts• L284-336
Event processing
Events are the main input to a service. The runtime looks for a matching transition from the active state and its ancestors, selects the first transition whose guard passes, and then resolves its target. A transition can change state, re-enter the current state, or run actions without changing state.
The event record keeps the current and previous event available to machine logic. Events may also carry source information for callbacks or use replacement keys when a newer pending event should supersede an older one. The machine runtime explains this processing order and the service lifecycle in more detail.
References: packages/…/types.ts• L89-119 packages/…/state.ts• L192-207 packages/…/machine.ts• L223-263
State and context handling
Context values
Context holds changing data that is not itself a state path. A machine creates bindable context values with an initial value and can read or update them through the service. A context value can be internally owned or controlled by props, with change callbacks used to report updates to the component.
Context is shared with guards, actions, effects, computed values, and watchers. This gives those parts one common view of interaction data without forcing every data change to become a state transition. The machine authoring page describes the conventions for choosing state, context, and derived values.
References: packages/…/types.ts• L12-20 packages/…/types.ts• L44-65 packages/…/machine.ts• L105-165 packages/…/bindable.ts• L5-43
Props and computed values
Props are the input boundary between a component and its machine. They can set initial behavior, control a value, provide callbacks, and supply DOM or accessibility options. Computed values derive a read-only answer from props, context, refs, event data, or scope when several inputs must be combined for consumers.
This boundary supports both uncontrolled behavior and controlled behavior. The machine still owns the interaction flow, but a consumer can provide a current value and receive a change request instead of allowing the service to store that value itself. The dialog machine shows this model for its open state and trigger value.
References: packages/…/types.ts• L163-175 packages/…/types.ts• L196-241 packages/…/machine.ts• L97-149 packages/…/dialog.machine.ts• L10-55
Scope and refs
Scope gives machine logic access to the component's root node, document, window, active element, and identified elements. Refs hold non-reactive handles such as timers or other external objects. Together they let a framework-neutral machine coordinate browser work without storing DOM details in its state value.
The scope is created from the consumer's identity and root-node settings. Framework adapters pass the same kind of scope to the machine, which keeps DOM behavior consistent across document and shadow-root use cases.
References: packages/…/types.ts• L67-87 packages/…/scope.ts• L4-19 packages/…/machine.ts• L87-165
Actor orchestration
Parent and child services
Zag supports actor-like orchestration by allowing one running service to be passed into another machine as a prop. The child service can read shared parent context, derive values from the parent, and send events back to the parent service. This keeps separate interaction lifecycles while giving them an explicit communication link.
The toast system uses this pattern. A toast is a child service of a toast group, reads group data for layout, and sends a removal event to the group when it must leave. The parent remains responsible for the collection, while each child owns its visible and timing behavior.
References: packages/…/toast.types.ts• L143-165 packages/…/toast.machine.ts• L49-73 packages/…/toast.machine.ts• L255-291
Service composition
Composition works through service contracts rather than through shared global state. A parent service exposes the state and context needed by a child, and the child uses ordinary events to request a parent change. This makes ownership clear: the service that owns a value also decides how that value changes.
The pattern is useful when one interaction contains repeated or independently timed parts. It avoids placing every child detail into one large state machine, while preserving a clear path for coordination between the parent and child services.
References: packages/…/types.ts• L269-285 packages/…/toast.machine.ts• L10-47 packages/…/toast.machine.ts• L255-257
Framework boundary
Adapter contract
Framework packages bind the same machine contract to framework lifecycle and reactivity rules. They create the service, connect state and context changes to rendering, and clean up effects when the host component is removed. The machine definition remains the source of interaction behavior.
This separation lets component packages share behavior across React, Preact, Vue, Solid, Svelte, and vanilla usage. The machine runtime focuses on how a service executes, while framework pages explain how each adapter presents that service to a rendering system.
References: packages/…/machine.ts• L46-95 packages/…/machine.ts• L37-50 packages/…/machine.ts• L30-51 packages/…/machine.svelte.ts• L34-48
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to
Machine,Service, orMachineSchemacan affect every framework adapter and every component machine that uses the shared type contract. - Changes to nested state resolution must preserve relative targets, initial child expansion, ancestor matching, and exit or enter ordering together.
- Changes to bindable controlled values must keep the relationship between internal updates and consumer change callbacks intact.
- Changes to parent service props must preserve both the child-to-parent event contract and the parent context data read by the child.
- Changes to service start and stop behavior must keep effect cleanup aligned with framework mount and unmount lifecycles.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A compound state has no valid initial child | The machine cannot build a valid state tree | createMachine builds the state index and rejects missing or unknown initial children. |
| A transition target uses the wrong relative form | The service enters a different state or leaves the target unresolved | Use bare names for sibling states and dot-prefixed names for child states. |
| A controlled context value changes without its change callback | The UI can request a change that the consumer does not receive | Define the controlled value and its change callback as one contract. |
| A service stops without effect cleanup | Outside listeners, focus traps, timers, or scroll locks can remain active | Let the owning framework lifecycle stop the service and run registered cleanups. |
| A child service receives the wrong parent service type | Child layout or coordination reads invalid parent data | Pass the parent service contract expected by the child machine. |
Key files
| File | Why to read it |
|---|---|
| packages/…/types.ts | Defines the shared machine, transition, context, parameter, service, and lifecycle contracts. |
| packages/…/create-machine.ts | Shows machine creation and the shared guard helpers. |
| packages/…/state.ts | Explains nested state paths, target resolution, transition lookup, tags, and enter or exit calculation. |
| packages/…/machine.ts | Gives the clearest end-to-end view of service construction, event processing, effects, start, stop, and subscriptions. |
| packages/…/machine.ts | Shows how the same service contract maps to a component lifecycle and reactive rendering. |
| packages/…/dialog.machine.ts | Illustrates a component machine with controlled props, context, nested behavior, effects, actions, and guards. |
| packages/…/toast.machine.ts | Illustrates parent and child service coordination through shared context and parent events. |
| packages/…/nested-states.test.ts | Demonstrates the observable contract for nested state matching, tags, transitions, and entry or exit order. |