Secret input

Slant / Forms

A token or key field that hides its value until asked, with copy.

Demo

Usage

@tuttn/slant
import { Field, SecretInput } from '@tuttn/slant'

// In a Field, uncontrolled: masked, with a reveal toggle and a copy button.
<Field>
  <Field.Label>API key</Field.Label>
  <SecretInput name="key" defaultValue={savedKey} copyable />
</Field>

// A stored value shown once: read-only, and reveal and copy still work.
<SecretInput label="Deploy token" readOnly value={token} copyable />

// Controlled reveal, say to hide the value again when the dialog closes.
const [revealed, setRevealed] = useState(false)
<SecretInput label="Webhook secret" value={secret} onChange={event => setSecret(event.target.value)}
  revealed={revealed} onRevealedChange={setRevealed} />

Props

PropTypeDefaultDescription
revealedboolean—Whether the value shows as text. Pass it with onRevealedChange to control it; leave it out and the field keeps its own.
defaultRevealedbooleanfalseWhether an uncontrolled field starts with its value showing.
onRevealedChange(revealed: boolean) => void—Called with the new state when the reveal toggle is pressed.
copyablebooleanfalseAdds a copy button after the reveal toggle, which copies the current value, hidden or not.
labelstring—The accessible name, unless a Field's label or `aria-labelledby` names the field.
size'sm' | 'md' | 'lg''md'Height on the control scale: 28, 34 or 40px at regular density.
invalidboolean—Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this.
…restInputHTMLAttributes<HTMLInputElement>—The ref and every other attribute (name, value, defaultValue, onChange, placeholder, readOnly, aria-*, handlers) reach the <input>; className and style go on the group.

Keyboard

Tabmoves to the input, then the reveal toggle, then the copy button
Space / Enteron the reveal toggle, shows or hides the value; on the copy button, copies it

Accessibility

  • A native <input>: type="password" while the value is hidden, so screen readers read it as a protected field, and type="text" while it shows. It is named by a Field’s label, aria-labelledby, or label.
  • The reveal toggle is a button named “Show value” (messages.showValue) with aria-pressed: pressed while the value shows. The name stays the same in both states, as aria-pressed already says which it is in; only its icon changes, from the eye to the struck-through eye.
  • The copy button is an icon-only CopyButton named “Copy” (messages.copy), then “Copied” for a moment, and the root’s polite announcer says so. With no clipboard on the page it is aria-disabled while the value is hidden, and selects the shown text when pressed once it shows.
  • A press on either button leaves focus where it was, so a reader typing in the field keeps typing, and the caret and selection stay put when the value is shown or hidden.
  • Disabled, the input and both buttons are disabled. Read-only, the input is, and both buttons still work.

Masked, in monospace

The value is set in the monospace face, so a token's characters line up and 0, O, l and 1 tell apart once it shows. The input sets autoComplete="off" (unless you pass your own), spellCheck={false}, and turns off autocapitalise and autocorrect, so the browser neither offers saved passwords nor rewrites a key.

Copying

copyable adds a copy button that copies the input's current value, hidden or not, controlled or not. Browsers never copy out of a password field, so the input is the copy's fallback only while the value shows: then a refused copy selects the text for the reader to copy themselves. While hidden, a refused copy just says it failed.

On a page with no clipboard at all (served without HTTPS), the copy button is aria-disabled while the value is hidden, its tooltip saying copying isn't available here. Once the value shows it is pressable again: a press selects the input's text, and the tooltip says so.

Read-only values

For a value shown once, such as a token just created, pass readOnly and the value. The field takes the read-only surface, and reveal and copy work as usual.

It is drawn as an Input group, like Search field.