# Smart paste

> useSmartPaste fills a form from pasted text - an address, an email, a support ticket - through your own adapter, with any Button. It describes the fields, writes the answer into React-controlled inputs and reports what it filled.

Canonical: https://salmonui.dev/docs/chat/smart-paste

Smart paste is a hook:
`useSmartPaste(formRef, adapter)` from `@salmonui/chat/smart-paste`. It
has no visual part and no stylesheet, so it works with any button; the
examples use the Buttons `Button`.

```tsx
import { useRef } from 'react';
import { Button } from '@salmonui/buttons/button';
import '@salmonui/buttons/button/styles.css';
import { useSmartPaste, type SmartPasteAdapter } from '@salmonui/chat/smart-paste';

const adapter: SmartPasteAdapter = async ({ clipboardText, fields }, { signal }) => {
  const response = await fetch('/api/smart-paste', { method: 'POST', body: JSON.stringify({ clipboardText, fields }), signal });
  return response.json(); // { name: 'Ada Lovelace', country: 'nl', ... }
};

function ContactForm() {
  const formRef = useRef<HTMLFormElement>(null);
  const { paste, pending } = useSmartPaste(formRef, adapter);
  return (
    <form ref={formRef}>
      {/* your fields */}
      <Button loading={pending} onClick={() => paste().catch(() => {})}>
        Smart paste
      </Button>
    </form>
  );
}
```

The hook has no stylesheet of its own; import the stylesheet of the
button you use. Every option and type is in the
[API reference](https://salmonui.dev/docs/reference/chat/smart-paste).

## The adapter

`adapter({ clipboardText, fields }, { signal })` is the only place a
model appears, and it is yours: call your server, which builds the
prompt and talks to the provider. It resolves an object keyed by field
`name`, or JSON text of one; text around the object (a Markdown code fence,
a sentence before it) is ignored. The adapter receives the pasted text and
the field descriptors, never other page data.

## Reading the clipboard

`paste()` reads `navigator.clipboard.readText()`. Call it directly
from the click handler: the read happens before the first `await`, inside
the user gesture, so the browser can show its permission prompt.
`paste(text)` skips the clipboard: use it when the Clipboard API is
unavailable or denied (fall back to a dialog with a textarea), or for text
from a drop or a file.

## Which fields are described

The hook looks at the `input`, `textarea` and `select` elements inside
`formRef` (a `<form>` or any container) and sends one descriptor per `name`:

| Descriptor field | Source |
| --- | --- |
| `name` | The control's `name` (or its `id`); the key of the answer. |
| `label` | The associated `<label>` (without the control's own text), `aria-labelledby`, `aria-label`, or the `<legend>` of a radio or checkbox group. |
| `description` | `data-salmon-paste-description` on the control or an ancestor, else the `aria-describedby` text (the description of a Salmon field). |
| `type` | The input `type`, or `textarea`, `select`. |
| `options`, `multiple` | `<option>` values, radio values, checkbox group values; `multiple` for a multiple select or a checkbox group. |

Skipped: anything inside `data-salmon-paste="off"`, password, file and
hidden inputs, buttons, disabled and read-only controls, and the hidden
mirror elements that custom fields render for forms (`aria-hidden` and out
of the tab order). Use `data-salmon-paste-description` to tell the model
what a field means ("Customer reference, format AB-1234").

## How fields are filled

Text inputs, textareas and selects are written through the element's
prototype `value` setter, followed by bubbling `input` and `change` events;
checkboxes and radios are clicked into the wanted state. React
sees both as user input, so React-controlled inputs update their state,
and the Inputs `TextField`, `TextArea`, `NumberField`, `Checkbox`,
`RadioGroup` and `Switch` work because they render native controls.
Select values match an option value first, then case-insensitively a value
or the option text; a checkbox accepts `true`, `"yes"`, `"on"`, `1`; a
checkbox group or multiple select accepts an array or a comma-separated
string.

Fields that are not native controls, such as the Dropdowns `Select` or a
`DatePicker`, are passed as custom fields: the hook describes them and
calls your `fill`:

```tsx
const [country, setCountry] = useState<string | null>(null);
const { paste } = useSmartPaste(formRef, adapter, {
  fields: [{ name: 'country', label: 'Country', type: 'select', options: ['nl', 'my'], fill: (value) => setCountry(String(value)) }],
});
```

Return `false` from `fill` when the value does not fit; the field is then
reported as skipped.

## Pending state and the result

`pending` is true while a paste waits for the clipboard or the adapter;
pass it to `Button` `loading`, which sets `aria-busy` and ignores
further presses. `paste()` resolves `{ filled, skipped }`: the names it
filled, and the described fields the answer left empty or that did not
accept the value. A new paste aborts the previous one (its `signal`
aborts, its promise rejects with an `AbortError`), and so does unmounting
the component; adapter and clipboard errors reject the promise too.

## Any Button

The hook only needs something to call `paste()`. Every Buttons capability
(content, icons, variants, `disabled`, `loading`, focus and click handling,
documented in the [Button](https://salmonui.dev/docs/buttons/button) page) is the Button's own;
a plain `<button>` works as well.

## SSR

`useSmartPaste` is SSR-safe: it renders nothing, reads the form and the
clipboard only inside `paste()` (a click handler), and `pending` starts as
`false` on the server and the client, so the button's server markup and
the hydrated markup match.

## Design notes

The hook has no common component props: it has no element or
lifecycle of its own, so they belong to the `Button` and the form. It
renders no HTML strings, so it needs no sanitizer. Persist what `paste()`
filled through your form state, set `dir` on an ancestor, style the
`Button` with `className`, and use an effect for setup and teardown.

### Live demo: Smart paste into a contact form

Live demo: https://salmonui.dev/demos/chat/smart-paste

useSmartPaste with two Buttons - one pastes the sample text, one reads the clipboard - filling TextFields, a Checkbox and a TextArea, plus a Select as a custom field, with the pending state and the filled and skipped fields in a log.

`Demo.tsx`:

```tsx
'use client';

import { useRef, useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';
import { TextArea, TextField } from '@salmonui/inputs/text-field';
import { useSmartPaste, type SmartPasteAdapter } from '@salmonui/chat/smart-paste';
import { EventLog, useEventLog } from '../../components/demo-kit';

const PLANS = [
  { value: 'free', label: 'Free' },
  { value: 'pro', label: 'Pro' },
];

const SAMPLE = 'Hi, this is Grace Hopper (grace@example.com) from Arlington. Please move me to the Pro plan and keep the newsletter coming. Call me after 5pm.';

/**
 * A stand-in for your server: a real adapter posts `{ clipboardText, fields }` to an endpoint that
 * prompts a model with the field descriptors. This one finds the values with regular expressions,
 * after a short delay so the pending state is visible.
 */
const adapter: SmartPasteAdapter = async ({ clipboardText }, { signal }) => {
  await new Promise<void>((resolve, reject) => {
    const timer = setTimeout(resolve, 600);
    signal.addEventListener('abort', () => {
      clearTimeout(timer);
      reject(signal.reason);
    });
  });
  const email = /[\w.+-]+@[\w-]+\.[\w.]+/.exec(clipboardText)?.[0];
  const name = /this is ([A-Z][a-z]+ [A-Z][a-z]+)/.exec(clipboardText)?.[1];
  const city = /from ([A-Z][a-z]+)/.exec(clipboardText)?.[1];
  const plan = /\bpro\b/i.test(clipboardText) ? 'pro' : undefined;
  const notes = /(Call [^.]+\.)/.exec(clipboardText)?.[1];
  return { name, email, city, plan, newsletter: /newsletter/i.test(clipboardText), notes };
};

export default function SmartPasteDemo() {
  const formRef = useRef<HTMLFormElement>(null);
  const [text, setText] = useState(SAMPLE);
  const [plan, setPlan] = useState('free');
  const [log, push] = useEventLog();
  // The Select is not a native control, so it is described and filled as a custom field.
  const { paste, pending } = useSmartPaste(formRef, adapter, {
    fields: [
      {
        name: 'plan',
        label: 'Plan',
        type: 'select',
        options: PLANS.map((p) => p.value),
        fill: (value) => {
          const next = PLANS.find((p) => p.value === String(value).toLowerCase());
          if (!next) return false;
          setPlan(next.value);
        },
      },
    ],
  });
  const run = (fromClipboard: boolean): void => {
    (fromClipboard ? paste() : paste(text)).then(
      ({ filled, skipped }) => push(`filled ${filled.join(', ') || '-'}; skipped ${skipped.join(', ') || '-'}`),
      (error: unknown) => push(`error ${error instanceof Error ? error.name : String(error)}`),
    );
  };
  return (
    <div style={{ display: 'grid', gap: 16, maxWidth: 520 }}>
      <TextArea label="Text to paste" rows={3} value={text} onValueChange={setText} />
      <form ref={formRef} style={{ display: 'grid', gap: 12 }} onSubmit={(e) => e.preventDefault()} onReset={() => setPlan('free')}>
        <TextField name="name" label="Full name" />
        <TextField name="email" type="email" label="Email" />
        <TextField name="city" label="City" data-salmon-paste-description="City of the customer's address" />
        <label>
          Plan{' '}
          <Select aria-label="Plan" data={PLANS} value={plan} onValueChange={(value) => value != null && setPlan(String(value))} />
        </label>
        <Checkbox name="newsletter">Newsletter</Checkbox>
        <TextArea name="notes" label="Notes" rows={2} />
        <div data-salmon-paste="off" style={{ display: 'grid' }}>
          <TextField name="agent" label="Agent (never pasted)" defaultValue="Ada" />
        </div>
        <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
          <Button variant="solid" loading={pending} onClick={() => run(false)}>
            Smart paste the text above
          </Button>
          <Button variant="outline" disabled={pending} onClick={() => run(true)}>
            Paste from clipboard
          </Button>
          <Button type="reset" variant="ghost">
            Reset
          </Button>
        </div>
      </form>
      <EventLog entries={log} />
    </div>
  );
}
```
