Svelte and vanilla adapters
The Svelte and vanilla adapters connect Zag state machines to two different UI models. Svelte uses component lifecycle and compiler-managed reactivity. Vanilla JavaScript uses an explicit service lifecycle, subscriptions, and direct DOM updates. Both adapters turn machine services into connected element props.
Integration model
Both adapters follow the same product flow: create a machine service, connect it to a component API, normalize the returned props, and apply those props to elements. This keeps behavior in the machine while the adapter supplies the lifecycle and DOM contract.
flowchart LR
M[Machine and user props] --> S[Adapter service]
S --> C[Component connect API]
C --> N[Normalized element props]
N --> D[Rendered or updated DOM]
D --> E[User events]
E --> S
The two adapters differ mainly in who owns updates:
| Adapter | Service start and stop | Update source | DOM handoff |
|---|---|---|---|
| Svelte | Component mount and destroy | Svelte state and effects | Svelte prop spreading or actions |
| Vanilla | Caller-controlled start() and stop() | Service subscriptions | spreadProps() |
Svelte adapter
The Svelte adapter in packages/…/index.ts exposes the service and DOM helpers needed by Svelte components. It fits machine state into Svelte 5 runes without changing the machine's event, state, or effect model.
Service binding
useMachine creates a service from a machine and user props. The props can be a value or a function, so a Svelte component can pass values that change with its reactive state. The returned service exposes state, context, refs, computed values, scope, events, and send for the connected API.
The service starts when the component mounts and runs machine entry work at that point. It stops when the component is destroyed, which runs effect cleanup and machine exit work. This lifecycle also prevents normal component teardown from being mistaken for a machine transition.
sequenceDiagram
participant C as Svelte component
participant U as useMachine
participant M as Machine service
participant A as Connected API
C->>U: machine and reactive props
U->>M: create service
C->>M: mount
M-->>A: state and context
C->>A: connect(service, normalizeProps)
C->>M: send(event)
M-->>A: updated service values
C->>M: destroy
M-->>C: cleanup effects and exit work
References: packages/…/index.ts• L1-7 packages/…/machine.svelte.ts• L34-47 packages/…/machine.svelte.ts• L231-302 examples/…/+page.svelte• L5-13
DOM prop application
Svelte components normally apply the connected API with prop spreading. normalizeProps changes Zag's common prop shape into Svelte's DOM names, such as class, for, and Svelte event names. It also turns style objects into CSS text and preserves case-sensitive SVG attributes.
mergeProps combines several prop sources before rendering. It keeps all event handlers, combines class values in a form that Svelte can resolve, and merges styles into one Svelte-compatible value. The portal action moves a node to a chosen container or document body when a machine needs an element outside its normal DOM position.
References: packages/…/normalize-props.ts• L6-20 packages/…/normalize-props.ts• L22-67 packages/…/merge-props.ts• L17-47 packages/…/portal.ts• L1-21 examples/…/+page.svelte• L5-13 examples/…/+page.svelte• L42-46
Reactivity and subscriptions
Svelte state primitives keep machine state and bindable context values current for the component. A bindable value supports both controlled mode, where the parent remains the source of truth, and uncontrolled mode, where the adapter stores the value locally. A change callback tells the caller when the machine requests a controlled update.
Machine watches use track to run an action when selected values change. For external stores, useSyncExternalStore reads an initial snapshot, subscribes in an effect, and removes the subscription when the component is destroyed. These paths let connected APIs update without requiring the component to manage a manual render loop.
References: packages/…/bindable.svelte.ts• L5-52 packages/…/track.svelte.ts• L8-29 packages/…/use-sync-external-store.svelte.ts• L1-18 packages/…/machine.svelte.ts• L191-229
Imperative usage
Svelte remains the preferred place to render the connected API, but the service is also an imperative handle. Code can call send for an event and read state or context through the service when a component needs to coordinate with another system. The component still owns creation and destruction, so imperative calls must stay inside the service's active lifecycle.
For server rendering, service creation does not run root exit work during render. Browser-only DOM work belongs to mount-time actions such as portal rather than to server-rendered code.
References: packages/…/machine.svelte.ts• L253-302 packages/…/ssr.test.ts• L1-32 packages/…/machine.test.ts• L78-124
Vanilla adapter
The vanilla adapter in packages/…/index.ts provides a service class and DOM helpers for applications that do not have a component runtime. It supports direct DOM code, custom render loops, and class-based UI wrappers while preserving the same machine behavior used by the framework adapters.
Service binding
VanillaMachine constructs a service from a machine and its props. The caller starts the service, connects service to the machine's API, and applies the resulting props to the required elements. The caller must stop the service when the owning UI is removed.
updateProps replaces the current user prop source and publishes the merged result. This allows an imperative component to accept new inputs without making a new machine. The service also exposes state, context, refs, computed values, scope, events, and send through the same service shape used by connected APIs.
References: packages/…/index.ts• L1-4 packages/…/machine.ts• L46-104 packages/…/machine.ts• L211-221 packages/…/machine.ts• L310-355 examples/…/tooltip.ts• L5-14
DOM prop application
normalizeProps maps common Zag props to DOM attribute and property names before they reach the page. spreadProps then reconciles those props with one element. It updates attributes, DOM properties, boolean values, styles, and event listeners, and removes values that no longer exist.
The helper treats value, checked, and selected as DOM properties because their live element state matters. It patches style properties instead of replacing the full style block when it receives a style object. This preserves inline values written by other systems, such as positioning or layer code. A machine key can scope reconciliation when more than one machine updates the same element.
References: packages/…/normalize-props.ts• L3-47 packages/…/spread-props.ts• L1-65 packages/…/spread-props.ts• L67-173 examples/…/tooltip.ts• L16-28
Reactivity and subscriptions
Vanilla has no component render pass, so the service publishes its current service view when machine state or bindable context changes. subscribe lets the caller rerun its connect-and-render step, and the returned function removes that listener. Machine watches use value tracking and run actions only when tracked values change.
This model keeps the machine independent from the DOM. The subscriber decides which elements to update and can use the same connected API for a full redraw or a smaller targeted update.
References: packages/…/machine.ts• L61-64 packages/…/machine.ts• L125-131 packages/…/machine.ts• L333-375 packages/…/machine.ts• L378-396 examples/…/counter.ts• L105-130
Imperative usage
The normal vanilla pattern is explicit: create a machine, connect its service, render once, subscribe to changes, and start the machine. Event handlers call send, while later service notifications call the render function again. This pattern works for hand-built DOM components and for wrapper classes that own their element lookup.
start() initializes the machine and its entry work. stop() cleans active effects, removes internal subscriptions, runs exit work, and prevents later events from changing the service. DOM event cleanup is separate: callers must also use the cleanup function returned by spreadProps when the element or binding is removed.
References: packages/…/machine.ts• L223-263 packages/…/machine.ts• L310-339 packages/…/spread-props.ts• L164-173 examples/…/counter.ts• L76-130
Shared service contracts
Svelte and vanilla expose the same machine-facing ideas even though they use different update systems. A connected API reads the service and returns element props, while the adapter makes those props usable in its target DOM model.
Controlled values
Controlled values remain owned by user props in both adapters. A machine action can request a change through the bindable value's change callback, but the visible value changes only after the owner supplies the new prop. Uncontrolled values stay inside the adapter and change through the service.
References: packages/…/bindable.svelte.ts• L15-35 packages/…/bindable.ts• L14-43 packages/…/machine.test.ts• L485-521 packages/…/machine.test.ts• L824-867
Prop composition
Prop composition is the boundary between machine behavior and application-owned DOM behavior. Use the adapter's merge helper when several sources contribute event handlers, classes, styles, or attributes, then pass the result through the adapter's normalizer before rendering or applying it.
References: packages/…/merge-props.ts• L17-47 packages/…/merge-props.ts• L1 packages/…/normalize-props.ts• L33-47 packages/…/merge-props.test.ts• L18-65
Related pages
- Framework integrations explains the shared adapter role and machine-to-DOM flow.
- React and Preact adapters covers hook-based service binding and prop application.
- Vue and Solid adapters compares ref-based and signal-based reactivity.
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to Svelte or vanilla prop maps must stay aligned with the connected API's event names, form properties, style values, and SVG attributes.
- Changes to Svelte mount or destroy behavior must preserve entry, exit, effect cleanup, and server-rendering boundaries.
- Changes to
VanillaMachine.start,stop, orsubscribemust preserve the order between service lifecycle, notifications, and imperative renders. - Changes to
spreadPropsmust preserve stale attribute removal, event replacement, style ownership, and machine-key scoping. - Changes to controlled bindable values must preserve the callback contract that lets the prop owner reflect a requested value back into the service.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A vanilla caller sends events before start() or after stop() | Events do not drive transitions or actions | Start the service before wiring active controls and stop it with its owner |
| A controlled Svelte or vanilla value does not receive the requested prop update | The machine emits a change request but the visible value stays at the old external value | Update the owner prop from the bindable change callback |
Connected props bypass normalizeProps | Framework or DOM event names and attributes can be ignored or mapped incorrectly | Normalize every connected prop set before rendering or applying it |
| Repeated vanilla rendering does not remove old props or listeners | Stale attributes, styles, or handlers remain on the element | Use spreadProps for each update and retain its cleanup function |
| Two machines reconcile the same element without separate scopes | One machine can remove values owned by the other | Pass a distinct machine key to spreadProps |
| A vanilla service is stopped but DOM bindings are not cleaned up | Internal machine work stops while DOM listeners remain attached | Run both service cleanup and each spreadProps cleanup |
Key files
| File | Why to read it |
|---|---|
| packages/…/machine.svelte.ts | Shows Svelte service binding, lifecycle, state transitions, and machine effects. |
| packages/…/normalize-props.ts | Defines the Svelte DOM and event prop boundary. |
| packages/…/merge-props.ts | Shows how Svelte combines classes, styles, and handlers. |
| packages/…/use-sync-external-store.svelte.ts | Shows the Svelte subscription shape for external state. |
| packages/…/machine.ts | Shows explicit start, stop, update, publish, and subscribe behavior. |
| packages/…/spread-props.ts | Shows the vanilla DOM reconciliation contract. |
| packages/…/normalize-props.ts | Defines the vanilla DOM prop boundary. |
| examples/…/+page.svelte | Shows the short Svelte service-to-connected-API pattern. |
| examples/…/counter.ts | Shows an imperative machine, render loop, subscription, and event wiring. |