FUNSTACK UI

HomeWhyGetting startedConceptsComponentsRoadmap
GitHub

FUNSTACK UI is in beta. Part of the FUNSTACK family with FUNSTACK Router and FUNSTACK Static, which build this site.

Introduction

  • Why FUNSTACK UI
  • Overview
  • Getting started

Concepts

  • Action props
  • Pending & optimistic state
  • Transitions & ViewTransition
  • Suspense
  • Browser-owned state
  • Server Components

Guides

  • Replacing the element (render)
  • Direction and locales
  • Experimental features
  • Next.js App Router
  • React Router framework mode
  • Content Security Policy
  • Using with AI agents

Components

  • Buttons
    • Button
    • Toggle
    • ToggleGroup
    • Toolbar
  • Disclosure
    • Tabs
    • Accordion
    • Collapsible
    • NavigationMenu
  • Overlays
    • Tooltip
    • HoverCard
    • Popover
    • Dialog
    • AlertDialog
    • Drawer
    • Menu
    • Context menu (Menu)
    • Menubar (Menu)
    • Toast
  • Forms
    • Field and Form
    • Checkbox
    • CheckboxGroup
    • RadioGroup
    • Switch
    • Select
    • Combobox
    • MultiSelect
    • Slider
    • NumberField
    • OTPField
    • TextField
  • Output
    • Progress
    • Meter
    • Avatar
  • Layout
    • Separator
    • Scroll areas (recipe)

Resources

  • Roadmap & changelog
  • GitHub ↗

Pending & optimistic state

Every component that owns state keeps three values for it. Knowing which part of the component shows which value is the key to styling FUNSTACK UI.

Terms

These words are used throughout the documentation. A part is one element-rendering piece of a component, such as Tabs.Tab.

State owner
The part that holds a piece of state, runs its action in a transition and decides when the state is committed. For a compound component it is usually the Root; a single-element component (a Toggle, a Switch) owns its own state.
Trigger
A part the user acts on to change the state: a button, a tab, a summary, a checkbox. Some components have a part literally named Trigger; the concept is wider than that name.
Pure trigger
A trigger whose action changes no state of its own: it runs a task (save, close, confirm). See A second click while pending.
Content region (panel)
A part that shows the content belonging to a state rather than letting the user change it: the content of the selected tab, of an open accordion item, of an open dialog. The docs call it a panel when the part is named Panel.
Committed value
The value the application has accepted: the controlled prop, or, uncontrolled, the internal state written after the action succeeded (at once, when there is no action prop). Content regions read it through useDeferredValue, so new content that suspends keeps the old content on screen whether the change was a transition or an urgent write.
Optimistic value
The value the user asked for, shown before the action has finished. It is React's useOptimistic over the committed value, set at the start of the action.
Pending
True while an action dispatched through a component is in flight, from the user's interaction until the render that ends it has committed (including a Suspense wait). It is the pending flag of the transition that runs the action: the state owner's, or a pure trigger's own. On a state owner it is also true while its content regions have not caught up with a change made without an action (the deferred render is running, or the new content suspends). Field and Form read the form channel (useFormStatus) instead.
Rollback
The optimistic value falling back to the committed one when the action ends without the new value being committed (it rejected, or a controlled parent did not adopt it).

Triggers are optimistic, content is committed

Triggers render the optimistic value, so the user sees their click land at once. Content regions render the committed value. This rule follows from how React works, not from taste. An optimistic update renders synchronously, and a synchronous render that suspends shows the nearest Suspense fallback. If a content region followed the optimistic value, choosing content that loads data would flash a fallback. Because content regions follow the committed value, the old content stays on screen while the new one loads, the trigger already shows the new choice, and the state owner and the trigger are marked pending. When the data is ready, the content swaps in one commit.

Example: Tabs

In Tabs, Tabs.Tab and Tabs.Indicator are triggers and show the optimistic selection (aria-selected, data-selected); Tabs.Panel is the content region and shows the committed selection; Tabs.Root is the state owner. In the tabs below, switching waits for an 800 ms save. The indicator and the tab colour move at once (optimistic); the panel changes when the save resolves (committed). Every third save fails and everything rolls back.

The profile settings.

Show code
TabsSaved.tsx
"use client";
import { Tabs } from "@funstack/ui/tabs";
import { DemoErrorBoundary } from "./DemoErrorBoundary.js";
import { fakeSave } from "./fake-api.js";

let saves = 0;

// Manual activation with an awaited save: arrow keys only move focus, Enter / Space / click
// select. The panel switches when the save resolves; every third save rejects, the selection
// rolls back and the error reaches the Error Boundary. Panels slide by DOM direction through
// the library's fsui-tabs-next / fsui-tabs-prev transition types.
export function TabsSaved() {
  return (
    <DemoErrorBoundary>
      <Tabs.Root
        className="demo-tabs"
        defaultValue="profile"
        activationMode="manual"
        valueChangeAction={async (next) => {
          await fakeSave(`tab "${next}"`, { fail: ++saves % 3 === 0 });
        }}
      >
        <Tabs.List className="demo-tabs-list" aria-label="Account settings">
          <Tabs.Tab value="profile" className="demo-tab">
            Profile
          </Tabs.Tab>
          <Tabs.Tab value="billing" className="demo-tab">
            Billing
          </Tabs.Tab>
          <Tabs.Tab value="danger" className="demo-tab" disabled>
            Danger zone
          </Tabs.Tab>
          <Tabs.Tab value="team" className="demo-tab">
            Team
          </Tabs.Tab>
          <Tabs.Indicator className="demo-tabs-indicator" />
        </Tabs.List>
        {["profile", "billing", "danger", "team"].map((value) => (
          <Tabs.Panel
            key={value}
            value={value}
            className="demo-tab-panel"
            transition={{
              "fsui-tabs-next": "demo-slide-next",
              "fsui-tabs-prev": "demo-slide-prev",
              default: "none",
            }}
          >
            <p>The {value} settings.</p>
          </Tabs.Panel>
        ))}
      </Tabs.Root>
    </DemoErrorBoundary>
  );
}
tabs.css
/* Tabs: optimistic selection on tabs and the indicator, committed selection on panels. */
.demo-tabs-list {
  /* The Indicator is absolutely positioned against the Tabs (its anchors) inside the list. */
  position: relative;
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--color-border);
}

.demo-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.5em 0.9em;
  border: none;
  background: none;
  color: var(--color-text-secondary);
  font: inherit;
  cursor: pointer;
  transition: color 120ms;
}

.demo-tab[data-selected] {
  color: var(--color-primary);
}

.demo-tab[data-pending] {
  cursor: progress;
}

.demo-tab[aria-disabled="true"] {
  opacity: 0.4;
  cursor: not-allowed;
}

.demo-tab:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: -2px;
}

/* CSS Anchor Positioning places the indicator under the optimistic tab; a CSS transition
   slides it. */
.demo-tabs-indicator {
  position: absolute;
  bottom: -1px;
  left: anchor(left);
  width: anchor-size(width);
  height: 3px;
  border-radius: 2px;
  background: var(--color-primary);
  transition:
    left 200ms ease-out,
    width 200ms ease-out;
}

.demo-tab-panel {
  padding: 12px 4px 0;
}

.demo-tab-panel :is(p, ul) {
  margin: 0 0 12px;
}

/* The old panel stays while the next one loads; dim it after a short delay. */
.demo-tabs[data-pending] .demo-tab-panel {
  opacity: 0.5;
  transition: opacity 150ms 150ms;
}

/* ViewTransition classes used by the `transition` prop on Tabs.Panel. */
::view-transition-old(.demo-slide-next) {
  animation: 180ms ease-in both demo-out-left;
}
::view-transition-new(.demo-slide-next) {
  animation: 180ms ease-out both demo-in-right;
}
::view-transition-old(.demo-slide-prev) {
  animation: 180ms ease-in both demo-out-right;
}
::view-transition-new(.demo-slide-prev) {
  animation: 180ms ease-out both demo-in-left;
}

@keyframes demo-out-left {
  to {
    opacity: 0;
    translate: -2rem 0;
  }
}
@keyframes demo-in-right {
  from {
    opacity: 0;
    translate: 2rem 0;
  }
}
@keyframes demo-out-right {
  to {
    opacity: 0;
    translate: 2rem 0;
  }
}
@keyframes demo-in-left {
  from {
    opacity: 0;
    translate: -2rem 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none !important;
  }
  .demo-tabs-indicator,
  .demo-tab {
    transition: none;
  }
}

Two consequences for triggers:

  • Content rendered from a trigger's state (its children function) must not suspend, or must have its own Suspense boundary.
  • The optimistic flip is a synchronous commit and never starts a view transition. Animate trigger visuals with a CSS transition on the data attribute (see What animates when).

Each component page lists which of its parts show which value in its Optimistic and pending section and its data attributes table.

Where data-pending lands

  • On the state owner while its transition is pending, or while its content regions have not caught up with a change made without an action prop (new content that suspends). It does not show an action that is pending elsewhere in the app for such a change.
  • On the trigger the user is waiting on: the one whose optimistic value differs from the committed one, or that started the action. Decorative parts that follow the trigger (an indicator) may mirror it.
  • On a part that asked its owner to close (a Dialog.Close, Popover.Close, Drawer.Close, an AlertDialog Cancel or Action, a closing Menu.Item): from the click until that close has committed, with or without an action of its own. The Trigger may be out of sight under a modal backdrop. Without an action the part is never aria-disabled: nothing of its own is ignored.
  • Never on content regions. A content region shows the committed value or nothing; it is never "in between". To dim the old content while the next one loads, select through the state owner.
  • Never for a fact. A fact, a change that had already happened when the library heard of it (a popup dismissed with Escape or a click outside), is reported to an on* handler and is not awaited, so nothing is pending for it: the popup, data-open and the committed state all change at once (Requests and facts).

The same names exist in the function props (className, style, children as a function of state): state.pending, and the state's own name such as state.pressed, state.selected or state.open.

Example: Tabs

data-pending is on Tabs.Root and on the Tabs.Tab that is the optimistic selection; Tabs.Indicator mirrors the Root. The panels never get it, so the CSS selects through the Root:

.tabs[data-pending] .panel { opacity: 0.6; transition: opacity 150ms 150ms; }
.tab[data-pending]::after { content: ""; /* spinner */ }
<Tabs.Tab value="billing">
  {({ pending }) => <>Billing{pending && <Spinner />}</>}
</Tabs.Tab>

A second click while pending

A trigger that changes state takes every click: a second click on a pending trigger is a new intent, the optimistic value follows it, and the latest intent wins (see Overlapping actions). A pure trigger has no value to protect, so what a second click, Enter or Space does while its own action is pending is its whilePending prop:

  • "run": the activation runs the action again, in the same transition. The part shows data-pending until every run has settled, and is not aria-disabled: it still acts.
  • "ignore": the activation starts nothing and its default action (a link's navigation, a form submission, a command) is cancelled. The part renders aria-disabled="true" for as long as it shows data-pending, so a screen-reader user who presses again hears that it is unavailable.

The default follows who owns the action. Where the action is yours alone (a Button), every click runs, as with React's own useTransition: only you know whether two overlapping runs are a bug (Save) or the point (a counter). Where the library acts after your action (a Dialog.Close or Popover.Close closes its overlay, a Menu.Item closes its menu, a Toast.Action closes its toast), a second activation is ignored: the first one already ends in that close. Switch the library's close off (closeOnActivate={false}) and the item or action becomes yours, so it runs every activation again. Either way the prop overrides the default, and no part is ever natively disabled, so focus stays where it is. A closing part in "run" mode closes once, when its last run has settled, and only if the latest run resolved: a rejection leaves the overlay, menu or toast open, as it does in "ignore" mode.

Example: Button

A counter wants every click; a Save must not run twice. Opt the Save in to "ignore":

<Button action={async () => { await incrementOnServer(); }}>+1</Button>  // every click runs
<Button action={save} whilePending="ignore">Save</Button>                // one save at a time

In "run" mode the runs overlap, and their errors follow the usual rule: the latest run's rejection reaches the Error Boundary, an older run's goes to reportError. For runs that must happen one after another, each seeing the previous result, use useActionState and call its dispatch from the action.

Rollback

If the action rejects, or a controlled parent does not adopt the new value, the optimistic value falls back to the committed one when the transition ends. Nothing has to be undone by hand: uncontrolled state is only written after success.

Assistive technology

ARIA state on a trigger (aria-pressed, aria-selected, a native input's checked state) follows the optimistic value, so screen reader users get the same immediate feedback as sighted users. Where HTML gives the state to a native element that cannot carry the ARIA attribute, assistive technology hears the committed state and the optimistic data attribute is visual only; the component page says so when that applies (for example Accordion). The same holds for the trigger of a popup and for the Combobox input: assistive technology hears whether the popup is shown, and data-open is the optimistic state (see Triggers of a popup).

Testing pending state

A trigger whose label depends on pending changes its accessible name while the action runs. A query by role and name then stops matching halfway through the test: the button named "Close" is named "Closing…" while it is pending, so an assertion on its data-pending waits for an element the query no longer finds. Playwright locators run their query again on every use, so they hit this every time. A Testing Library query returns the element itself, so an element you queried before the click stays usable.

Two ways to keep the name stable:

  • Keep the pending indicator out of the name: a label that does not change, plus an aria-hidden spinner while pending.
  • Name the trigger with an aria-label that does not change, and match that name in the test.

For example, the second way on a Popover's Close button, tested with Playwright:

// The name stays "Close" while the label reads "Closing…".
<Popover.Close aria-label="Close" action={save}>
  {({ pending }) => (pending ? "Closing…" : "Close")}
</Popover.Close>

const close = page.getByRole("button", { name: "Close", exact: true });
await close.click();
await expect(close).toHaveAttribute("data-pending");