# Integrations

> Settings → Integrations, done properly: Connect waits on the provider's own window, then folds away as the account it connected opens in its place.

Integration pages usually redirect away to the provider and drop you back on a reloaded page, or flip a Connect button to a grey "Connected" with no idea which account it used. Here Connect says Connecting while the provider's own window is open (openOAuthWindow opens it and waits for your callback). When it comes back the button folds away and the account it connected opens in its place, its avatar and name with a check drawing beside them, and what the connection may do opens under the row. A closed window or a refusal shakes the button, says Try again and lifts the reason in under the description. Category tabs slide their highlight and search narrows the list; rows that stay glide into place. Disconnect stretches the plug into a confirm and folds the account back into Connect.

## Install

```bash
npx shadcn@latest add @hairline/integrations
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `text-morph`, `number-roll`, `status-button`
- Docs and live preview: https://hairlineui.com/ui/integrations
- Registry item (source): https://hairlineui.com/r/integrations.json

## Usage

```tsx
import { Integrations, openOAuthWindow } from "@/components/integrations";

<Integrations
  defaultValue={integrations}
  onConnect={async (i) => {
    const { account } = await openOAuthWindow(`/api/oauth/${i.id}/start`);
    return account;
  }}
  onDisconnect={(i) => fetch(`/api/integrations/${i.id}`, { method: "DELETE" })}
/>
```

## OAuth in a small window, finished by your callback page

The start route redirects to the provider; the provider redirects back to your callback route, which swaps the code for tokens, stores them, and renders a tiny page that posts the account to the window that opened it and closes. openOAuthWindow must be the first thing onConnect does, inside the click, or the browser blocks the window. Nango and Pipedream Connect replace both routes with their own.

```tsx
// app/api/oauth/[provider]/start/route.ts
export async function GET(req: Request, { params }: { params: Promise<{ provider: string }> }) {
  const { provider } = await params;
  const state = crypto.randomUUID();
  const url = new URL(PROVIDERS[provider].authorizeUrl);
  url.searchParams.set("client_id", PROVIDERS[provider].clientId);
  url.searchParams.set("redirect_uri", `${process.env.APP_URL}/api/oauth/${provider}/callback`);
  url.searchParams.set("scope", PROVIDERS[provider].scopes.join(" "));
  url.searchParams.set("state", state);
  return new Response(null, {
    status: 302,
    headers: { Location: url.toString(), "Set-Cookie": `oauth_state=${state}; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=600` },
  });
}

// app/api/oauth/[provider]/callback/route.ts
export async function GET(req: Request, { params }: { params: Promise<{ provider: string }> }) {
  const { provider } = await params;
  const account = await exchangeAndStore(provider, req); // checks state, swaps the code, saves tokens
  // Escaped so an account name can't close the script tag.
  const data = JSON.stringify({ type: "oauth:done", account }).replace(/</g, "\u003c");
  return new Response(
    `<script>
      window.opener?.postMessage(${data}, location.origin);
      new BroadcastChannel("oauth").postMessage(${data});
      window.close();
    </script>`,
    { headers: { "Content-Type": "text/html" } },
  );
}

// app/settings/integrations.tsx
"use client";
import { Integrations, openOAuthWindow, type IntegrationAccount } from "@/components/integrations";

<Integrations
  defaultValue={integrations}
  settingsHref={(i) => `/settings/integrations/${i.id}`}
  onConnect={async (i) => {
    const { account } = await openOAuthWindow<{ account: IntegrationAccount }>(`/api/oauth/${i.id}/start`, { name: i.id });
    return account;
  }}
  onDisconnect={async (i) => {
    const res = await fetch(`/api/integrations/${i.id}`, { method: "DELETE" });
    if (!res.ok) throw new Error("It couldn't be disconnected.");
  }}
/>
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onConnect` | `(integration) => Promise<{ name, detail?, image? }>` | – | Runs the provider's sign-in and returns the account it connected. Called inside the click. Throw to show Try again and the message. |
| `onDisconnect` | `(integration) => Promise<unknown>` | – | Disconnects after a confirm. Without it there is no Disconnect. |
| `value` | `Integration[]` | – | Controlled list: { id, name, description?, icon?, category?, account?, scopes? }. |
| `defaultValue` | `Integration[]` | – | Starting list when uncontrolled. |
| `onValueChange` | `(integrations) => void` | – | After every connect and disconnect. |
| `settingsHref` | `(integration) => string \| undefined` | – | A Settings link beside a connected integration's name. |
| `title` | `string` | `"Integrations"` | Heading and the list's accessible name. |
| `description` | `string` | – | The line under the heading. |
| `openOAuthWindow` | `(url, { name?, width?, height?, type?, timeout? }) => Promise<data>` | – | Exported helper: opens a centred window and resolves with what your callback posts ({ type: "oauth:done", … }) by postMessage or BroadcastChannel; rejects if the window closes first, is blocked or times out. |

## States

- **connect**: Connect turns to Connecting with a spinner the moment it's clicked; onConnect is called in the same click, so a window it opens isn't blocked.
- **connected**: The button folds sideways (460ms) while the account chip opens from a 3px blur 120ms behind it; a check draws beside the name 380ms in. The scopes and the account's detail open under the row by height, and "3 connected" rolls.
- **refused**: If onConnect throws, the button shakes and says Try again, and the reason lifts in over the description in red for 4.2s before both settle back.
- **filter**: With more than one category, tabs appear and their highlight slides between them (380ms); search narrows by name and description. Rows that stay glide to their new places, the rest fold away.
- **disconnect**: The plug stretches into a red Disconnect; a check draws, then the account folds back and Connect opens again.

## What it handles

Integration pages often redirect away to the provider and drop people back on a reloaded page, or flip a button to a grey “Connected” with no idea which account was used. This keeps people on the page and shows what was connected.

- **Connect without leaving**: The provider’s own window opens and the button reads Connecting until it comes back.
- **It shows which account**: The account’s avatar and name open in place of the button, and what the connection may do opens under the row.
- **Popups aren’t blocked**: onConnect is called inside the click. The openOAuthWindow helper opens the window and waits for your callback page.
- **Refusals are explained**: A closed or refused window shakes the button, says Try again and shows the reason under the description.
- **Categories and search**: Tabs and search narrow the list, and the rows that stay glide into place.
- **Disconnect asks first**: The plug stretches into a confirm. Once done, the account folds away and Connect returns.

## Replaces

an OAuth popup helper, a Connect button with states, a connected account row, category tabs and search, a disconnect confirmation

## Where it fits

- **Settings → Integrations**: The page where people connect Slack, GitHub or Google Drive.
- **Onboarding**: Ask new users to connect the tools they already use.
- **App directories**: A set of connectors grouped in categories and searchable.
- **AI products**: Connect the data sources an assistant is allowed to read.

## Notes

- Each Connect button is named after its integration. Connecting, connected accounts, refusals and disconnects are announced in a status region; the drawn check is for the eye and the chip says "connected" to screen readers.
- Category tabs are a tablist that arrows move along; search filters by name and description.
- openOAuthWindow only accepts messages from your own origin, and gives the callback's message a moment to land before treating a closed window as cancelled.
- Reduced motion swaps Connect and the account in place, with no folds, blurs or glides.

License: MIT (https://hairlineui.com/license#components)
