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'
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
| CopyField | Type | Default |
|---|---|---|
valuerequired | string | |
label | string | |
once | boolean | |
secret | boolean |