# Domain connect

> A custom domain from typed to live: copyable records that tick green as DNS finds them, checks that wait longer each time, and a rail that runs to Live.

Custom domain screens usually leave people refreshing a table of records and wondering whether anything is happening. Here the address you type (a pasted URL is cut down to its host) lifts out of the field into the domain's own heading as the records it needs open beneath it, each value with its own copy button. The card checks again by itself, waiting longer each time and never while the tab is hidden; "Checking again in 0:42" rolls down, and Check now asks at once. Each record ticks green the moment your server sees it, the rail above fills record by record, then waits on the certificate and runs to Live, where the status morphs, a check draws, the records fold behind "Show records" and the address becomes a link. If something is wrong, your server's message says what, in plain words.

## Install

```bash
npx shadcn@latest add @hairline/domain-connect
```

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/domain-connect
- Registry item (source): https://hairlineui.com/r/domain-connect.json

## Usage

```tsx
import { DomainConnect } from "@/components/domain-connect";

<DomainConnect
  onAdd={(domain) => api.addDomain(domain)}       // → { domain, status, records }
  check={(domain, { signal }) => api.getDomain(domain, { signal })}
  onRemove={(domain) => api.removeDomain(domain)}
/>
```

## Custom hostnames on Cloudflare for SaaS

Your route creates the custom hostname and maps what Cloudflare reports to the card's state: the CNAME to your fallback origin, the ownership TXT record, and the certificate's validation records. The card polls the GET route on its own schedule. Vercel's domains API and Resend's sending domains map the same way.

```tsx
// app/api/domains/route.ts
import type { DomainState } from "@/components/domain-connect";

const CF = `https://api.cloudflare.com/client/v4/zones/${process.env.CF_ZONE_ID}/custom_hostnames`;
const headers = { Authorization: `Bearer ${process.env.CF_API_TOKEN}`, "Content-Type": "application/json" };

function toState(h: any): DomainState {
  const live = h.status === "active";
  const ssl = h.ssl ?? {};
  const records = [
    { type: "CNAME", name: h.hostname, value: "customers.relay.app", status: live ? "verified" : "pending" },
    ...(h.ownership_verification ? [{ type: "TXT", name: h.ownership_verification.name, value: h.ownership_verification.value, status: live ? "verified" : "pending" }] : []),
    ...(ssl.validation_records ?? []).filter((r: any) => r.txt_name).map((r: any) => ({ type: "TXT", name: r.txt_name, value: r.txt_value, status: ssl.status === "active" ? "verified" : "pending" })),
  ] as DomainState["records"];
  return { domain: h.hostname, status: live && ssl.status === "active" ? "active" : live ? "issuing" : "pending", records, message: h.verification_errors?.[0] ?? null };
}

export async function POST(req: Request) {
  const { domain } = await req.json();
  const res = await fetch(CF, { method: "POST", headers, body: JSON.stringify({ hostname: domain, ssl: { method: "txt", type: "dv" } }) });
  const body = await res.json();
  if (!body.success) return Response.json({ error: body.errors?.[0]?.message ?? "Couldn't add it." }, { status: 400 });
  return Response.json(toState(body.result));
}

export async function GET(req: Request) {
  const domain = new URL(req.url).searchParams.get("domain")!;
  const body = await (await fetch(`${CF}?hostname=${encodeURIComponent(domain)}`, { headers })).json();
  return Response.json(toState(body.result[0]));
}

// app/settings/domain.tsx
"use client";
<DomainConnect
  onAdd={async (domain) => {
    const res = await fetch("/api/domains", { method: "POST", body: JSON.stringify({ domain }) });
    if (!res.ok) throw new Error((await res.json()).error);
    return res.json();
  }}
  check={async (domain, { signal }) => (await fetch(`/api/domains?domain=${domain}`, { signal })).json()}
/>
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onAdd` | `(domain) => Promise<DomainState>` | – | Registers the domain (already cut down to its host) and returns { domain, status, records, message? }. Throw to keep the field and show the message. |
| `check` | `(domain, { signal }) => Promise<DomainState>` | – | Reports what your server sees now. Called on the schedule and by Check now; the signal aborts it when the domain is removed or a newer check starts. |
| `onRemove` | `(domain) => Promise<unknown>` | – | Removes the domain; the card goes back to the field. Without it there is no Remove. |
| `value` | `DomainState \| null` | – | Controlled state. status is "pending", "issuing", "active" or "failed"; each record is { type, name, value, priority?, status? }. |
| `defaultValue` | `DomainState \| null` | – | A domain that was already added, when uncontrolled. |
| `onValueChange` | `(state \| null) => void` | – | Every add, check and removal. |
| `schedule` | `number[]` | `[10, 20, 30, 60, 120, 300]` | Seconds before each automatic check; the last one repeats. Checks wait while the tab is hidden. |
| `title` | `string` | `"Custom domain"` | The card's heading. |
| `description` | `string` | – | The line under the heading. |
| `placeholder` | `string` | `"app.yourcompany.com"` | The field's placeholder and the example in its error. |

## States

- **add**: A pasted URL becomes its host on paste ("https://App.Fernhill.co/pricing" → "app.fernhill.co"). Anything that isn't a domain shakes the field and says so. Connect turns to Connecting while onAdd runs.
- **added**: The field folds away in 480ms while the domain opens; its heading travels 620ms from where the address sat in the field. The records rise in one after another, 70ms apart.
- **waiting**: A pinging amber dot says "Waiting for DNS", then "1 of 2 records found". "Checking again in 0:42" rolls down a second at a time; while check() runs it reads "Checking DNS" with a spinner.
- **found**: A record that verifies swaps its spinner for a drawn check, says Found and glows green for 1.4s; the rail fills to that record's share of the first step in 760ms.
- **certificate**: With every record found the status morphs to "Issuing certificate" and the middle step spins until your server reports the domain active.
- **live**: The rail runs to its end, each step draws its check, "Live" turns green, the records fold behind "Show records" and the heading becomes a link to the domain.
- **failed**: Your server's message opens under the records in red and automatic checks stop; the wrong record is marked. Check now starts again.

## What it handles

Custom domain screens usually leave people refreshing a table of DNS records and wondering whether anything is happening. This takes one domain from typed to live, and shows each step on the way.

- **Paste a URL, get a domain**: A pasted address is cut down to its host. Anything that is not a domain shakes the field and says so.
- **Records you can copy**: Each record opens with its own copy buttons, named after the record, such as “Copy CNAME value”.
- **It checks again by itself**: Checks wait longer each time and never run while the tab is hidden. “Checking again in 0:42” counts down, and Check now asks at once.
- **Records tick green**: Each record turns green the moment your server sees it, and the rail above fills record by record.
- **From certificate to Live**: It shows “Issuing certificate”, then Live. The records fold behind “Show records” and the address becomes a link.
- **Failures in plain words**: Your server’s message says what is wrong, the wrong record is marked and automatic checks stop.

## Replaces

a DNS records table, copy buttons, a polling loop, a progress rail, verification messages

## Where it fits

- **Custom domains for SaaS**: Let customers serve their app or site from their own domain.
- **Site and page builders**: Connect a domain to a published site and show when it is live.
- **Email sending domains**: Verify a domain before a product sends email from it.
- **Help centres and status pages**: Customer-branded pages that live on the customer’s own address.

## Notes

- The records are a table with a row per record; each name and value has its own copy button, named after the record ("Copy CNAME value"), whose icon turns into a drawn check.
- Changes in status ("2 of 2 records found", "app.fernhill.co is live") are announced in a status region; the countdown is hidden from screen readers.
- Automatic checks stop when the domain is live, has failed or has been removed, and wait while the page is hidden. A newer check aborts an older one.
- The rail is drawn for the eye only; the status line under the heading says the same in words.
- Reduced motion swaps every state in place: no flight, rises, glows or pings.

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