Tone OTP inputs
Five one-time-code inputs that keep the same slot geometry and vary only the semantic tone: info, success, warning, danger and muted. The empty border, the filled border and fill, and the focus ring all read from the matching token. Each is self-contained, sized, auto-advances on typing, and handles paste and Backspace.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/input-otp-toneDependencies, the @ai2/tokens theme and the component file are installed together.
Copy the source
components/ui/input-otp-tone.tsx"use client"
import * as React from "react"
import { cn } from "@/lib/utils"
/* Tone OTP family: 5 semantically toned one-time-code inputs (info, success,
warning, danger, muted). The difference is ONLY the color: the empty cell border,
the filled cell border/fill and the focus ring all come from the relevant token.
Every cell is a real <input maxLength=1 inputMode=numeric>; typing a digit moves
focus forward, Backspace clears and moves back, and pasting fills every cell.
Color comes ONLY from tokens, via alpha color-mix. No animation.
Self-sufficient: NO input-otp package and NO radix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
type OtpProps = {
className?: string
size?: StyledSize
length?: number
value?: string
defaultValue?: string
onValueChange?: (v: string) => void
}
const cellSize: Record<StyledSize, string> = {
sm: "size-8 text-sm",
md: "size-10 text-base",
lg: "size-12 text-lg",
xl: "size-14 text-xl",
}
const cellBase =
"bg-transparent text-center font-medium tabular-nums text-foreground outline-none transition-colors placeholder:text-muted-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50"
const rootBase = "inline-flex items-center gap-2"
/* Kontrollu/kontrolsuz OTP durumu + ileri/geri odak, yapistirma. */
function useOtpCode(
length: number,
{ value, defaultValue, onValueChange }: Pick<OtpProps, "value" | "defaultValue" | "onValueChange">
) {
const isControlled = value !== undefined
const [internal, setInternal] = React.useState<string>(() => (defaultValue ?? "").slice(0, length))
const current = (isControlled ? value : internal) ?? ""
const chars = React.useMemo(() => {
const arr: string[] = Array(length).fill("")
for (let i = 0; i < length; i++) arr[i] = current[i] ?? ""
return arr
}, [current, length])
const refs = React.useRef<Array<HTMLInputElement | null>>([])
const commit = (next: string) => {
if (!isControlled) setInternal(next)
onValueChange?.(next)
}
const setAt = (i: number, raw: string) => {
const ch = raw.slice(-1)
const arr = [...chars]
arr[i] = ch
commit(arr.join(""))
if (ch && i < length - 1) refs.current[i + 1]?.focus()
}
const onKey = (i: number, e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Backspace") {
const arr = [...chars]
if (chars[i]) {
arr[i] = ""
commit(arr.join(""))
} else if (i > 0) {
arr[i - 1] = ""
commit(arr.join(""))
refs.current[i - 1]?.focus()
}
e.preventDefault()
} else if (e.key === "ArrowLeft" && i > 0) {
refs.current[i - 1]?.focus()
} else if (e.key === "ArrowRight" && i < length - 1) {
refs.current[i + 1]?.focus()
}
}
const onPaste = (i: number, e: React.ClipboardEvent<HTMLInputElement>) => {
e.preventDefault()
const text = e.clipboardData.getData("text").replace(/\s+/g, "")
if (!text) return
const arr = [...chars]
for (let k = 0; k < text.length && i + k < length; k++) arr[i + k] = text[k]
commit(arr.join(""))
const last = Math.min(i + text.length, length - 1)
refs.current[last]?.focus()
}
return { chars, refs, setAt, onKey, onPaste }
}
type Controller = ReturnType<typeof useOtpCode>
function cellHandlers(ctl: Controller, i: number) {
return {
ref: (el: HTMLInputElement | null) => {
ctl.refs.current[i] = el
},
value: ctl.chars[i],
onChange: (e: React.ChangeEvent<HTMLInputElement>) => ctl.setAt(i, e.target.value),
onKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => ctl.onKey(i, e),
onPaste: (e: React.ClipboardEvent<HTMLInputElement>) => ctl.onPaste(i, e),
inputMode: "numeric" as const,
autoComplete: i === 0 ? ("one-time-code" as const) : ("off" as const),
maxLength: 1,
"aria-label": `Digit ${i + 1}`,
}
}
type Tone = "info" | "success" | "warning" | "danger" | "muted"
/* Per tone: an empty border, a filled border plus a soft fill, and the focus ring. */
const toneEmpty: Record<Tone, string> = {
info: "border-[color-mix(in_oklab,var(--color-info)_35%,transparent)]",
success: "border-[color-mix(in_oklab,var(--color-success)_35%,transparent)]",
warning: "border-[color-mix(in_oklab,var(--color-warning)_35%,transparent)]",
danger: "border-[color-mix(in_oklab,var(--color-danger)_35%,transparent)]",
muted: "border-field-border",
}
const toneFilled: Record<Tone, string> = {
info: "border-info bg-[color-mix(in_oklab,var(--color-info)_12%,transparent)]",
success: "border-success bg-[color-mix(in_oklab,var(--color-success)_12%,transparent)]",
warning: "border-warning bg-[color-mix(in_oklab,var(--color-warning)_12%,transparent)]",
danger: "border-danger bg-[color-mix(in_oklab,var(--color-danger)_12%,transparent)]",
muted: "border-muted-foreground bg-muted",
}
const toneFocus: Record<Tone, string> = {
info: "focus-visible:border-info focus-visible:ring-info/40",
success: "focus-visible:border-success focus-visible:ring-success/40",
warning: "focus-visible:border-warning focus-visible:ring-warning/40",
danger: "focus-visible:border-danger focus-visible:ring-danger/40",
muted: "focus-visible:border-ring focus-visible:ring-ring/50",
}
/* Shared shell: the only difference is the tone. */
function ToneOtp({ tone, className, size = "md", length = 6, value, defaultValue, onValueChange }: OtpProps & { tone: Tone }) {
const ctl = useOtpCode(length, { value, defaultValue, onValueChange })
return (
<div
data-slot="styled-input-otp"
data-tone={tone}
role="group"
aria-label="Verification code"
className={cn(rootBase, className)}
>
{ctl.chars.map((ch, i) => (
<input
key={i}
{...cellHandlers(ctl, i)}
className={cn(
cellBase,
cellSize[size],
"rounded-md border",
toneFocus[tone],
ch ? toneFilled[tone] : toneEmpty[tone]
)}
/>
))}
</div>
)
}
/* Info: the info tone, for a neutral verification flow. */
export function InfoOtp(props: OtpProps) {
return <ToneOtp tone="info" {...props} />
}
/* Success: onaylanmis bir kod girisinin tonu. */
export function SuccessOtp(props: OtpProps) {
return <ToneOtp tone="success" {...props} />
}
/* Warning: dikkat isteyen bir kod girisinin tonu. */
export function WarningOtp(props: OtpProps) {
return <ToneOtp tone="warning" {...props} />
}
/* Danger: the tone of an incorrect or risky code entry. */
export function DangerOtp(props: OtpProps) {
return <ToneOtp tone="danger" {...props} />
}
/* Muted: sessiz, notr token tonu. */
export function MutedOtp(props: OtpProps) {
return <ToneOtp tone="muted" {...props} />
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Info
The info tone for a neutral verification flow.
import { InfoOtp } from "@/components/ui/input-otp-tone"
<InfoOtp />Success
The success tone for an accepted code.
import { SuccessOtp } from "@/components/ui/input-otp-tone"
<SuccessOtp />Warning
The warning tone for a code that needs attention.
import { WarningOtp } from "@/components/ui/input-otp-tone"
<WarningOtp />Danger
The danger tone for a rejected or risky code.
import { DangerOtp } from "@/components/ui/input-otp-tone"
<DangerOtp />Muted
A quiet neutral tone that stays out of the way.
import { MutedOtp } from "@/components/ui/input-otp-tone"
<MutedOtp />ai2 Tone OTP inputs: 5 styled variations on the token system
The ai2 Tone OTP inputs are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around one-time-code entry in a semantic tone. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: no animation is used; only the tone changes. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, nothing changes because there is no motion to skip.
What is in the ai2 Tone OTP inputs?
5 exports in one file: Info, Success, Warning, Danger and Muted. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: no animation is used; only the tone changes.
- Reduced-motion aware: Under prefers-reduced-motion, nothing changes because there is no motion to skip.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Tone OTP inputs sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.