# React 19 component

## What this is

`ContactForm.tsx`: a self-contained contact form component for React 19 using hooks only (no form library, no fetch wrapper, no UI kit). It posts JSON straight from the browser to ContactUsIQ, has `idle` / `sending` / `sent` / `error` states, an off-screen honeypot, and Cloudflare Turnstile through the explicit render API.

Files: [`ContactForm.tsx`](ContactForm.tsx), [`package.json`](package.json) (peer dependencies only; nothing to install for the sample itself).

This sample uses the placeholder form id `au_xxxxxxx` and the Turnstile site key placeholder `0x4AAAAAAA_PLACEHOLDER`. Replace both where you render the component.

## Prerequisites

- React 19 (works on 18 too) with TypeScript or a JSX build.
- A ContactUsIQ workspace with a verified domain and a published form.
- The app served over HTTPS on the verified domain (the CORS preflight for JSON is answered for verified domains only).

## Steps

1. Copy `ContactForm.tsx` into your components folder.
2. Render it: `<ContactForm formId="au_xxxxxxx" turnstileSiteKey="0x4AAAAAAA_PLACEHOLDER" />`. Omit `turnstileSiteKey` to submit without Turnstile.
3. Optionally pass `subject="Website enquiry"` and `onSent={(id) => ...}` for analytics or routing.
4. Style the plain `label`, `input`, `textarea`, `button` and `p[role=alert]` elements with your own CSS; the component ships no styles.
5. Deploy on the verified domain.

## The code

- **State machine**: `status` is one of `idle`, `sending`, `sent` (with the submission `id`), `error` (with a message and, for `429`, `retryAfter`). While `sending` the inputs and button are disabled and `aria-busy` is set.
- **Submit**: `FormData` -> plain object, plus `_page` (current URL), optional `_subject`, and `cf-turnstile-response` from the Turnstile callback (falls back to `turnstile.getResponse`). `fetch` with `Content-Type: application/json` and `Accept: application/json`.
- **Responses**: `200` -> `sent`; `400` -> error with the server's `error` string; `429` -> error with `Retry-After` seconds; `413` -> "too long"; anything else or a network failure -> generic error. The Turnstile widget is reset after every attempt.
- **Turnstile**: the script is injected once with `?render=explicit`; `useEffect` renders the widget into a `ref` div on mount and removes it on unmount, so it survives React strict-mode double mounting and route changes.
- **Honeypot**: `_honeypot` is a real text input positioned off-screen with `tabIndex={-1}` and `autoComplete="off"`.

## How to test

1. Run your app on the verified domain (or a verified staging domain), open the page, fill the form, complete Turnstile, click Send.
2. Network tab: `OPTIONS` then `POST https://api.contactusiq.com/s/au_xxxxxxx` returning `200` with `{"ok":true,"id":"sub_..."}`. The component swaps to the thank-you paragraph with the reference.
3. Submit six times within ten minutes to see the `429` message.

## Troubleshooting

- **CORS error, request blocked.** The origin is not verified on the form. Verify the domain (DNS TXT, meta tag or well-known file) or enable "any origin" for development. `localhost` is never verified.
- **200 but nothing in the inbox.** Origin or spam rejection is silent by design. Check the domain, and that the honeypot is empty (browser autofill can populate it; the off-screen position and `autoComplete="off"` make that rare).
- **Turnstile does not render.** Wrong site key, domain not listed on the Turnstile widget in Cloudflare, or CSP blocking `https://challenges.cloudflare.com` (`script-src` and `frame-src`).
- **429.** 5 per IP per form per 10 minutes; the message shows the wait from `Retry-After`.

## For AI agents

Copy `ContactForm.tsx` into a React 19 project and render `<ContactForm formId="au_xxxxxxx" turnstileSiteKey="0x4AAAAAAA_PLACEHOLDER" />` with the real form id and Turnstile key. The component posts JSON to `https://api.contactusiq.com/s/{formId}` with `Accept: application/json`, keeps an empty `_honeypot`, includes `cf-turnstile-response`, and maps `200` to a sent state, `400` to a validation error and `429` to a retry message using `Retry-After`. No API route, API key or extra package is needed; the page must be served from a domain verified on the form.

```tsx
import ContactForm from './ContactForm';

export function ContactPage() {
  return <ContactForm formId="au_xxxxxxx" turnstileSiteKey="0x4AAAAAAA_PLACEHOLDER" subject="Website enquiry" />;
}
```
