# Getting started

Install the package, import a component from its subpath, and style it with CSS. There is no provider, no theme and no stylesheet to import.

## Install

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

> **Beta**
>
> `@funstack/ui` is in beta. Breaking changes can still ship in any release, each with a migration note in the release notes. [Stability](roadmap.md#stability) has the details.

React **19.3 or later** is a peer dependency (`react` and `react-dom` `>=19.3.0`). The library relies on React 19.3 features such as `<ViewTransition>` and `addTransitionType`. The package is ESM only and has no other runtime dependencies. A project template can still ask for an older React: TanStack's `@tanstack/cli create` writes `"react": "^19.2.0"`. A fresh install resolves that range to the latest 19.x, but a lockfile made earlier may hold 19.2, so raise the range to `^19.3.0`.

With the Next.js App Router, which runs its own React canary, see [Next.js App Router](concepts/nextjs.md). With React Router in framework mode, which has no Server Functions, see [React Router framework mode](concepts/react-router.md). With TanStack Start under a Content Security Policy with nonces, the default server handler gives React the nonce in a form that leaves the library's `<style>` elements without it, so it needs a custom server entry ([Content Security Policy](concepts/content-security-policy.md#tanstack-start)).

## Imports

Every component has its own subpath, which is the documented form. The root entry re-exports everything, and both resolve to the same files.

```tsx
import { Toggle } from "@funstack/ui/toggle";
import { Tabs } from "@funstack/ui/tabs";
import { Accordion } from "@funstack/ui/accordion";
import { Tooltip } from "@funstack/ui/tooltip";

// Also works:
import { Tabs, Toggle } from "@funstack/ui";

// Shared types live in @funstack/ui/types (the root entry exports them too).
import type { ActionDetails, ChangeAction } from "@funstack/ui/types";
```

`@funstack/ui/types` holds the types that several components share and none owns: `Action`, `ActionDetails`, `ChangeAction`, `ChangeReason`, `Rejectable`, `RenderProp`, `StateValue`, `UrgentChangeHandler`, `UrgentHandler` and `WhilePending`. It contains types only, so importing it costs nothing at runtime. A type that belongs to one component (`TabsValueChangeDetails`) comes from that component's subpath; the component subpaths do not re-export the shared types.

Single-element components are plain components (`Toggle`). Compound components are a namespace of parts (`Tabs.Root`, `Tabs.Tab`, ...). The namespace itself is not a component.

A <a id="part"></a>part is one piece of a component that you render yourself and that usually renders one element: `Tabs.Tab` is a part, and so is a whole `Toggle`. The Root is the part of a compound component that holds its state (and its transition, when it has one) and provides them to the other parts through context; it is usually, not always, the outermost part. Other recurring words (trigger, content region, optimistic and committed value) are defined on [Pending & optimistic state](concepts/pending-and-optimistic.md#terms).

### With the Vite dev server

When the dev server starts, Vite scans your code (lazy `import()`s included) and pre-bundles each dependency it finds; every subpath counts as one. When the dev server meets a subpath for the first time after it has started (an import you added while it was running), it pre-bundles again, logs "optimized dependencies changed. reloading" and reloads the page. Until that reload the page can run two copies of React and fail with "Invalid hook call" ("Cannot read properties of null (reading 'useState')"). After the reload it works. To avoid it, list the subpaths you use in `optimizeDeps.include`, so they are pre-bundled when the server starts:

```tsx
// vite.config.ts
export default defineConfig({
  optimizeDeps: {
    include: ["@funstack/ui/tabs", "@funstack/ui/drawer", "@funstack/ui/toast"],
  },
});
```

Excluding the package, `optimizeDeps: { exclude: ["@funstack/ui"] }`, avoids it too, and is the setting a React Server Components setup needs anyway ([With the Vite dev server](concepts/server-components.md#vite-dev)). Production builds are not affected.

## A first component

```tsx
"use client";
import { Toggle } from "@funstack/ui/toggle";
import { saveFavorite } from "./api";

export function FavoriteButton({ postId, initial }: { postId: string; initial: boolean }) {
  return (
    <Toggle
      className="favorite"
      defaultPressed={initial}
      pressedChangeAction={(pressed) => saveFavorite(postId, pressed)}
    >
      Favorite
    </Toggle>
  );
}
```

`pressedChangeAction` is an [action](concepts/action-props.md#action): it runs inside a transition. While the returned promise is pending, the button already shows the new pressed state ([optimistic](concepts/pending-and-optimistic.md#optimistic)) and carries `data-pending`. If it rejects, the pressed state rolls back and the error goes to the nearest Error Boundary. The [Action props](concepts/action-props.md) page explains the details.

## <a id="styling"></a>Styling: data attributes and CSS

Components render plain, accessible HTML and ship no styles. Library state appears as `data-*` attributes, emitted by presence (`data-pending=""` when true, absent when false). Enum-like states carry a value (`data-orientation="vertical"`).

```css
.favorite[data-pressed] { background: gold; }
.favorite[data-pending] { cursor: progress; opacity: 0.8; }
.favorite[data-disabled] { opacity: 0.5; }

/* Interaction state comes from the platform, not from attributes. */
.favorite:hover { filter: brightness(1.05); }
.favorite:focus-visible { outline: 2px solid; }
```

Hover, focus and active state are deliberately not attributes: use `:hover`, `:focus-visible` and `:active`. Tailwind's `data-*` variants work as is: `data-pressed:bg-amber-300 data-pending:opacity-80`.

When CSS is not enough, `className`, `style` and `children` also accept a function of the part's state:

```tsx
<Toggle
  className={({ pressed, pending }) => (pending ? "fav busy" : pressed ? "fav on" : "fav")}
>
  {({ pressed, pending }) => (pending ? <Spinner /> : pressed ? <StarFilled /> : <StarOutline />)}
</Toggle>
```

Each component page lists the fields of a part's state object in the **State** line under the part's props table.

To render a different element (your design system's button, a link), pass `render`; see [Replacing the element: `render`](concepts/render.md).

## What the components share

These props mean the same on every component that has them:

| Prop | Meaning |
| - | - |
| `x` / `defaultX` / `xChangeAction` | Controlled value, initial uncontrolled value, and the action called on every change. |
| `transition` | On parts whose content is driven by library state: renders a `<ViewTransition>` around the part. A class animates only this instance's own changes (the ones the library dispatched for it) and its own mount and unmount (when the part is the top of what mounts or unmounts); a map from transition type to class follows React's rules. Without it the part renders no `<ViewTransition>`. See [The transition prop](concepts/transitions.md#transition-prop). |
| `render` | Replaces the rendered element (no `asChild`, no `as`). See [Replacing the element](concepts/render.md). |
| `ref` | An ordinary prop on every part that renders an element. |
| `experimental_*` | Opt-in use of a platform feature that is not in every engine yet. |

One attribute is shared too, but it is not a prop: every component with an action renders `data-pending` while an action dispatched through it is in flight (see [Styling](#styling) above).
