Stores and shared type contracts
Stores and shared TypeScript contracts form the state boundary between Zag machines, framework adapters, and consumers. Stores publish state changes in a common shape, while core and type packages describe machine state, configuration, events, props, and service access so each layer can use the same contract.
Store primitives
Key-value stores
The key-value store is a small mutable state service for shared values that do not belong to one machine instance. It exposes reads, single-key writes, grouped updates, snapshots, and subscriptions. A comparison function prevents a write from publishing when the value has not changed.
This store fits coordination between machine instances. For example, the tooltip machine uses one store to record the visible tooltip and closes another instance when that shared identity changes. The store is therefore a communication point for related machines rather than the source of each machine's local state.
References: packages/…/store.ts• L1-63 packages/…/tooltip.store.ts• L1-7 packages/…/tooltip.machine.ts• L273-301 packages/…/tooltip.machine.ts• L416-426
Proxy stores
The proxy store makes an object reactive by observing assignments and deletions. Consumers subscribe to a proxy and read a stable snapshot. Nested proxy objects contribute their changes to the parent subscription, so a machine or adapter can observe a state tree through one boundary.
The store also supports explicit references for objects that must stay outside proxy tracking. Computed properties can expose derived values while still using the same underlying state. These features make the proxy store useful for framework-neutral bindable values and other state that must be observed without tying it to one rendering system.
References: packages/…/index.ts• L1-4 packages/…/proxy.ts• L13-29 packages/…/proxy.ts• L228-278 packages/…/proxy-computed.ts• L3-33 packages/…/bindable.ts• L1-43
Shared machine contracts
Bindable values
Bindable is the common value contract used by machine state and context. It gives the runtime a way to read and set a value, keep its initial value, compare or hash it, and report a change. BindableParams adds the boundary between controlled input and internal state through value, defaultValue, equality, change, and synchronization options.
Each framework adapter supplies the same contract with its own reactive primitive. This keeps machine logic independent of React state, Vue refs, Solid signals, Svelte state, or the vanilla proxy store. The machine can therefore use get, set, and invoke in the same way across adapters.
References: packages/…/types.ts• L34-76 packages/…/bindable.ts• L9-73 packages/…/bindable.ts• L5-67 packages/…/bindable.ts• L5-58 packages/…/machine.svelte.ts• L191-229
Machine services
Machine, MachineSchema, and Service define the exchange between a machine description and its consumer. A schema names the types of state, context, refs, computed values, events, guards, actions, and effects. A machine uses that schema to describe behavior. A service exposes the live state and context together with event sending, props, scope, refs, computed values, and lifecycle status.
The generic schema is the main type link across packages. Component packages define a schema for their behavior, while adapters return a Service with those same types. This preserves the component's state and configuration names from machine authoring through the public API.
References: packages/…/types.ts• L97-121 packages/…/types.ts• L177-242 packages/…/types.ts• L251-291 packages/…/machine.ts• L37-40 packages/…/machine.ts• L30-33
Consumer-facing type contracts
Configuration and DOM props
The shared types package defines common configuration values such as direction, orientation, locale, identifiers, and root-node lookup. It also defines the prop normalization contract that turns component-level DOM props into framework-specific attributes and event handlers. This lets a machine describe behavior once while an adapter applies the result to its target view system.
PropTypes names the DOM roles a component can produce. NormalizeProps maps those roles to functions that accept HTML, ARIA, data, and style values. The contract is broad enough for framework adapters while keeping component APIs typed at their public boundary.
References: packages/…/index.ts• L13-56 packages/…/prop-types.ts• L3-79 packages/…/index.ts• L1-4 packages/…/index.ts• L1-5
Component schemas
Component packages combine shared types with local schemas to describe their public API. A schema connects consumer props to machine state and context, then connects the resulting service to a component API. The tooltip contract illustrates this path: its props include controlled and default values, its schema names machine state and context, and its API exposes typed state accessors and DOM prop builders.
This pattern makes configuration changes visible across the full boundary. Consumers get typed props and callbacks, machines get typed values and events, and adapters can return the same component API without redefining the behavior model.
References: packages/…/tooltip.types.ts• L35-145 packages/…/tooltip.types.ts• L151-189 packages/…/index.ts• L1-16
Cross-framework state flow
Adapter synchronization
Framework adapters translate the shared subscription and snapshot model into each framework's lifecycle. An adapter reads an initial snapshot, subscribes while the consumer is mounted, updates the framework's reactive value when the store changes, and removes the subscription when the consumer is destroyed.
The same contract supports different update models. React and Preact use an external-store interface, while Vue, Solid, and Svelte connect the subscription to their own refs, signals, or reactive state. The machine service remains the stable boundary between those choices and the consumer.
The diagram shows how a machine value moves through a bindable or store subscription to a framework consumer.
flowchart LR
A[Machine or shared store] --> B[Bindable or subscribe/getSnapshot contract]
B --> C[Framework adapter]
C --> D[Reactive consumer state]
D --> E[Normalized DOM props and component API]
E -->|user event| A
References: packages/…/use-sync-external-store.ts• L1-9 packages/…/use-sync-external-store.ts• L1-23 packages/…/use-sync-external-store.ts• L1-20 packages/…/use-sync-external-store.svelte.ts• L1-19 packages/…/machine.ts• L242-282
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
Bindable,BindableParams, and every frameworkbindableimplementation aligned. A mismatch can change controlled-value or change-callback behavior in only one adapter. - Keep
MachineSchemafield names aligned with component schemas. A renamed state, event, context, or implementation key can break type inference between a machine and its service. - Preserve the
subscribeand snapshot contract when changing a store. Framework adapters depend on an unsubscribe function and on a fresh read after each notification. - Update both the shared prop types and the adapter normalizer when adding a new DOM or ARIA output. A type-only change can leave a runtime prop path unsupported.
- Keep cross-instance stores scoped to the behavior they coordinate. A shared identity change must still cause the owning machine to remove its subscription during cleanup.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A store publishes without changing its value | Consumers perform needless updates and can repeat derived work | Compare values before publishing in createStore or the bindable implementation |
| A consumer keeps a store subscription after its machine or view stops | A stopped machine can react to later shared changes | Return and run the unsubscribe function from the effect cleanup |
| A controlled value is written as if it were internal state | The local value diverges from the consumer's source of truth | Keep value and defaultValue distinct and report changes through onChange |
| A machine schema and its service use different keys or value types | Events, context reads, or public state access lose type safety and can fail at runtime | Define the component schema once and use Machine<T> and Service<T> with that schema |
| A prop role exists in a component API but not in the normalizer | The consumer receives typed props that are not applied to the target element | Keep PropTypes, NormalizeProps, and adapter normalization in sync |
Key files
| File | Why to read it |
|---|---|
| packages/…/store.ts | Defines the small key-value store contract for shared coordination. |
| packages/…/proxy.ts | Defines reactive object updates, subscriptions, and snapshots. |
| packages/…/proxy-computed.ts | Shows how derived values fit into a proxy store. |
| packages/…/types.ts | Defines bindables, machine schemas, transitions, and services shared by runtimes and adapters. |
| packages/…/index.ts | Defines common configuration and geometry contracts used by component packages. |
| packages/…/prop-types.ts | Defines typed DOM output roles and prop normalization. |
| packages/…/bindable.ts | Shows the shared bindable contract applied to a framework state model. |
| packages/…/bindable.ts | Shows how the proxy store supplies bindable state without a framework runtime. |
| packages/…/use-sync-external-store.ts | Shows lifecycle-safe subscription and snapshot updates for Vue. |
| packages/…/tooltip.types.ts | Illustrates the path from public props to a typed machine schema and component API. |
| packages/…/tooltip.machine.ts | Shows a shared store coordinating multiple machine instances. |
Related pages: Public API foundations, Machine runtime and service lifecycle, Framework integrations