Passcode field
A row of one-character boxes for a verification code that advances as digits are typed and fills on paste.
Verification code
Two-Factor Authentication
Enter the code sent to your other devices.
"use client"
import { PasscodeField } from "@applecn/ui/components/passcode-field"
import { Text } from "@applecn/ui/components/text"
export default function PasscodeFieldBasic() {
return (
<div className="flex flex-col items-center gap-4 text-center">
<Text variant="headline">Two-Factor Authentication</Text>
<Text variant="footnote" color="label-2">
Enter the code sent to your other devices.
</Text>
<PasscodeField aria-label="Verification code" length={6} />
</div>
)
}Install
From the registry this site publishes, with dependencies resolved by the CLI.
npx shadcn@latest add https://applecn.vercel.app/r/passcode-field.jsonSource
packages/ui/src/components/passcode-field.tsx
"use client"
import { OTPField as OTPFieldPrimitive } from "@base-ui/react/otp-field"
import { cn } from "cn"
import { useId } from "react"
/**
* A passcode field: the row of one-character boxes for a verification code or passcode, as
* in Apple ID's two-factor prompt. Each box is the platform's bordered text field; typing
* advances, Backspace retreats, and pasting fills the row.
*
* `aria-label` names the field through a hidden `<label>`, which is what Base UI reads: it
* names the group and the first box, and the boxes after it say which digit they are.
*/
type PasscodeFieldProps = Omit<OTPFieldPrimitive.Root.Props, "length"> & {
"aria-label": string
className?: string
length?: number
}
function PasscodeField({
className,
"aria-label": label,
id: idProp,
length = 6,
...props
}: PasscodeFieldProps) {
const generatedId = useId()
// Base UI names the field, and its first box, from an associated `<label>` — it ignores
// `aria-label` on that box, which otherwise leaves it unnamed. The label carries the name
// for both, so the root takes neither `role` nor `aria-label` of its own.
const id = idProp ?? generatedId
return (
<OTPFieldPrimitive.Root
id={id}
data-slot="passcode-field"
length={length}
inputMode="numeric"
className={cn("flex items-center gap-2", className)}
{...props}
>
<label htmlFor={id} className="sr-only">
{label}
</label>
{Array.from({ length }, (_, i) => (
<OTPFieldPrimitive.Input
key={i}
data-slot="passcode-field-input"
aria-label={`Digit ${i + 1}`}
className="h-(--alert-button-height) w-10 rounded-field border-[0.5px] border-separator bg-background-3 text-center text-[length:var(--control-font-large)] font-medium text-label transition-[box-shadow] duration-(--duration-hover) outline-none focus-visible:ring-4 focus-visible:ring-ring/60 macos:h-(--control-height-large) macos:w-8 macos:shadow-control web:border web:border-label-4"
/>
))}
</OTPFieldPrimitive.Root>
)
}
export { PasscodeField }
export type { PasscodeFieldProps }