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 ↗

Suspense

Suspending children are the normal case, not an edge case. Content that reads data with use() works inside any FUNSTACK UI content region without extra props.

You own the boundaries

The library never wraps children in <Suspense> and has no fallback props. Where a fallback appears is a layout decision, so it is yours. Put a boundary wherever you would put one without the library, for example above the whole component.

What happens when new content suspends

A change of state is a transition, and content regions follow the committed value. When the content for the new value suspends, React keeps the current content on screen instead of showing the fallback, because during a transition React does not hide content that is already visible behind a fallback. Meanwhile the trigger already shows the new value (optimistic), and the state owner and the trigger carry data-pending. When the data arrives, the new content replaces the old in one commit. If the user changes their mind while waiting, only the latest choice is ever committed.

Example: Tabs

Selecting a tab whose panel suspends keeps the current panel on screen; the new tab is already selected and the Root and that tab carry data-pending. Pressing arrow keys quickly with automatic activation dispatches one selection per key; the latest one wins, and only its panel is committed.

Loading orders…

Show code
TabsSuspending.tsx
"use client";
import { Suspense, use, useState } from "react";
import { Tabs } from "@funstack/ui/tabs";
import { sleep } from "./fake-api.js";

// Cache the promises so `use()` sees the same one on every render.
const cache = new Map<string, Promise<string[]>>();
function loadOrders(status: string): Promise<string[]> {
  let promise = cache.get(status);
  if (promise === undefined) {
    promise = sleep(1200).then(() => [1, 2, 3].map((n) => `${status} order #${n}0${n}`));
    cache.set(status, promise);
  }
  return promise;
}

function Orders({ status }: { status: string }) {
  const orders = use(loadOrders(status));
  return (
    <ul>
      {orders.map((order) => (
        <li key={order}>{order}</li>
      ))}
    </ul>
  );
}

const statuses = ["Open", "Shipped", "Returned"];
const tabs = ["Summary", ...statuses];

// Panels suspend while their data loads. The Suspense boundary sits above Tabs.Root, yet its
// fallback never replaces a loaded panel: selection is a transition, so the old panel stays
// (dimmed through .demo-tabs[data-pending]) and the selected tab shows a spinner.
export function TabsSuspending() {
  const [generation, setGeneration] = useState(0);
  return (
    <>
      <Suspense key={generation} fallback={<p>Loading orders…</p>}>
        <Tabs.Root className="demo-tabs" defaultValue="Summary">
          <Tabs.List className="demo-tabs-list" aria-label="Orders">
            {tabs.map((status) => (
              <Tabs.Tab key={status} value={status} className="demo-tab">
                {({ pending }) => (
                  <>
                    {status}
                    {pending && <span className="demo-spinner" aria-hidden />}
                  </>
                )}
              </Tabs.Tab>
            ))}
            <Tabs.Indicator className="demo-tabs-indicator" />
          </Tabs.List>
          {/* The first panel needs no data, so the server-rendered HTML is complete. */}
          <Tabs.Panel value="Summary" className="demo-tab-panel">
            <p>Pick a status. Each list takes 1.2 s to load the first time.</p>
          </Tabs.Panel>
          {statuses.map((status) => (
            <Tabs.Panel key={status} value={status} className="demo-tab-panel">
              <Orders status={status} />
            </Tabs.Panel>
          ))}
        </Tabs.Root>
      </Suspense>
      <button
        type="button"
        className="demo-button"
        onClick={() => {
          cache.clear();
          setGeneration((g) => g + 1);
        }}
      >
        Clear the cache and start over
      </button>
    </>
  );
}

The same rule has two more shapes. Content that is hidden until a state opens it waits in the closed state: the trigger is pending, and the open happens once, complete. Content that is re-rendered for a new input (a search) keeps the previous result on screen, marked stale, until the next one is ready. Each component page describes its case in its Suspense section.

Example: Accordion

Accordion.Panel is always mounted; its function child receives the committed open state, which is how content is mounted lazily. In library mode, if that content suspends, the item stays closed with the trigger pending until the content is ready, then opens.

Example: Combobox

A Combobox renders its results from useDeferredValue of the typed text, so an async search is use(search(query)) inside a boundary in the List. The first load shows the fallback (a new boundary); after that, every keystroke keeps the previous results on screen, marked data-stale and aria-busy, until the new ones are ready. Only the latest query is ever rendered, so responses cannot arrive out of order.

Opting into a fallback

A boundary that mounts with the new content shows its fallback immediately (a React rule). That is the way to say "show a skeleton instead of waiting". Put the boundary inside the content region:

Example: Tabs

<Tabs.Panel value="orders">
  <Suspense fallback={<OrdersSkeleton />}>
    <Orders />
  </Suspense>
</Tabs.Panel>

To get a fresh fallback per tab rather than a wait, key the boundary: <Suspense key={tab}>.

Keep triggers from suspending

A trigger should answer a click at once. Its content is rendered from optimistic state, which React renders right away rather than in the transition. React shows a fallback when such a render suspends, so if a trigger's content suspends, the nearest Suspense boundary above it replaces the trigger (and everything else inside that boundary) with its fallback. That is React working as designed, but rarely what you want.

To keep the trigger on screen:

  • Don't suspend in a trigger's children function, or give the part that suspends its own Suspense boundary inside the trigger. The props tables mark these props "must not suspend".
  • Do the same for content that a component renders ahead of time, before it is shown: keep it static, or put the boundary inside the part that shows it.

Pick a status. Each list takes 1.2 s to load the first time.