# Secret field

A key or token you can show and copy: the eye swaps when shown, and the copy icon draws its tick and turns green for a moment.

Tier: free. Type: component. Category: inputs. Tags: settings.

## Install

```bash
npx shadcn@latest add https://pixilui.com/r/secret-field.json
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/secret-field.tsx`.

## Preview

Live preview: https://pixilui.com/components/secret-field

Knobs:

- `size`: `none`, `sm`, `md`, `lg`, `xl`

Signature: Copy the key and the icon draws its tick in green, holds it long enough to read, then goes back.

Styles: `lively` (default), `quiet`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: lively https://pixilui.com/r/standalone/secret-field.tsx, quiet https://pixilui.com/r/standalone/secret-field.quiet.tsx.

## Code

`components/pixilui/secret-field.tsx`

```tsx
"use client";

/*
  The secret field, locked 1 Dec 2026 (design/explorations/components/locked/inputs, B8). The frame is the
  Field's (../field/field); the look is foundation.css section 8.

  At rest   the key, masked, read-only on a soft fill (still selectable and copyable).
  Show      the eye swaps (the animated reveal icon plays data-state="revealed"); the button says Hide.
  Copy      copies without revealing; the copy icon draws its tick and turns green, and the button says
            Copied, for --pixilui-field-confirm-hold (1600ms).
*/

import { forwardRef, useEffect, useRef, useState, type ComponentPropsWithoutRef, type ReactNode } from "react";
import { Field, FieldControl, FieldIconButton, FieldInput, FieldPrefix, FieldSuffix, fieldTime, type FieldSize } from "./field";

export type SecretFieldProps = Omit<ComponentPropsWithoutRef<"input">, "size" | "prefix" | "value" | "defaultValue" | "type" | "onCopy"> & {
  /** The key or token. */
  value: string;
  /** The label above the field. */
  label?: ReactNode;
  /** Help under the field ("Only admins can rotate it"). */
  description?: ReactNode;
  /** The icon before the key (the animated key icon). */
  icon?: ReactNode;
  /** The show button's icon (the animated reveal icon: its eye swaps on reveal). */
  revealIcon: ReactNode;
  /** The copy button's icon (the animated copy-check icon: it draws its tick). */
  copyIcon: ReactNode;
  /** The show button's name while the key is hidden. */
  showLabel?: string;
  /** The show button's name while the key shows. */
  hideLabel?: string;
  /** The copy button's name. */
  copyLabel?: string;
  /** The copy button's name for a moment after copying. */
  copiedLabel?: string;
  /** Runs after the key is copied. */
  onCopy?: (value: string) => void;
  /** The button height of the same name. */
  size?: FieldSize;
  /** Classes for the field. Native attributes and the ref go to the input. */
  className?: string;
};

/** An API key or token, masked, with show and copy; copying never reveals it. */
export const SecretField = forwardRef<HTMLInputElement, SecretFieldProps>(function SecretField(
  { value = "", label, description, icon, revealIcon, copyIcon, showLabel = "Show key", hideLabel = "Hide key", copyLabel = "Copy key", copiedLabel = "Copied", onCopy, size, className, id, ...props },
  ref,
) {
  const field = useRef<HTMLLabelElement | null>(null);
  const [shown, setShown] = useState(false);
  const [copied, setCopied] = useState(false);
  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
  useEffect(() => () => clearTimeout(timer.current), []);
  return (
    <Field ref={field} className={className} label={label} description={description} kind="secret" size={size} filled={value.length > 0} inputId={id}>
      <FieldControl>
        {icon != null ? <FieldPrefix>{icon}</FieldPrefix> : null}
        <FieldInput ref={ref} type={shown ? "text" : "password"} value={value} readOnly {...props} />
        <FieldSuffix>
          <FieldIconButton data-reveal="" data-state={shown ? "revealed" : undefined} aria-pressed={shown} aria-label={shown ? hideLabel : showLabel} onClick={() => setShown((s) => !s)}>
            {revealIcon}
          </FieldIconButton>
          <FieldIconButton
            data-copy=""
            data-state={copied ? "copied" : undefined}
            aria-label={copied ? copiedLabel : copyLabel}
            onClick={async () => {
              try {
                await navigator.clipboard.writeText(value);
              } catch {
                // the clipboard can refuse (no permission); the tick still answers the press, as on the locked page
              }
              onCopy?.(value);
              setCopied(true);
              clearTimeout(timer.current);
              timer.current = setTimeout(() => setCopied(false), fieldTime(field.current, "--pixilui-field-confirm-hold"));
            }}
          >
            {copyIcon}
          </FieldIconButton>
        </FieldSuffix>
      </FieldControl>
    </Field>
  );
});
```

## Usage

When to use:

- API keys, webhook secrets and tokens in settings.

When not to use:

- A password a person types: use Input with type="password".

```tsx
import { IconCopyCheck } from "@/components/pixilui/icons/copy-check";
import { IconKey } from "@/components/pixilui/icons/key";
import { IconReveal } from "@/components/pixilui/icons/reveal";
import { SecretField } from "@/components/pixilui/secret-field";
import type { FieldSize } from "@/components/pixilui/field";

// The first option of each knob is the demo's default; the docs preview starts there.
export const knobs = {
  size: ["none", "sm", "md", "lg", "xl"],
} as const;

/** An API key: show it, copy it. */
export default function SecretFieldDemo({ size = "none" }: KnobValues<typeof knobs>) {
  return (
    <div className="w-full max-w-sm">
      <SecretField label="API key" icon={<IconKey />} value="sk-live-4f9a2c7e81b04d6aa3e5" revealIcon={<IconReveal />} copyIcon={<IconCopyCheck />} description="Only admins can rotate it" size={size === "none" ? undefined : (size as FieldSize)} />
    </div>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes for the field. Native attributes and the ref go to the input. |
| `value` (required) | `string` | `""` | The key or token. |
| `label` | `ReactNode` |  | The label above the field. |
| `description` | `ReactNode` |  | Help under the field ("Only admins can rotate it"). |
| `icon` | `ReactNode` |  | The icon before the key (the animated key icon). |
| `revealIcon` (required) | `ReactNode` |  | The show button's icon (the animated reveal icon: its eye swaps on reveal). |
| `copyIcon` (required) | `ReactNode` |  | The copy button's icon (the animated copy-check icon: it draws its tick). |
| `showLabel` | `string` | `"Show key"` | The show button's name while the key is hidden. |
| `hideLabel` | `string` | `"Hide key"` | The show button's name while the key shows. |
| `copyLabel` | `string` | `"Copy key"` | The copy button's name. |
| `copiedLabel` | `string` | `"Copied"` | The copy button's name for a moment after copying. |
| `onCopy` | `(value: string) => void` |  | Runs after the key is copied. |
| `size` | `FieldSize` |  | The button height of the same name. |

Also accepts every native `<input>` attribute except `size`, `prefix`, `value`, `defaultValue`, `type`, `onCopy`, and a ref to it.

## Keyboard

- `Tab`: Moves to the show button, then the copy button.
- `Enter` + `Space`: Shows or hides the key, or copies it.

## Accessibility

- The show button says Show key or Hide key with aria-pressed; copy says Copy key, then Copied.

## Motion

- The eye swaps when the key shows; the copy icon draws its tick for 1.6s. Both are the locked animated icons.

## Responsive

- Fills its container's width; the label row and the message wrap.

## Performance

- Native typing: nothing renders per key beyond the value; the look is CSS in the foundation (section 8).

## Notes for AI

- <SecretField label="API key" value={key} revealIcon={<IconReveal />} copyIcon={<IconCopyCheck />} />.
- Read-only by design; every label is a prop (showLabel, hideLabel, copyLabel, copiedLabel).

## Related

- [Input](https://pixilui.com/components/input/markdown): A text field with its label, help and error, a unit or icon on either side, and a rule that checks on blur and Enter and says what's wrong in plain words.
- [Copy button](https://pixilui.com/components/copy-button/markdown): A button that copies a link, an ID or a snippet and says so: the icon swaps to a check that draws itself and the label to Copied, at a width that never moves.
