Secret field
FreeA 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.
npx shadcn@latest add https://pixilui.com/r/secret-field.jsonPreview
Show the key, copy it, and the tick says it worked.
Size
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>
);
}When to use
Use it when
- API keys, webhook secrets and tokens in settings.
Use something else when
- A password a person types: use Input with type="password".
Installation
Terminal
npx shadcn@latest add https://pixilui.com/r/secret-field.jsonAdds secret-field.tsx with field.tsx, and installs clsx and tailwind-merge. The URL works in any app with a components.json, with no registry to add first.
components/pixilui/secret-field.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>
);
});
It uses Field: add it the same way. Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
example.tsx
import { SecretField } from "@/components/pixilui/secret-field";
<SecretField
value={value}
revealIcon={revealIcon}
copyIcon={copyIcon}
onCopy={onCopy}
/>Fills its container's width; the label row and the message wrap.
API reference
PropTypeDefault
valueRequiredstring""The key or token.revealIconRequiredReactNode–The show button's icon (the animated reveal icon: its eye swaps on reveal).copyIconRequiredReactNode–The copy button's icon (the animated copy-check icon: it draws its tick).labelReactNode–The label above the field.descriptionReactNode–Help under the field ("Only admins can rotate it").iconReactNode–The icon before the key (the animated key icon).showLabelstring"Show key"The show button's name while the key is hidden.hideLabelstring"Hide key"The show button's name while the key shows.copyLabelstring"Copy key"The copy button's name.copiedLabelstring"Copied"The copy button's name for a moment after copying.onCopy(value: string) => void–Runs after the key is copied.sizeFieldSize–The button height of the same name.classNamestring–Classes for the field. Native attributes and the ref go to the input.Also takes every native <input> attribute except size, prefix, value, defaultValue, type, onCopy, and a ref.
Keyboard
TabMoves to the show button, then the copy button.
Enter SpaceShows 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.