# Codes and secrets

Show a short code on one device, type it on another, lay out a long secret, and copy that tells the truth.

Live variants: https://design.arcadia.is/components/codes

```tsx
import {
  CodeInput, CodeDisplay, KeyBlock, CopyButton, copyText, setClipboardBridge,
} from "@arcadia/ui";
```

```tsx
import { useState } from "react";
import { CodeDisplay, CopyButton, Field, CodeInput } from "@arcadia/ui";

function Pair() {
  const [typed, setTyped] = useState("");
  const complete = typed.length === CODE.length;
  const verified = typed === CODE;
  return (
    <div {...sx("cmp-form", cmp.form)}>
      <div {...sx("cmp-code-row", cmp.bar)}>
        <CodeDisplay code={CODE} label="Code for MacBook Air" />
        <CopyButton text={CODE} iconOnly label="Copy code" />
      </div>
      <Field
        label="Code shown on MacBook Air"
        error={complete && !verified ? "Codes don't match. Check the digits and try again." : undefined}
      >
        <CodeInput value={typed} onValueChange={setTyped} success={verified} />
      </Field>
    </div>
  );
}
```

```tsx
import { CopyButton, KeyBlock } from "@arcadia/ui";

<KeyBlock value={SAMPLE_KEY} label="Example recovery key" actions={<CopyButton text={SAMPLE_KEY} iconOnly label="Copy key" />} />
```

## Accessibility

`CodeInput` is one labelled text field (`autocomplete="one-time-code"`), so screen readers and password managers see a normal input; the boxes are decorative and no character is read twice. In a `Field` the label's `for`, the hint, the description and the error reach it as for any control, so the field never carries a second name over the one on screen. `invalid` sets `aria-invalid`; `success` says "Code verified" in a polite region. The shake honours reduced motion, and so does the roll of a character into its box.

## API reference

### CodeInput

- `length` (`number`, default `6`)
- `kind` (`"numeric" | "alphanumeric"`, default `"numeric"`)
- `value` (`string`)
- `defaultValue` (`string`, default `""`)
- `onValueChange` (`(value: string) => void`)
- `onComplete` (`(value: string) => void`)
- `invalid` (`boolean`, default `false`)
- `success` (`boolean`, default `false`)
- `disabled` (`boolean`, default `false`)
- `autoFocus` (`boolean`, default `false`)
- `group` (`number`, default `3`)
- `label` (`string`)
- `name` (`string`)
- `id` (`string`)
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).

### CodeDisplay

- `code` (`string`, required): The code, without separators ("482913").
- `group` (`number`, default `3`): Characters per visual group (a gap, never a character, separates them).
- `size` (`"md" | "lg"`, default `"lg"`): `lg` for the one code a screen is about; `md` inside a row or a sheet.
- `label` (`string`, default `"Code"`): What the code is, for assistive tech ("Code"): the group's name, never drawn.
- `rootStyle` (`RootStyle`): Typed StyleX override: layout only.

### KeyBlock

- `value` (`string`, required)
- `perLine` (`number`, default `4`)
- `label` (`string`, required)
- `actions` (`ReactNode`)
- `onCopied` (`() => void`)
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).

### CopyButton

- `text` (`string | (() => string)`, required): The text written to the clipboard (or a function that produces it at click time).
- `label` (`string`, default `"Copy"`): The label at rest.
- `copiedLabel` (`string`, default `"Copied"`): The label for COPIED_HOLD_MS after a copy that landed.
- `failedLabel` (`string`, default `"Couldn't copy"`): The label after a copy the clipboard refused.
- `iconOnly` (`boolean`, default `false`): Draw only the glyph (an IconButton named by `label`).
- `clipboard` (`ClipboardWriteOptions`): Clipboard options: `clearAfterMs` for a secret.
- `onCopied` (`(ok: boolean) => void`): Called after every copy with whether it landed.
- `variant` (`"primary" | "secondary" | "ghost" | "text" | "danger" | "bare"`, default `"secondary"`): `primary` is the monochrome ink CTA, for the one action that moves the flow forward (send). `bare` is a row or a line of text that presses: no fill, frame, padding or height of its own, in the text style and ink of the line it stands in, its label on its capitals, its ink a step up under the pointer.
- `size` (`"xs" | "sm" | "md" | "lg"`, default `"sm"`): Heights 20 / 24 / 28 / 32: `xs` and `sm` stand in a Caption pair, `md` in Body, `lg` in Headline, all bold.
- `tone` (`"neutral"`)
- `density` (`"compact" | "large"`)
- `iconEnd` (`IconName`): Trailing icon. Beside a wrapped text label, it sits on the first line.
- `hint` (`ShortcutSpec | ReactElement`): Trailing key hint, drawn quietly: `"⇧Tab"`, `"⏎"`, `["⌘","K"]`, `"mod+k"`, or a `Kbd`. It stands at the end of a button wider than its words (a search trigger), the label taking the room between. Visual only (`aria-hidden`, so the name stays "Allow", not "Allow ⏎"); the keys are exposed as `aria-keyshortcuts` on the button. Binding the key is yours.
- `progress` (`number`): Determinate progress, 0..1: a ring fills in the icon's slot (a download, an upload) while the label says the number. The button stays pressable; `loading` wins when both are set.
- `labelRow` (`boolean`): The label is a row of parts (an avatar beside words), each on its capitals.
- `wrap` (`boolean`, default `false`): Its label wraps onto more lines instead of running past the button, growing it: a choice whose words are longer than a narrow column. One line keeps the control's height.
- `roundEnds` (`boolean`): Each side that ends in text takes the room its round end cuts (R12) under a pill's corner too. A pill always takes it.
- `rootStyle` (`ButtonStyle`): Typed StyleX override: where the button sits (margins, self-alignment, flex and grid placement, size bounds) and its `--button-*` knobs.
- `render` (`RenderProp<{ disabled: boolean }>`): Replace the rendered element, e.g. `render={<a href="/settings" />}`, or a function of props and state.
- `disabled` (`boolean`): Native disabled: no pointer events, no tooltip, skipped by Tab.
- `focusableWhenDisabled` (`boolean`): Keep a disabled control focusable and hoverable (`aria-disabled`), so a tooltip can say why.
- `nativeButton` (`boolean`): Button semantics, inferred from what renders: a `<button>` is native, an `<a>` stays a link, anything else gets `role="button"` and key handling. `false` forces button semantics on whatever renders (a link acting as a button).

Rest props go to `<button>` props.
