On this page

Touch, the agent and your code reach a component through the same actions. A controller exposes those actions to your code; a value is the state those actions change. This page covers both, using the types in nitro-ui-sdk/core.

Controllers

Every primitive has createApi(). It returns a Controller<Actions> whose current holds the actions while a component is attached to it, and undefined otherwise.

import { useState } from "react";
import { Slider, sliderPrimitive } from "nitro-ui-sdk/slider";

export function Volume() {
  const [volume] = useState(sliderPrimitive.createApi); // one controller per mounted instance

  return (
    <>
      <Slider.Root api={volume} label="Volume" defaultValue={40} min={0} max={100} />
      {/* Elsewhere: volume.current?.increment(); volume.current?.setValue(80); */}
    </>
  );
}
  • Create the controller once (useState(primitive.createApi)) and pass it as api on roots that support a controller and call api.attach(actions). Static or display-only roots (such as a non-pressable Card.Root) do not accept api. When supported, the root attaches its actions in a layout effect and detaches on unmount, so current is ready by the time your effects run.
  • Use one controller per mounted instance. When a second component attaches to the same controller, the controller follows the latest one, and the first one’s detach no longer clears it.
  • Actions called through the controller default to the "api" source and never play haptics.

Action results

Actions report expected outcomes as status values rather than throwing for disabled state or refusals:

Status Meaning
accepted The action ran, or the value already was the requested one (including in controlled mode).
disabled The control is disabled (or busy); nothing ran.
refused A handler called details.preventDefault().
unavailable There is nothing to run, such as a press without an onPress.
requested A controlled value change to a different value was reported to its owner, who has not rendered the new value yet.

Note that activate and createValue.change do not catch synchronous errors thrown inside your onPress or onValueChange handler — those propagate to the caller. When the same action runs through registry.act, the agent registry catches thrown errors and maps them to "failed", alongside its own failures such as stale-ref; see Agent API.

Uncontrolled and controlled values

Value families (Switch, Checkbox, Slider, Stepper, RadioGroup, SegmentedControl, Select, DatePicker, Tabs, the text editors) accept either a defaultValue or a value:

// Uncontrolled: the component keeps the value.
<Switch.Root label="Wi-Fi" defaultValue onValueChange={(on) => save(on)} />

// Controlled: you keep the value and render the change.
const [on, setOn] = useState(true);
<Switch.Root label="Wi-Fi" value={on} onValueChange={(next) => setOn(next)} />

onValueChange(value, details) runs before the change is applied, for every source. In controlled mode, nothing changes until you render the new value: if you do not, the control stays where it was.

details.source and preventDefault

The second argument is an ActionDetails:

interface ActionDetails {
  readonly source: "user" | "agent" | "api";
  readonly defaultPrevented: boolean;
  preventDefault(): void;
}

source tells you who asked. preventDefault() declines the change: an uncontrolled value is not stored, and the action returns refused.

<Switch.Root
  label="Share location"
  value={sharing}
  onValueChange={(next, details) => {
    // A person may turn sharing on; an agent may only turn it off.
    if (next && details.source === "agent") return details.preventDefault();
    setSharing(next);
  }}
/>

requested: a controlled change in flight

In controlled mode, requesting the value that is already current (Object.is(next, get())) returns "accepted" immediately without calling onValueChange. Requesting a different value cannot finish inside the action call because the new value only exists after the owner renders, so a controller action on a controlled component returns "requested" (unless onValueChange synchronously updates what get() reads).

const status = wifi.current?.toggle(); // "requested" while you hold `value`

Settling a controlled change for agents is each family’s responsibility — the agent registry itself passes through whatever status an action returns (including "requested" if a custom action returns it directly). Built-in value families settle controlled requests with useSettleChange() from src/core/use-settle-change.ts (or the native value hook): useSettleChange() returns a function (status: ChangeStatus, requested: Value, read: () => Value) => Promise<ActionStatus> that waits for the next render and resolves to "accepted" if Object.is(read(), requested), "refused" if the owner kept the old value, or "unavailable" if the control unmounted.

One commit per interaction

Ranges report every intermediate value through onValueChange while a drag is in progress, and the final value once through onValueCommit when it ends. Use the commit for work that should not run per frame, such as saving or sending.

<Slider.Root
  label="Brightness"
  defaultValue={0.6}
  min={0}
  max={1}
  onValueChange={(value) => preview(value)}
  onValueCommit={(value) => persist(value)}
/>

No commit is reported when the value ends where it started, or when the control is disabled.

Building a family: the primitives behind it

nitro-ui-sdk/core exposes the pieces each family is made of:

Export Purpose
definePrimitive<State, Actions>()({ parts }) Declares a family: its part names and createApi.
createController<Actions>() The attachable controller behind createApi.
createValue(options, fallback) Controlled and uncontrolled value logic: get, change(next, source), begin(source).commit(). It reads the latest options on every call, so a mounted controller never sees stale props.
activate(disabled, source, handler) Runs a press-like handler and maps the result to a status.
actionDetails(source) Creates the details object.
useStableCallback(fn) A callback with a stable identity that always runs the latest props.
import { createValue } from "nitro-ui-sdk/core";

const value = createValue(() => props, false);
value.change(true, "user"); // "accepted" | "disabled" | "refused" | "requested"
const drag = value.begin("user");
// ...several value.change(..., "user") calls...
drag.commit(); // one onValueCommit if the value moved