Public API foundations
Zag's public APIs rely on a small set of shared contracts. Type definitions describe machine inputs and outputs, reactive stores carry state changes, anatomy metadata gives parts stable names, and package entry points expose these features in a consistent way. Together, these foundations let the same component behavior serve framework adapters and application code.
Public API model
Contract layers
The public API is layered around a framework-neutral machine. A component package publishes its machine, its connection API, its anatomy, its props, and its named types. Framework packages then adapt the resulting service and prop data to a rendering system rather than changing the component contract. See Machine definitions and interaction contracts and Framework integrations for the machine and adapter views of this model.
The main relationships are:
- Shared types define common values such as direction, orientation, DOM props, events, geometry, and machine services.
- The store provides reactive state, snapshots, subscriptions, and computed values for machine and adapter use.
- Anatomy metadata names component parts and produces selectors and data attributes for styling and DOM composition.
- Component exports gather the machine-facing API into one package entry point.
- Framework exports provide the adapter-facing API for a specific rendering system.
This diagram shows how a component package's public contract moves from shared foundations to a framework consumer.
flowchart LR
Types[Shared types] --> Machine[Component machine]
Store[Reactive store] --> Machine
Anatomy[Anatomy metadata] --> Component[Component package API]
Machine --> Component
Component --> Adapter[Framework adapter]
Adapter --> Consumer[Application consumer]
References: packages/…/types.ts• L97-118 packages/…/index.ts• L1-14 packages/…/index.ts• L1-5
Shared contracts
Type vocabulary
The @zag-js/types package is the common type vocabulary for component machines and framework adapters. It keeps values that cross package boundaries consistent, while its DOM and JSX types let adapters describe normalized HTML, SVG, ARIA, style, and event props without requiring a framework-specific type package. The package entry point re-exports these contracts for consumers.
The most important contract groups are:
- Environment values: direction, orientation, locale, machine identity, and root-node lookup.
- Interaction values: keyboard event keys, event maps, points, sizes, and rectangles.
- DOM props: element prop categories, intrinsic HTML and SVG attributes, ARIA attributes, data attributes, and style values.
- Machine support values: required-key helpers, nullable values, and values that can be supplied directly or through a function.
Component packages add their own props, callback details, machine schema, service type, and connected API on top of these shared types. For example, a component can require common identity and direction properties while exposing its own controlled value and event details. The more detailed relationship between these contracts is covered in Stores and shared type contracts.
References: packages/…/index.ts• L3-99 packages/…/prop-types.ts• L45-79 packages/…/jsx.ts• L17-36 packages/…/accordion.types.ts• L1-3 packages/…/accordion.types.ts• L27-90
Prop contracts
Component props have two related roles. The public props type describes what an application may configure or control, while the prop list defines which values belong to that component when props are split, normalized, or passed to a machine. This keeps the machine contract separate from unrelated DOM or parent-component values.
The prop list is checked against the public props type. A missing key is therefore a type-level contract error rather than a silent omission. Component packages also use the same pattern for local part props, such as the value and disabled state of an accordion item.
References: packages/…/create-props.ts• L1-10 packages/…/accordion.props.ts• L1-22 packages/…/accordion.types.ts• L27-68
Reactive state
Store boundary
The @zag-js/store package supplies the reactive state boundary used by the library. A proxy wraps an object, nested changes can be observed, and a snapshot gives consumers a stable read view. Subscriptions report changes and return a removal function, so framework adapters can connect updates to their own render lifecycles. See Stores and shared type contracts for the store behavior and type contracts in detail.
The store also supports two important boundaries:
- Refs mark values that should remain outside reactive proxying. This is useful for objects such as DOM or framework values.
- Computed values expose derived properties from a snapshot and can optionally map writes back to the underlying state.
The store keeps its shared proxy state and ref set available across the runtime global. This allows separately loaded package copies to recognize the same reactive objects instead of creating isolated store domains.
References: packages/…/index.ts• L1-4 packages/…/proxy.ts• L17-31 packages/…/proxy.ts• L219-278 packages/…/proxy-computed.ts• L3-32 packages/…/global.ts• L8-15
Service contract
The core service contract is the bridge between a running machine and its consumers. It exposes status, state, context, events, computed values, refs, props, scope, and event sending through one typed surface. Framework adapters use this surface to observe machine changes and provide them to components, while component APIs use it to expose behavior in a more focused form.
This boundary keeps machine state separate from rendering. A machine can manage transitions and context once, then each adapter can decide how to subscribe, render, and handle framework refs. The service and machine types are defined in @zag-js/core; the store supports the reactive portions of that exchange.
References: packages/…/types.ts• L196-242 packages/…/types.ts• L264-293 packages/…/package.json• L27-31
Anatomy metadata
Part identity
An anatomy is the named-part contract for a component. It gives each structural part a stable name and builds matching selectors and data-scope and data-part attributes. Styling and DOM composition can then target a part without depending on the framework adapter or on a private DOM shape.
Component packages define their anatomy near their machine contract and export it from the package entry point. The anatomy can also extend, rename, or omit part names when a related component needs a controlled variation. Read Anatomy and icon assets for the full anatomy and asset model.
References: packages/…/create-anatomy.ts• L1-17 packages/…/create-anatomy.ts• L19-45 packages/…/index.ts• L1-2 packages/…/accordion.anatomy.ts• L1-4
Asset catalog
The @zag-js/anatomy-icons package turns component part names into a catalog of visual assets. It exposes a lookup map and a typed component-name key so a consumer can select an icon set by a component name. Each icon receives an accent color and derives a palette for its SVG presentation; the package also exposes matching gradient helpers.
The asset catalog follows the same names used by component anatomies, but it is a visual support layer rather than the interaction contract. The package is React-based and declares React as a peer dependency. This distinction matters when a framework integration consumes anatomy metadata but does not consume the icon components.
References: packages/…/index.ts• L1-4 packages/…/index.ts• L46-98 packages/…/create-component.tsx• L4-15 packages/…/create-palette.ts• L5-24 packages/…/package.json• L43-50
Package exports
Entry-point conventions
Each public package uses src/index.ts as its package-level entry point in the repository and publishes its built dist files. The entry point is the contract boundary: it re-exports supported functions and types while leaving supporting files private to the package. Consumers should import from the package name instead of reaching into internal paths.
The export shape follows the package role:
| Package role | Public surface |
|---|---|
| Shared types | Common values, DOM prop types, and type helpers |
| Store | Proxy state, snapshots, subscriptions, refs, cloning, and computed values |
| Anatomy | Anatomy creation and anatomy types |
| Component machine | Anatomy, machine, connect function, props, and component-specific types |
| Framework adapter | Machine binding, prop normalization, portals, and framework reactivity helpers |
| Anatomy icons | Component icon catalog, palette helpers, and gradient helpers |
Package metadata marks these libraries as public and side-effect free. Workspace dependencies connect the packages during development, while the package build and pack steps define what downstream consumers receive.
References: packages/…/package.json• L14-35 packages/…/package.json• L16-40 packages/…/package.json• L17-35 packages/…/package.json• L18-46 packages/…/package.json• L18-31 packages/…/package.json• L17-37
Component export shape
A component package normally presents one coherent public surface from its entry point. The exported anatomy supports styling, the machine defines behavior, the connect function turns a service into DOM-facing props and state, and the named types describe the inputs and outputs. This shape gives framework adapters a stable target and gives application code a predictable place to import component APIs.
The export shape is visible in the accordion package: its entry point exports the anatomy, connect function, machine, props, and aliases for the public API, machine, props, and service types. Other component packages follow this convention even when their own behavior and part names differ.
References: packages/…/index.ts• L1-14 packages/…/accordion.types.ts• L122-145 packages/…/accordion.anatomy.ts• L1-4
Change checklist
When these files or sections are changed, remember to consider these:
- When a shared prop or event type changes, check component schemas, adapter-facing normalized props, and any public type aliases that re-export it.
- When a component part is added, renamed, or removed, update its anatomy names, generated selectors and attributes, connect methods, and any matching icon catalog entry.
- When a store snapshot or subscription contract changes, check every framework adapter that maps store updates to rendering and lifecycle cleanup.
- When a package entry point changes, check the package metadata, built
distoutput, and downstream imports that rely on the package-level path. - When a component export changes, check related documentation links and any generated package or type checks that expect the standard entry-point shape.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A prop is added to a public type but not to the checked prop list | Prop splitting or machine setup can silently drop the new value | createProps requires all keys from the declared type; keep the list and type together |
| A component part name changes without changing its anatomy consumers | Selectors and data-part attributes no longer match styles or composed DOM | Treat anatomy names as a stable contract and update the component API and asset catalog together |
| A non-proxy object is placed in reactive state without a ref boundary | The store can proxy a value that belongs to a DOM or framework runtime | Mark external objects with the store's ref contract |
| A consumer imports a private source path | Internal file moves can break the consumer even when the package API is unchanged | Import from the package entry point |
| A subscription is not removed when an adapter lifecycle ends | Stale listeners can continue to receive state changes | Use the unsubscribe function returned by the store subscription |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.ts | Defines the shared values and type exports used across packages. |
| packages/…/prop-types.ts | Defines normalized element, data, ARIA, and style prop contracts. |
| packages/…/proxy.ts | Shows the public reactive state, snapshot, and subscription boundary. |
| packages/…/proxy-computed.ts | Shows how derived store values become part of the state contract. |
| packages/…/types.ts | Defines the machine, service, context, event, and state contracts. |
| packages/…/create-anatomy.ts | Defines stable part names, selectors, and data attributes. |
| packages/…/index.ts | Defines the component-to-icon catalog and its typed lookup surface. |
| packages/…/index.ts | Illustrates the standard component package export shape. |
| packages/…/index.ts | Illustrates a framework adapter package entry point. |