Masked Value
A secret drawn with most of it hidden, such as an API key, a token or a card number, from the parts the client is allowed to have.
Usage
import { MaskedValue } from 'nyctoui/masked-value'
<MaskedValue end="d8af" /> // ••••••••d8af
<MaskedValue start="sk_live_" /> // sk_live_••••••••
<MaskedValue start="sk_" end="d8af" /> // sk_••••••••d8af
<MaskedValue end="4242" variant="card" /> // •••• •••• •••• 4242The secret itself never reaches the browser. The server sends what may be shown, such as a key’s prefix or a card’s last four digits, and MaskedValue draws the mask between start and end. The mask’s length is a picture, not the secret’s real length.
It is text only, so it sits in a sentence, a table cell or a title. Put a CopyButton or other controls beside it when a row needs them. Screen readers skip the mask characters and read maskLabel in their place, so sk_••••••••d8af reads as “sk_ hidden d8af”.
Good for
- API keys and tokens in a developer settings table, showing only the prefix.
- A saved card on a billing page, showing the last four digits.
- A bank account or IBAN in a payouts screen.
- A webhook signing secret after it was created.
Examples
Shapes
Pass end, start or both. Keys use monospace, so the mask and the readable characters share one grid. variant="card" uses the text face with even-width digits, as billing pages do.
Mask
maskLength sets how many mask characters are drawn and maskChar which one.
Groups
groupSize splits the whole text, readable parts included, into groups counted from the start, and groupSeparator sets what goes between them. Choose a maskLength that makes the total a multiple of the group size, or the last group comes up short.
Reveal and copy
MaskedValue has no reveal of its own: the full key is not on the client. Ask the server for it when the user presses the eye, show it with maskLength={0} so it keeps the same face, and drop it again on hide. Copy waits until the key is there. The request here is a stand-in that resolves after 600ms.
Card on file
Inside a title, the value takes the title’s weight and color.
In a table
As a string
maskValue takes the same options and returns plain text, for a toast, a title or a log line. maskRuns returns the readable and masked stretches, to draw them some other way.
Best practices
- Send only the parts the browser may show, such as a prefix or the last four digits. The mask is drawn, not a hidden copy of the secret.
- Set
maskLabelso screen readers hear something useful, such as “key ending d8af”. - Put a copy button beside it only when the full value is safe to hand over, such as just after creation.
- Load the whole secret into the page and mask it with CSS. Anyone can read it in the page source.
- Make the mask’s length match the secret’s. It should be a picture, not a hint.
API reference
MaskedValue
Renders a <span> with data-variant.
| Prop | Type | Default |
|---|---|---|
startReadable characters before the mask, such as a key’s prefix. | string | — |
endReadable characters after the mask, such as the last four digits. | string | — |
variantcode is monospace; card uses the text face, 12 mask characters and groups of four. | 'code' | 'card' | 'code' |
maskLengthHow many mask characters to draw. | number | 8, or 12 for card |
maskCharDrawn for each hidden character. | string | '•' |
groupSizeSplits the whole text into groups of this many characters, from the start. 0 turns it off. | number | 4 for card, else 0 |
groupSeparatorPlaced between groups. | string | ' ' |
maskLabelRead by screen readers in place of the mask characters. | string | 'hidden' |
renderReplaces the span. | ReactElement | (props) => ReactElement | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
maskValue
maskValue(options) returns the masked text as a string. maskRuns(options) returns it as { text, masked } runs.
| Prop | Type | Default |
|---|---|---|
startReadable characters before the mask, such as a key’s prefix. | string | — |
endReadable characters after the mask, such as the last four digits. | string | — |
variantcode is monospace; card uses the text face, 12 mask characters and groups of four. | 'code' | 'card' | 'code' |
maskLengthHow many mask characters to draw. | number | 8, or 12 for card |
maskCharDrawn for each hidden character. | string | '•' |
groupSizeSplits the whole text into groups of this many characters, from the start. 0 turns it off. | number | 4 for card, else 0 |
groupSeparatorPlaced between groups. | string | ' ' |