# Browser-owned state

"Transition by default" is about state the library owns. Some state belongs to the browser, which changes it before React hears about it. FUNSTACK UI prefers that state whenever the platform offers it, because it is the only way a component can work without JavaScript.

## Terms

- **Library-owned state**: State held in React and changed only by the library, usually inside the [state owner](pending-and-optimistic.md#state-owner)'s transition after the user acts on a [trigger](pending-and-optimistic.md#trigger). Some library-owned state is urgent instead (typed text, a toast queue): still the library's, but not a transition.
- **Browser-owned state**: State held in the DOM and changed by the browser itself: whether a `<details>` is open, whether a popover is in the top layer (the layer the browser draws above the whole page), whether a dialog is shown. The library reads it; it does not keep a copy in React state.
- **<a id="dismissal"></a>Dismissal**: Closing something without one of its parts: Escape, the Android back gesture, an assistive-technology dismiss, a click outside, or another top-layer element opening (the Popover API calls the last two light dismiss).
- **<a id="invoker"></a>Invoker**: A button that names a popover or dialog by id, so that the browser itself opens or closes it on click, without JavaScript: through `popovertarget` (the Popover API) or through `commandfor` and `command` (Invoker Commands). A `<button>` or an `<input type="button">` / `<input type="reset">` can be one; a disabled button and a submit button inside a form are not. See [Triggers of a popup](#popup-triggers).
- **<a id="foreign-invoker"></a>Foreign invoker**: An invoker of your own that targets a component's popup or dialog, but is not one of the component's parts.
- **<a id="platform-path"></a>Platform path**: A change the browser makes to library-owned state on its own, without a click the library sees: a form reset, find-in-page opening an item, a click before hydration. The component dispatches it like a user change, with `details.reason: "platform"`, and re-applies the committed state afterwards.
- **Request and fact**: A [request](action-props.md#request) is a change the library can still roll back: it goes to an action prop (`openChangeAction`) before it is committed. A [fact](action-props.md#fact) is a change that had already happened when the library heard of it, and that it could not hold: it is reported to an [\`on\*\` handler](action-props.md#on-handler) (`onOpenChange`). Both terms are defined on Action props; see [Requests and facts](#requests-and-facts) for what they mean for a popup.
- **<a id="parts-close"></a>A part's close**: A close the user asks one of the component's parts for (a Close button, the Trigger, a form submit). It is a request: dispatched in a transition, awaited, vetoable and animatable. The opposite of a dismissal.
- **<a id="js-free-mode"></a>JS-free mode and library mode**: Some components can run either way. In **JS-free mode** (no controlled or default state, no change action and no `on*` handler), the browser owns the state and the library adds no behaviour on the native path. In **library mode** (any of those props), the state is library-owned. See [Two modes](#two-modes).

## Two kinds of state

| | Library-owned | Browser-owned |
| - | - | - |
| Who changes it | the library, inside its transition | the browser (hover, light dismiss, Escape, find-in-page, a summary click, an [invoker](#invoker)) |
| Optimistic state | React state ([optimistic](pending-and-optimistic.md#optimistic), `useOptimistic`) | the DOM itself |
| Callback | an action prop for a [request](action-props.md#request), awaited; an `on*` handler for a [fact](action-props.md#fact) (typed text) | an `on*` handler for a fact (`onOpenChange`), not awaited; or none |
| Styling | `data-*` attributes | pseudo-classes: `:popover-open`, `:open` |
| Animation | `transition` prop (`<ViewTransition>`) | CSS: `@starting-style`, `transition-behavior: allow-discrete` |

The library never mirrors browser-owned state into React state. It reads the DOM when it needs to, and it does not duplicate a pseudo-class with a data attribute. State that nobody persists or awaits is left to the browser entirely, with no prop and no action. Whether a hint popup (such as a tooltip) is showing is one example. One read is mirrored: whether a popup is shown, where no native element exposes it to assistive technology: on a Trigger that is no invoker, and on the Combobox input (see [Triggers of a popup](#popup-triggers)). Each component page says what it leaves to the browser in its **Browser-owned state** section.

Example: [Tooltip](../components/tooltip.md)

A tooltip's visibility is not application state. The popup is a `popover` in the top layer, so Tooltip has no `open` prop, no action and no `data-open`: `:popover-open` already says it. Style and animate it in CSS:

```css
.tip { opacity: 0; transition: opacity 120ms, display 120ms allow-discrete, overlay 120ms allow-discrete; }
.tip:popover-open {
  opacity: 1;
  @starting-style { opacity: 0; }
}
/* The trigger of an open tooltip: the popup is its next sibling. */
.trigger:has(+ .tip:popover-open) { outline: 1px solid; }
```

## <a id="two-modes"></a>Two modes, one anatomy

Some components built on a native element that does the whole interaction offer both (their pages have a **Two modes** section). Without any controlled value, default value, change action or `on*` handler it runs in [JS-free mode](#js-free-mode): the browser owns the state and nothing runs when it changes. Add any of them and the component switches to **library mode**: the native interaction is intercepted, the state is React state changed in a transition, and triggers get optimistic data attributes and `data-pending`. The parts and the markup are the same in both. The mode is decided on the first render; switching between them later is not supported (development warning).

The browser can still change the state in library mode (find-in-page, a text fragment, a click before hydration). The component turns that into a regular dispatch with `details.reason === "platform"`, and once the action settles it re-applies the committed state to the DOM, so a controlled parent that refuses the change gets its state back.

Example: [Accordion](../components/accordion.md)

An Accordion without `value`, `defaultValue` and `valueChangeAction` leaves every `<details open>` to the browser, and `exclusive` renders the native `name` group. With any of them, the summary click is intercepted and open state changes in a transition.

```tsx
// JS-free: the browser owns open state.
<Accordion.Root exclusive>...</Accordion.Root>

// Library mode: transitions, actions, optimistic state.
<Accordion.Root exclusive defaultValue={null} valueChangeAction={save}>...</Accordion.Root>
```

## Requests and facts

For something that opens and closes in library mode, a change is either a [request](action-props.md#request) or a [fact](action-props.md#fact), and each has its own prop. Opening, and a close the user asks one of the component's parts for, are requests: the click, submit or command is cancelled and dispatched in a transition, `openChangeAction` is awaited, a controlled parent can veto it, and it can animate with a view transition. A [dismissal](#dismissal) is a fact: the element closes natively and at once, and `onOpenChange(false, details)` is called synchronously from the browser's own event. Nothing is pending, no view transition runs (a CSS exit animates it), and the committed state is `false` in the same event. A close the library performs itself but cannot hold, because the user has already moved on (focus leaving the popup), is a fact too.

The line sits where the platform lets the library cancel a step freely: a part's click (which cancels its command), a `command` event, a form's `submit`. Escape and the back gesture reach the element as an HTML [close request](https://html.spec.whatwg.org/multipage/interaction.html#close-requests) (the platform's term, not a request in the sense above), whose `cancel` event the browser honours only once per user activation, and a user who pressed Escape expects the element to go away now. So the library never holds a dismissal.

A popup the browser opened before hydration is still a request (`openChangeAction(true, { reason: "platform" })`): the library can roll it back by hiding the empty popup, and its load-on-open code runs. So `onOpenChange` is only ever called with `false`.

**Controlled mode needs both props.** Pass the same setter to both: `openChangeAction` adopts a request (in a transition), `onOpenChange` adopts a fact (an [urgent update](action-props.md#urgent-update)). Set state directly in `onOpenChange`, never in `startTransition` and never after an `await`: a parent that still says `open={true}` once the browser's event has been handled gets the element shown again at once, and a late `false` then closes it a second time. A controlled Root without `onOpenChange` warns in development.

A parent that keeps `open={true}` after a dismissal on purpose gets the same re-open. This is not a veto: the user sees the element close and come back, possibly for a frame. A popup that must not be dismissed is set up on the platform side instead, where the component offers it. The re-open never moves focus: a Menu, a Combobox, a Popover (its default `<dialog>` as well as a `<div>`) or a non-modal Dialog shown again this way leaves focus where the dismissal put it (the Trigger after Escape, the field the user clicked, the next field after Tab). So a parent that never adopts `false` cannot trap the keyboard in the popup, and the re-open does not take focus away from the field the user just clicked. A modal Dialog (the default) is the exception: it must hold focus, so its re-open moves focus inside as on any open.

**Uncontrolled, pass what you need.** `openChangeAction` for work an open or a part's close must wait for (loading data on open); `onOpenChange` to observe a dismissal (logging it, remembering it). To hear every close, pass one function to both props and check its first argument: the action is called with `true` for an open too.

Example: [Dialog](../components/dialog.md)

A library-mode Dialog opens through its Trigger and closes through its Close parts and `Dialog.Form` after `openChangeAction` resolves. Escape or a click outside closes it at once and calls `onOpenChange(false, details)` with `reason: "dismiss"`. Controlled, with both props:

```tsx
const [open, setOpen] = useState(false);
<Dialog.Root open={open} openChangeAction={setOpen} onOpenChange={setOpen}>
```

A Dialog without `lightDismiss` ignores a click outside; Escape still closes a modal dialog.

## <a id="one-escape"></a>One Escape closes one layer

A **layer** here is anything an Escape can close: a popup, a dialog, a toast, or the unsaved text of an input that reverts on Escape. Layers nest (a tooltip inside a dialog, a listbox inside a drawer), and one Escape closes only one of them. The browser does this for the layers it owns (a modal dialog, an `auto` popover) through its [close watchers](https://html.spec.whatwg.org/multipage/interaction.html#close-watchers). Where the library handles Escape in JavaScript, it follows the same order:

- An input with an edited draft takes the Escape first: it reverts the text, and nothing else closes.
- Then a tooltip or a hover card that is showing, wherever focus is; with two unrelated ones, the one that opened first.
- Then the innermost layer around the focus. The next Escape reaches the layer outside it.

To keep a layer open on Escape, call `preventDefault()` on the `keydown`. `stopPropagation()` keeps none open: the browser skips the close request only for a cancelled `keydown`, and the library reads the same signal. An Escape pressed during an IME composition only ends the composition.

Example: [Tooltip](../components/tooltip.md)

A Tooltip shown inside an open Dialog: the first Escape hides the tooltip and the dialog stays open; the second Escape closes the dialog.

## <a id="popup-triggers"></a>Triggers of a popup

A component whose popup is a popover has a **Trigger**: the part that opens and closes it. A Trigger rendered as a `<button>` names the popup with `popovertarget`, which makes it the popup's [invoker](#invoker). That relationship gives it three things from the browser:

- It can open the popup before hydration and with JavaScript off.
- A press on it is not a click outside: light dismiss spares the invoker of an open popover, so the Trigger's click can close the popup as [a part's close](#parts-close), awaited and animated.
- The browser exposes the popover's expanded or collapsed state on it. The Trigger therefore renders no `aria-expanded`; [ARIA in HTML](https://w3c.github.io/html-aria/) forbids the attribute on an element with `popovertarget`.

A component may give up one of these on purpose; its page says so.

Only a `<button>` can be the Trigger's invoker. A Trigger rendered as a link or another element works through the library's click handler instead, and has none of the above: it renders `aria-expanded` itself, from whether the popup is shown; and a press on it while the popup is open counts as a click outside, so light dismiss closes the popup before the click arrives. That close is a [fact](action-props.md#fact), reported to `onOpenChange` (with `reason: "activate"` and the click), not a request: it is not awaited, a controlled parent cannot veto it, and there is no view transition. A development warning says so. A disabled Trigger that stays focusable (it renders `aria-disabled`) is no invoker either, so that nothing native can open the popup.

**Two kinds of open state on a Trigger.** What assistive technology hears is whether the popup is shown. In library mode, `data-open` is the [optimistic](pending-and-optimistic.md#optimistic) state: it flips at the click, while an open that waits for an action or for content is still pending. Style click feedback with `[data-open]` and `[data-pending]`, never with `[aria-expanded]`, which a `<button>` Trigger does not have. An element that controls a popup without being its invoker, such as the Combobox input, renders `aria-expanded` from the shown popup too.

**One Trigger per Root.** The library anchors the popup to its Trigger or returns focus to it, so a Root has one. A second Trigger in the same Root is not supported (development warning); use a Root per trigger.

**Foreign invokers.** A [foreign invoker](#foreign-invoker) still works in library mode. An open through it is routed through the library like the Trigger's, so the action runs and the content loads first. A close through it has `reason: "activate"`, because the user acted on a button. It is a request where the component routes it through the library (it cancels the invoker's step and closes after `openChangeAction`), and a [fact](action-props.md#fact) reported to `onOpenChange` where the browser performs it first; each component page says which. A component that keeps focus in its popup or its input does not lose it when you press a foreign invoker: the invoker's own close is the only one.

Example: [Popover](../components/popover.md)

A `Popover.Trigger` renders `<button popovertarget>` with no `aria-expanded`. Its styling hook is `data-open`; on a page that never hydrates, select on the popup that follows it:

```css
.trigger[data-open] { color: var(--accent); }
/* Without JavaScript: the Popup right after the Trigger. */
.trigger:has(+ :popover-open) { color: var(--accent); }
```

## <a id="native-inputs"></a>Native inputs: the DOM is the optimistic state

A form control built on a real `<input>` has library-owned state, but the browser flips the input before any JavaScript runs, and after every event react-dom restores a controlled input to its current props. Only the synchronous optimistic state has rendered by then, so such a control renders the input from the optimistic value. That is a **correctness requirement**: an input driven by committed state would snap back right after the click and stay wrong for as long as the action runs. As a consequence the native pseudo-class (`:checked`) and the data attribute agree, and assistive technology hears the new state at once.

Example: [Switch](../components/switch.md)

A Switch renders `<input type="checkbox" role="switch">` with `checked={optimistic}`, so `:checked` and `data-checked` agree from the click on.

The browser can still change a native input without a click the library sees. Each case is a regular dispatch with `details.reason === "platform"`, and afterwards the DOM follows the decision:

- **Form reset**: a reset button, `form.reset()`, or React's automatic reset after a successful `<form action>`. Uncontrolled, the target is the latest default value; controlled, the browser's mount-time value is offered to the parent, which adopts it or not.
- **Mount reconciliation**: a click before hydration, or Firefox restoring form state on reload. React keeps the DOM value when hydrating and fires nothing, so the component compares on mount and dispatches once (once per group for a group of inputs).

## Consumer-owned, mirrored state

A fourth kind of state is **consumer-owned and mirrored**: the parent derives it, there is no default and no action, and the library only writes it to the DOM after every commit. Where the browser clears it on a click, the library stops writing it while an action is pending.

Example: [Checkbox](../components/checkbox.md)

`indeterminate` is derived by a "select all" parent from its rows. The library writes the DOM property after every commit and suspends it while an action is pending, because the browser clears it on the click.
