Introducing a new way to review your PRs.

Read the announcement

chakra-ui / zag

Build your design system in React, Solid, Vue, Svelte or Vanilla. Powered by finite state machines

View on GitHub

19 documents · Updated Sep 21, 2026

docs / framework-integrations

Framework integrations

Framework integrations connect framework-neutral UI machines to the state, render, and lifecycle systems of a UI framework. They keep machine behavior shared while adapting state updates, DOM props, refs, portals, effects, and mount cleanup to React, Preact, Vue, Solid, Svelte, or browser APIs.

Adapter boundary

The adapter is the boundary between a machine service and a component or DOM tree. A machine defines states, events, context, actions, guards, effects, refs, and computed values. The adapter gives that service the update and lifecycle rules needed by its host framework.

Machine service

Each component-facing adapter creates a service from a machine and user props. The service exposes the same core ideas across frameworks: current state, event dispatch, context access, computed values, refs, scope data, and status. This lets a component machine describe behavior once and lets each framework decide how that behavior becomes reactive UI. See component machines for the machine-facing model and machine runtime for the shared service flow.

References: packages/…/create-machine.ts• L24-39 packages/…/machine.ts• L37-40 packages/…/machine.ts• L326-336 packages/…/machine.ts• L343-354

State and context updates

Adapters represent machine state and context as bindable values. A bindable value can be controlled by outside props or owned by the machine. A state change calls the machine transition work and then reaches the host framework through its normal update model. This keeps event handling and state rules in the machine while rendering stays in the host application.

The adapters also provide framework-specific tracking for effects that depend on machine values. React uses effect dependencies, Vue watches reactive values, and the other adapters use their host signal or subscription model. The important contract is that a tracked effect runs when its declared machine inputs change and can be cleaned up with the machine lifecycle.

References: packages/…/bindable.ts• L9-59 packages/…/track.ts• L5-21 packages/…/bindable.ts• L5-37 packages/…/track.ts• L4-20 packages/…/machine.ts• L196-234 packages/…/machine.svelte.ts• L191-229

Refs and scope

Refs hold mutable values that machine logic can read and write without making every change a render update. They commonly represent DOM nodes, timers, or other short-lived values. Scope gives a machine stable identity data and a root-node lookup so generated ids and DOM work remain tied to the right component or document.

References: packages/…/refs.ts• L6-16 packages/…/machine.ts• L41-44 packages/…/refs.ts• L1-11 packages/…/machine.ts• L34-37 packages/…/machine.svelte.ts• L38-41 packages/…/machine.ts• L93-103

Rendering and DOM interfaces

Machine output is not a framework component by itself. Adapters turn machine prop data into the prop, attribute, event, style, and DOM-node forms expected by the renderer. This separation allows the same component machine to support different rendering systems without changing its behavior model.

Prop normalization

normalizeProps translates machine prop objects into the naming and value rules of the target renderer. It maps differences such as className versus class, focus and change event names, boolean attributes, and style values. mergeProps combines prop sources so a component can add its own handlers or attributes without losing machine behavior.

References: packages/…/normalize-props.ts• L4-15 packages/…/normalize-props.ts• L6-15 packages/…/normalize-props.ts• L49-68 packages/…/normalize-props.ts• L7-16 packages/…/spread-props.ts• L67-137

Portals

A portal moves rendered content to a chosen container while keeping the machine’s scope and event behavior tied to the original component. The React and Preact adapters expose a component portal. Svelte exposes a DOM action. These helpers can use a supplied container or the document associated with the machine root, and they can leave content in place when disabled or during server rendering where supported.

References: packages/…/portal.tsx• L7-22 packages/…/portal.tsx• L11-39 packages/…/portal.ts• L1-21

DOM prop spreading

The vanilla adapter applies normalized machine props directly to an element because no framework renderer performs that work. It reconciles attributes, DOM properties, styles, and event listeners while preserving styles written by other browser services. This gives vanilla consumers the same machine-to-DOM contract that framework renderers provide through their own prop systems.

References: packages/…/spread-props.ts• L35-65 packages/…/spread-props.ts• L67-173 packages/…/index.ts• L1-4

Framework lifecycle bindings

The six adapters share the same machine concepts but use different host lifecycle rules. The child pages describe the framework-specific APIs and behavior in more detail: React and Preact adapters, Vue and Solid adapters, and Svelte and vanilla adapters.

React and Preact adapters

React and Preact expose a hook-style useMachine API. They start the machine when the host component mounts, run entry and state effects through the machine service, and stop those effects when the component unmounts. Their bindings also account for host scheduling so state transitions and effect cleanup stay aligned with component updates. Both adapters provide renderer prop normalization and portals.

References: packages/…/machine.ts• L290-317 packages/…/portal.tsx• L13-22 packages/…/index.ts• L1-5 packages/…/machine.ts• L234-256 packages/…/portal.tsx• L17-39 packages/…/index.ts• L1-5

Vue and Solid adapters

Vue and Solid connect machine values to their native reactive systems. Vue accepts plain or reactive props and starts and stops the service with component mount hooks. Solid uses accessors and signal-based updates with mount and cleanup hooks. Both adapters expose machine services and prop normalization, but they do not define the React-style portal component used by the React and Preact packages.

References: packages/…/machine.ts• L30-37 packages/…/machine.ts• L242-265 packages/…/index.ts• L1-4 packages/…/machine.ts• L30-37 packages/…/machine.ts• L238-257 packages/…/index.ts• L1-5

Svelte and vanilla adapters

Svelte binds machine state to Svelte’s reactive values and component mount and destroy hooks. Its portal action moves an existing DOM node and removes it when the action is destroyed. The vanilla adapter has no component host, so the caller owns the service lifetime: create it, call start, subscribe to updates, update props when needed, and call stop to release effects and subscriptions.

References: packages/…/machine.svelte.ts• L34-40 packages/…/machine.svelte.ts• L233-251 packages/…/portal.ts• L7-21 packages/…/machine.ts• L211-221 packages/…/machine.ts• L310-339

Machine lifecycle flow

The lifecycle flow is shared in intent even when each framework uses a different mount and update API. The adapter starts the service, turns events into machine transitions, publishes state changes to the renderer, and runs cleanup when the host removes the service.

flowchart LR
    A[Component or DOM setup] --> B[Create machine service]
    B --> C[Start on host mount]
    C --> D[Receive event]
    D --> E[Resolve transition]
    E --> F[Update state or run action]
    F --> G[Notify framework or subscribers]
    G --> H[Render normalized props]
    H --> D
    C --> I[Host unmount or stop]
    I --> J[Run effect cleanup and exit work]

Lifecycle ownership

Framework adapters own the point at which a service becomes active and the point at which it becomes inactive. During activation they invoke the initial state and entry work. During deactivation they run active effect cleanup and machine exit work. Vanilla callers must make these calls directly because no component lifecycle can do it for them.

References: packages/…/machine.ts• L290-317 packages/…/machine.ts• L242-265 packages/…/machine.ts• L238-257 packages/…/machine.svelte.ts• L233-251 packages/…/machine.ts• L310-331

Event and render flow

An event enters through the service’s send function. The adapter resolves the machine transition and either changes state, re-enters the current state, or runs transition actions. A changed state then reaches the framework’s reactive or subscription system, where the component renders the normalized machine props.

References: packages/…/machine.ts• L118-157 packages/…/machine.ts• L267-299 packages/…/machine.ts• L259-295 packages/…/machine.svelte.ts• L253-281 packages/…/machine.ts• L223-263

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the service shape aligned across all six adapters when adding or changing machine-facing values such as state, refs, scope, events, or status.
  • Keep prop normalization rules aligned with the renderer’s event names, DOM property names, SVG case rules, and style format.
  • Keep controlled bindable values paired with their external change path. An adapter that changes only its local value can silently leave controlled UI stale.
  • Keep portal root selection aligned with getRootNode, shadow roots, server rendering, and disabled behavior in every adapter that exposes a portal.
  • Keep mount and stop behavior aligned with effect cleanup and machine exit work. Vanilla callers need an explicit equivalent when lifecycle ownership is not automatic.

Important failure modes

TriggerConsequenceGuard
A controlled bindable value is changed without its external owner applying the changeThe machine reports a new value but the rendered component can continue to show the old valueTreat onChange as the update contract for controlled state and keep the owner wired to it
A machine service is used before its adapter startsEvents can be ignored or delayed, so the first user action may not produce a transitionLet the host lifecycle start the service before interaction, and account for Vue’s pre-mount event buffering when nesting components
A normalized prop is sent to the wrong renderer formEvents, attributes, styles, or DOM properties do not reach the target element correctlyUse the adapter’s normalizeProps and its matching merge or spread helper
A portal uses the wrong document or containerFloating or layered content can mount outside the intended root or fail in a shadow-root setupPass the machine root lookup and use the adapter portal contract for the target framework
A vanilla service is not stoppedActive effects and subscriptions remain after the DOM owner is gonePair every start with stop and remove DOM prop listeners through the returned cleanup

Key files

FileWhy to read it
packages/…/create-machine.tsShows the shared machine creation boundary that all adapters consume.
packages/…/machine.tsShows the complete service contract in a component lifecycle and reactive renderer.
packages/…/machine.tsShows how reactive props and Vue mount hooks bind the same machine model.
packages/…/machine.tsShows the signal-based service lifecycle and update path.
packages/…/machine.svelte.tsShows Svelte reactive state and destroy-time cleanup.
packages/…/machine.tsShows explicit start, stop, subscription, prop updates, and DOM-independent service use.
packages/…/portal.tsxShows the component portal contract and root-container behavior.
packages/…/portal.tsShows the DOM action portal contract for Svelte.
packages/…/spread-props.tsShows how machine props become reconciled DOM attributes, styles, properties, and events.