Skip to content

SecretField

Shows a secret, such as an API key, with its middle hidden. An eye shows it, a button copies it, and a warning says when it will not be shown again.

import { CopyField } from '@sypnia/ui/react'

On this page
  1. Hidden until shown
  2. Shown only once
  3. When to use it
  4. For everyone
  5. Props

Hidden until shown

The secret key hides its middle; the public key does not need to.

For your servers only. Never put it in a web page or an app.

Safe to use in the browser: it only reads what your rules allow.

Code
import { CopyField, Field, FormGrid } from '@sypnia/ui/react';

// The middle is hidden until you press the eye. Copy always copies the real value.
export default function Masked() {
  return (
    <FormGrid>
      <Field label="Secret key" hint="For your servers only. Never put it in a web page or an app.">
        <CopyField secret value="sk_live_9f3a7c21d4e8b6a05c1f7e2d" />
      </Field>
      <Field label="Public key" hint="Safe to use in the browser: it only reads what your rules allow.">
        <CopyField value="pk_live_2b8d4f6a1c3e5g7i" />
      </Field>
    </FormGrid>
  );
}

Shown only once

Press «Create key»: the key appears with its warning in a dialog that only its button closes.

Code
import { useState } from 'react';
import { Actions, Button, CopyField, Dialog, Field, Input } from '@sypnia/ui/react';

// Press «Create key»: the key appears once, with the warning, and closing by accident is not possible.
export default function ShownOnce() {
  const [name, setName] = useState('Back office');
  const [key, setKey] = useState<string | null>(null);
  const create = () => setKey(`sk_live_${Array.from({ length: 24 }, () => '0123456789abcdef'[Math.floor(Math.random() * 16)]).join('')}`);
  return (
    <div className="sy-stack">
      <Field label="Key name"><Input value={name} onChange={(e) => setName(e.target.value)} /></Field>
      <Actions align="start"><Button variant="primary" icon="key" onClick={create}>Create key</Button></Actions>
      <Dialog open={key !== null} onClose={() => setKey(null)} dismissable={false} title={`The key «${name}» is ready`}
        footer={<Actions><Button variant="primary" onClick={() => setKey(null)}>I have copied it</Button></Actions>}>
        <Field label="Secret key"><CopyField secret once value={key ?? ''} /></Field>
      </Dialog>
    </div>
  );
}

When to use it

Use it when

  • A key, token or password that the system created and the person must store somewhere safe.
  • The secret is shown only at the moment it is created: add once, inside a Dialog that cannot be closed by accident.

Do not use it when

  • The person types the secret: that is PasswordInput.
  • The value is not secret (a public key, an ID): a plain CopyField is easier to check.
  • You would show the full secret every time the page opens. Show it once; afterwards show only its last characters.

For everyone

  • The eye is a toggle button named «Show» or «Hide». Copy always copies the real value, hidden or not.
  • The warning «Copy it now» is text with an icon, not colour alone.
  • When shown once, the dialog closes only with its button, and that button says what the person did: «I have copied it».

Props

CopyFieldTypeDefault
valuerequired string
label string
once boolean
secret boolean
  1. Hidden until shown
  2. Shown only once
  3. When to use it
  4. For everyone
  5. Props

    Search pages, sections and terms