# FUNSTACK UI

Headless React components, and the first UI library that is **Async React**-native.

> **Beta**
>
> The API is believed complete, but nobody uses it in production yet. Breaking changes can still ship, each with a migration note in the release notes (see [Stability](roadmap.md#stability)). Feedback is welcome on [GitHub](https://github.com/uhyo/funstack-ui/issues).

## Try it

The star flips at once, the button shows a spinner while the fake save runs, and a second click while it is pending wins. The code is one `Toggle` with an async action.

```tsx title="ToggleFavorite.tsx"
"use client";
import { Toggle } from "@funstack/ui/toggle";
import { fakeSave } from "./fake-api.js";

// Uncontrolled Toggle with an async action. The star flips at once (optimistic), the button
// carries data-pending for the 800 ms the save takes, and a second click while pending wins.
export function ToggleFavorite() {
  return (
    <Toggle className="demo-toggle" pressedChangeAction={(next) => fakeSave(`favorite=${next}`)}>
      {({ pressed, pending }) => (
        <>
          {pending ? (
            <span className="demo-spinner" aria-hidden />
          ) : (
            <span aria-hidden>{pressed ? "★" : "☆"}</span>
          )}
          Favorite
        </>
      )}
    </Toggle>
  );
}
```

```css title="toggle.css"
/* Toggle: data attributes carry library state; :hover / :focus-visible come from the platform. */
.demo-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.4em 0.9em;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  background: var(--color-bg);
  color: var(--color-text);
  font: inherit;
  cursor: pointer;
  /* The optimistic flip is a sync commit, so it animates with a plain CSS transition. */
  transition:
    background-color 120ms,
    color 120ms,
    border-color 120ms;
}

.demo-toggle[data-pressed] {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-on-primary);
}

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

.demo-toggle[data-disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

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

@media (prefers-reduced-motion: reduce) {
  .demo-toggle {
    transition: none;
  }
}
```

The components are headless: accessible HTML with data attributes, no styles. You bring the CSS. [Why FUNSTACK UI](why.md) shows what this saves you.

## Principles

- **[Suspense-native](concepts/suspense.md)**

  Content that suspends is the normal case. The old content stays on screen while the new content loads.

- **[Transition by default](concepts/transitions.md)**

  Every state change the library makes is a React transition. You do not opt in.

- **[Action props, not event handlers](concepts/action-props.md)**

  Pass an async function (`pressedChangeAction`, `valueChangeAction`); the component shows the new value at once and `data-pending` until it is done.

- **[\<ViewTransition> built in](concepts/transitions.md#transition-prop)**

  Give a part a `transition` prop and its changes animate with React's `<ViewTransition>`.

- **[Greedy about the platform](concepts/browser-owned-state.md)**

  Popover API, `<dialog>`, CSS Anchor Positioning, `<details>`, native form controls. Where HTML can do the job, it does.

- **Accessibility is not optional**

  Components follow the WAI-ARIA Authoring Practices and are tested for keyboard and assistive-technology behaviour.

## Install

```bash
npm install @funstack/ui
```

React 19.3 or later. Supported browsers: **the latest stable Blink, WebKit and Gecko. No fallbacks.** Platform features not yet in all three are opt-in [experimental props](concepts/experimental.md).

Components: [Button](components/button.md), [Toggle](components/toggle.md), [ToggleGroup](components/toggle-group.md), [Toolbar](components/toolbar.md), [Tabs](components/tabs.md), [Accordion](components/accordion.md), [Collapsible](components/collapsible.md), [NavigationMenu](components/navigation-menu.md), [Tooltip](components/tooltip.md), [HoverCard](components/hover-card.md), [Popover](components/popover.md), [Dialog](components/dialog.md), [AlertDialog](components/alert-dialog.md), [Drawer](components/drawer.md), [Menu](components/menu.md), [Context menu (Menu)](components/context-menu.md), [Menubar (Menu)](components/menubar.md), [Toast](components/toast.md), [Field and Form](components/field.md), [Checkbox](components/checkbox.md), [CheckboxGroup](components/checkbox-group.md), [RadioGroup](components/radio-group.md), [Switch](components/switch.md), [Select](components/select.md), [Combobox](components/combobox.md), [MultiSelect](components/multi-select.md), [Slider](components/slider.md), [NumberField](components/number-field.md), [OTPField](components/otp-field.md), [TextField](components/text-field.md), [Progress](components/progress.md), [Meter](components/meter.md), [Avatar](components/avatar.md), [Separator](components/separator.md) and [Scroll areas (recipe)](components/scroll-area.md). More are coming; see the [roadmap](roadmap.md).

## The FUNSTACK family

FUNSTACK UI is part of the FUNSTACK family, which aims to be the most modern React stack:

- [FUNSTACK Router](https://router.funstack.work): a React router built on the Navigation API.
- [FUNSTACK Static](https://static.funstack.work): a React Server Components static site framework. This site is built with it.

## All pages

### Introduction

- [Why FUNSTACK UI](why.md): Why choose FUNSTACK UI: headless React components built for Async React, which already do the async work for you. Pending and optimistic state, Suspense, transitions and the platform, built in.
- [Overview](index.md): FUNSTACK UI is a headless React component library and the first UI library that is Async React-native: Suspense-native, transition by default, action props, and \<ViewTransition> built in.
- [Getting started](getting-started.md): Install @funstack/ui, import a component from its subpath (and pre-bundle the subpaths under the Vite dev server), and style it with data attributes and CSS.

### Concepts

- [Action props](concepts/action-props.md): FUNSTACK UI components take action props instead of event handlers. Actions run inside the component's transition, may be async, and report errors to Error Boundaries. A change that had already happened is reported to an on\* handler instead.
- [Pending & optimistic state](concepts/pending-and-optimistic.md): A state owner keeps a committed, an optimistic and a pending value. Triggers render the optimistic value, content regions the committed one, and data-pending marks an action in flight.
- [Transitions & ViewTransition](concepts/transitions.md): Every state change in FUNSTACK UI is a transition. Parts whose content is driven by library state take a transition prop that renders a \<ViewTransition>: a class animates the instance's own changes, a type map keyed on the public fsui-\* transition types follows React's rules.
- [Suspense](concepts/suspense.md): FUNSTACK UI content regions keep old content on screen while new content suspends. You own the Suspense boundaries.
- [Browser-owned state](concepts/browser-owned-state.md): Some state belongs to the browser: the popover top layer, \<details open>, dismissing a \<dialog>, a native input before React hears about it. FUNSTACK UI observes it instead of driving it.
- [Server Components](concepts/server-components.md): FUNSTACK UI parts are client modules that Server Components can render directly. Some behaviour works before hydration.

### Guides

- [Replacing the element (render)](concepts/render.md): How the render prop replaces the element a FUNSTACK UI part renders: the element form, the function form, and how the part's props are merged into yours.
- [Direction and locales](concepts/direction-and-locales.md): Right-to-left pages and number formatting in FUNSTACK UI: dir is the only direction switch (no DirectionProvider), which keys mirror, the engine differences, OTPField's ltr default, and the locale prop per Root (no LocaleProvider) with SSR recipes.
- [Experimental features](concepts/experimental.md): Platform features that are not in every engine yet are opt-in through experimental\_\* props on the part that uses them.
- [Next.js App Router](concepts/nextjs.md): Using FUNSTACK UI with the Next.js App Router: the React canary it runs, Server Functions as action props (not in the server render, not for reads, dispatched one at a time, errors redacted in production), a toast and form errors in a dialog, Cache Components, and its string CSP nonce.
- [React Router framework mode](concepts/react-router.md): Using FUNSTACK UI with React Router 8 (and 7) in framework mode (no Server Functions): useTransitions, actions that return a navigate() promise, Form.Root through useFetcher (its submission includes the revalidation), toasting a fetcher result, refetch timing and optimistic lists, what works before hydration, and a CSP nonce from middleware.
- [Content Security Policy](concepts/content-security-policy.md): What a strict style-src needs for FUNSTACK UI: the SHA-256 of the shipped stylesheets (STYLE_SHA256) or a nonce through CSPProvider, and for server-rendered inline styles either style-src-attr or CSPProvider inlineStyles="after-hydration"; and the nonce form React Router, TanStack Start, Next.js and FUNSTACK Static give React.
- [Using with AI agents](concepts/ai-agents.md): Give a coding agent FUNSTACK UI knowledge: install the funstack-ui-knowledge skill from the npm package, or point the agent at the Markdown docs bundled in the package and published as llms.txt.

### Components

- [Button](components/button.md): A button whose click is a transition: action in its own transition, data-pending, whilePending to run or ignore a click while pending, formAction with per-submitter pending.
- [Toggle](components/toggle.md): A two-state button with an async pressedChangeAction, optimistic aria-pressed and data-pending.
- [ToggleGroup](components/toggle-group.md): A set of aria-pressed toggle buttons sharing one value: several pressed, or at most one with exclusive. Optimistic, with pending state.
- [Toolbar](components/toolbar.md): A WAI-ARIA toolbar with one Tab stop and arrow keys over any focusable children: toggles, menu buttons, links, inputs. No wrapper parts.
- [Tabs](components/tabs.md): Tabs with optimistic selection, committed panels that may suspend, automatic or manual activation, an anchor-positioned indicator, and find-in-page support.
- [Accordion](components/accordion.md): An accordion built on \<details>/\<summary>: no JavaScript when you need no state; actions, optimistic data-open and Suspense-friendly panels when you do.
- [Collapsible](components/collapsible.md): One disclosure built on \<details>/\<summary>: no JavaScript when you need no state; actions, optimistic data-open and Suspense-friendly content when you do.
- [NavigationMenu](components/navigation-menu.md): A site's main navigation: a row of links and buttons that open panels of more links, built on the Popover API. It needs no JavaScript; a panel whose content loads on open is a transition.
- [Tooltip](components/tooltip.md): A tooltip built on the Popover API and CSS Anchor Positioning, with a native interestfor path in Chromium. Open state belongs to the browser.
- [HoverCard](components/hover-card.md): A rich preview that appears when the user rests the pointer on a link or focuses it: the open is a transition, so the content loads on interest and the card appears when it is ready; every close is a fact.
- [Popover](components/popover.md): A non-modal popover on the Popover API: JS-free when you need no state; a transition-driven open, library-owned closes with a ViewTransition exit in every engine, and Suspense-ready content when you do.
- [Dialog](components/dialog.md): A dialog built on \<dialog> and Invoker Commands: JS-free when you need no state; a transition-driven open with a pending Trigger, Suspense-ready content, and library-owned closes with a ViewTransition exit in every engine when you do.
- [AlertDialog](components/alert-dialog.md): A Dialog with alert defaults: role alertdialog, always modal, no light dismiss, focus on Cancel. The confirm button runs your async action with its own pending state, closes when it resolves and stays open when it rejects.
- [Drawer](components/drawer.md): A dialog attached to an edge of the viewport that the user can swipe back to it, with mouse, pen or touch. A Dialog underneath: top layer, focus, Escape, the JS-free mode; the swipe is a dismissal reported to onOpenChange, and your CSS moves the drawer through three custom properties.
- [Menu](components/menu.md): A menu button on the Popover API: items whose actions run in their own transition with pending state, optimistic checkbox and radio items, the APG keyboard model with typeahead, and submenus with hover intent.
- [Context menu (Menu)](components/context-menu.md): Menu.ContextTrigger: a region whose right-click, Shift+F10 or touch long-press opens a Menu at the pointer, with the menu's pending items, optimistic checkbox and radio items and submenus unchanged.
- [Menubar (Menu)](components/menubar.md): The application menu of a desktop-style app: a row of ordinary Menu.Roots with one Tab stop, arrow keys between the menus and hover switching while a menu is open.
- [Toast](components/toast.md): Notifications fed by a toast() function and rendered by your own Viewport: an urgent queue that shows while the action it reports on is still pending, promise toasts, an Undo action prop, an onClose handler for the Undo window, ariaNotify announcements, and a region that stays interactive above modal dialogs.
- [Field and Form](components/field.md): Labelled, described, validated form fields on top of native constraint validation and :user-invalid, and a Form that runs its action in a transition, routes server errors to fields and resets only on success.
- [Checkbox](components/checkbox.md): A native \<input type=checkbox> with an async checkedChangeAction, optimistic checked state, a controlled indeterminate state and native form participation.
- [CheckboxGroup](components/checkbox-group.md): A group of native \<input type=checkbox> in a \<fieldset> with a set value, a mixed-state select-all Parent, group-level required, an async valueChangeAction and data-pending.
- [RadioGroup](components/radio-group.md): A radio group of native \<input type=radio> in a \<fieldset>: native keyboard and forms, an async valueChangeAction, optimistic checked state and data-pending.
- [Switch](components/switch.md): An on/off switch rendered as a native \<input type=checkbox role=switch>: optimistic checked state, data-pending while checkedChangeAction runs, native form participation.
- [Select](components/select.md): A real \<select>, progressively enhanced with customizable select (appearance: base-select): native forms, keyboard and pickers, an async valueChangeAction, optimistic selection and data-pending.
- [Combobox](components/combobox.md): An editable combobox with list autocomplete: urgent typing, results from a deferred query inside Suspense, aria-activedescendant, an async valueChangeAction with the Input pending and the listbox closed at once, free text, multiple selection with chips, forms and ariaNotify.
- [MultiSelect](components/multi-select.md): A select-only combobox with a multi-select listbox: a button that shows the selection, options that load on open inside Suspense, a pick that toggles with an optimistic selection and the option pending, the open state as actions and facts, forms and required.
- [Slider](components/slider.md): A slider on a visually hidden native \<input type=range> per thumb: every step a transition, each gesture saved once with rollback, multi-thumb, vertical and RTL.
- [NumberField](components/number-field.md): A text field that holds a number: typed in the user's locale, stepped with keys and press-and-hold buttons, posted as a plain number through a hidden native number input, saved once per gesture with rollback.
- [OTPField](components/otp-field.md): A field for a one-time code: one input with autocomplete one-time-code drawn as a row of boxes, a filter that keeps the text a code, and completeAction, the moment the code is complete as an awaited request with data-pending.
- [TextField](components/text-field.md): A text field that saves itself: one input (or textarea) whose typed text is a draft, committed on blur, Enter (an input) or Ctrl+Enter / Cmd+Enter (a textarea) through valueCommitAction with data-pending, rolled back when the save is refused, and put back by Escape.
- [Progress](components/progress.md): A progress bar on div\[role=progressbar]: the display of someone else's action, correct in every lane, localised aria-valuetext, and a Root that is itself a Server Component.
- [Meter](components/meter.md): A gauge on div\[role=meter] with the platform's low / high / optimum regions, localised aria-valuetext, and a Root that is itself a Server Component.
- [Avatar](components/avatar.md): A picture of a person or a thing with initials or an icon in its place: the image is in the server HTML, the fallback sits under it, the name is on the Root, and React's commit wait for images under a ViewTransition still applies.
- [Separator](components/separator.md): A line between two pieces of content: an hr with aria-orientation and data-orientation, a decorative mode, and a component that is itself a Server Component.
- [Scroll areas (recipe)](components/scroll-area.md): There is no ScrollArea part: a native scroller with scrollbar-width, scrollbar-color and scrollbar-gutter, edge fades from a scroll-driven animation, and a Tab stop for content with nothing focusable. A recipe, run in Chromium, Firefox and WebKit.

### Resources

- [Roadmap & changelog](roadmap.md): Where FUNSTACK UI is heading, and where to follow changes.
