Components
Input OTP
One-time-code field with grouped, focus-aware slots.
Composition
Custom digit groups
Pass length and separator to generate the slots automatically, or compose indexed slots as children.
- <InputOTP>
- first digit group
- <InputOTPSlot />
- <InputOTPSeparator />
- remaining digits
- <InputOTPSlot />
- first digit group
Installation
The Control UI source installs this component from src/registry/sources/control-ui/ui/input-otp.tsx.
npx shadcn@latest add https://control-ui.dev/r/input-otp.jsonRaw code
Primary installed source
"use client";
import { OTPField as OTPFieldPrimitive } from "@base-ui/react/otp-field";import type { ComponentProps, CSSProperties, ReactNode } from "react";import { createContext, Fragment, useContext, useId } from "react";import type { FieldKnobStyle } from "@/components/control-ui/knob-contracts/field-knobs";import { cn } from "@/components/control-ui/lib/cn";
export type InputOTPProps = { length?: number; separator?: boolean; value?: string; defaultValue?: string; onValueChange?: (value: string) => void; onValueComplete?: (value: string) => void; validationType?: OTPFieldPrimitive.Root.Props["validationType"]; autoSubmit?: boolean; form?: string; disabled?: boolean; readOnly?: boolean; required?: boolean; name?: string; mask?: boolean; id?: string; className?: string; children?: ReactNode; "aria-label"?: string; "aria-labelledby"?: string; "aria-describedby"?: string; slotLabel?: InputOTPSlotLabel;} & { style?: CSSProperties & FieldKnobStyle };
export type InputOTPSlotLabel = (index: number, length: number | undefined) => string;
export type InputOTPSlotProps = { index: number; length?: number; disabled?: boolean; placeholder?: string; className?: string; "aria-label"?: string; style?: CSSProperties & FieldKnobStyle;};
export type InputOTPSeparatorProps = ComponentProps<"div"> & { style?: CSSProperties & FieldKnobStyle };
type InputOTPContextValue = { labelledBy?: string; slotLabel: InputOTPSlotLabel };
function defaultSlotLabel(index: number, length: number | undefined) { return length ? `Character ${index + 1} of ${length}` : `Character ${index + 1}`;}
const InputOTPContext = createContext<InputOTPContextValue>({ slotLabel: defaultSlotLabel });
// First slot takes the group's name, so a wrapping Field label or the group label is announced on entry.export function InputOTPSlot({ index, length, className, ...props }: InputOTPSlotProps) { const { labelledBy, slotLabel } = useContext(InputOTPContext); return ( <OTPFieldPrimitive.Input data-control-ui="input-otp" data-field-kind="input-otp" data-slot="slot" data-control-family="field" data-control="true" aria-label={index === 0 ? undefined : slotLabel(index, length)} aria-labelledby={index === 0 ? labelledBy : undefined} className={cn("relative flex items-center justify-center", className)} {...props} /> );}
export function InputOTPSeparator({ className, ...props }: InputOTPSeparatorProps) { return ( <OTPFieldPrimitive.Separator data-control-ui="input-otp" data-control-family="field" data-field-kind="input-otp" data-slot="separator" className={cn("shrink-0", className)} {...props} /> );}
export function InputOTP({ length = 6, separator = false, className, children, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, slotLabel = defaultSlotLabel, ...props}: InputOTPProps) { const labelId = useId(); const ownLabel = ariaLabel && !ariaLabelledBy ? ariaLabel : undefined; const labelledBy = ariaLabelledBy ?? (ownLabel ? labelId : undefined); return ( <InputOTPContext.Provider value={{ labelledBy, slotLabel }}> {ownLabel ? ( <span id={labelId} hidden> {ownLabel} </span> ) : null} <OTPFieldPrimitive.Root data-control-ui="input-otp" data-control-family="field" data-field-kind="input-otp" data-slot="root" length={length} aria-labelledby={labelledBy} className={cn("flex items-center", className)} {...props} > {children ?? Array.from({ length }, (_, index) => `input-otp-slot-${index}`).map((slotKey, index) => ( <Fragment key={slotKey}> {separator && index === Math.ceil(length / 2) ? <InputOTPSeparator /> : null} <InputOTPSlot index={index} length={length} /> </Fragment> ))} </OTPFieldPrimitive.Root> </InputOTPContext.Provider> );}
Knobs
Typed custom properties the recipe paints with. Set one on the root — style, a utility class, or a skin — and every slot inherits it.
| Knob | Syntax | Default |
|---|---|---|
--cui-field-label-radius | <length-percentage> | var(--radius-control) |
--cui-field-label-background | <color> | transparent |
--cui-field-label-hover-background | <color> | var(--hover-fill) |
--cui-field-label-checked-background | <color> | oklch(from var(--primary) l c h / 0.06) |
--cui-field-label-border-color | <color> | var(--border) |
--cui-field-label-checked-border-color | <color> | var(--primary) |
--cui-field-label-border-width | <length> | var(--control-rim-width) |
--cui-field-label-padding-block | <length-percentage> | calc(var(--spacing) * 3) |
--cui-field-label-padding-inline | <length-percentage> | calc(var(--spacing) * 3) |
--cui-field-legend-foreground | <color> | var(--foreground) |
--cui-field-legend-font-size | <length-percentage> | var(--text-label) |
--cui-field-legend-font-weight | <number> | var(--font-weight-semibold) |
--cui-field-radius | <length-percentage> | var(--nest-radius, var(--radius-control)) |
--cui-field-background | <color> | var(--control-fill) |
--cui-field-hover-background | <color> | var(--hover-fill) |
--cui-field-affordance-background | <color> | transparent |
--cui-field-affordance-shadow | * | 0 0 transparent |
--cui-field-affordance-foreground | <color> | var(--muted-foreground) |
--cui-field-affordance-hover-background | <color> | var(--hover-fill) |
--cui-field-foreground | <color> | var(--foreground) |
--cui-field-border-color | <color> | var(--control-rim) |
--cui-field-focus-border-color | <color> | var(--focus-rim) |
--cui-field-border-width | <length> | var(--control-rim-width) |
--cui-field-shadow | * | var(--shadow-inset) |
--cui-field-backdrop-filter | * | blur(0px) |
--cui-field-height | <length-percentage> | var(--control-h-md) |
--cui-field-padding-inline | <length-percentage> | var(--padding-x) |
--cui-field-font-size | <length-percentage> | var(--text-body) |
--cui-field-font-weight | <number> | var(--font-weight-normal) |
--cui-field-font-family | * | var(--font-body) |
--cui-field-group-radius | <length-percentage> | var(--radius-panel) |
--cui-field-group-background | <color> | var(--card) |
--cui-field-group-border-color | <color> | var(--border) |
--cui-field-group-border-width | <length> | 1px |
--cui-field-group-shadow | * | 0 0 transparent |
--cui-field-group-padding-block | <length-percentage> | calc(var(--spacing) * 3) |
--cui-field-group-padding-inline | <length-percentage> | calc(var(--spacing) * 4) |
--cui-field-group-separator-color | <color> | var(--border) |
--cui-field-group-separator-inset | <length-percentage> | 0px |
--cui-field-label-font-size | <length-percentage> | var(--text-label) |
--cui-field-label-font-weight | <number> | var(--font-weight-medium) |
--cui-field-description-font-size | <length-percentage> | var(--text-caption) |