Inputs

OTP Field

A one-time code in boxes: a pasted code spreads itself, and Backspace on an empty box steps back.

  • input
  • otp
  • code
  • auth

Preview

1440pxHost theme

Use it with AI

  1. 1. Copy the link.
  2. 2. Write to your agent in your own words and drop the link into the sentence.
  3. 3. The agent opens the link and installs the component from the registry.

put this in the header: https://vibeui.ru/c/otp-001?lang=en

Code from SMS
Sent to +44 7700 000000. It should arrive within a minute
Show the full instructions

What the link says, spelled out. Use it when your agent cannot open links — paste this text instead.

# Install and place "otp-001" (OTP Field) from VibeUI

## 1. Install first — do not skip, do not recreate
Run this exact command before writing any code:
npx shadcn@latest add https://vibeui.ru/r/otp-001.json

Registry item: https://vibeui.ru/r/otp-001.json
Installs to: components/vibeui/otp-001.tsx (the exact path follows this project's components.json aliases).
npm dependencies: none.
Registry dependencies: none.

Install it from the registry. Do not recreate it from the description,
do not substitute a similar component from another library, and do not
rewrite it to match the project's existing style.

## 2. What it is
A one-time code in boxes: a pasted code spreads itself, and Backspace on an empty box steps back.

A confirmation code field: one digit per box, whole-code paste, focus moving forward and back, arrow keys. Zero dependencies, one file.

## 3. How to use it
import { Otp001 } from "@/components/vibeui/otp-001"

<Otp001 length={6} onChange={(code) => verify(code)} />

Read the installed file for the full prop list.

## 4. Where to place it
This is a small inline component. Put it exactly where the user asked,
inside the existing markup. Do not create a new page, section or
wrapper for it. If a similar control already sits in that spot,
replace it instead of adding a second one.

Placement: ___
(The user fills this line in. If it is still blank, ask where to put it
instead of guessing.)

## 5. Keep exactly as installed
- the local --vibeui-otp-001-* palette — do not swap it for your theme tokens
- whole-code paste: codes are copied from a message, nobody retypes them per box
- Backspace stepping back on an empty box — otherwise the code cannot be erased
- autocomplete="one-time-code" on the first box: phones fill it automatically
- inputMode="numeric" and "digit N of M" labels on every box
- left and right arrows between boxes

## 6. You may change
- length — how many digits
- label and hint
- the onChange handler
- the accent through the accent prop

## 7. Rules
- The component submits nothing: verify in onChange once the length matches.
- Do not mask the digits: the code is visible on the phone anyway and errors go unnoticed.
- Expiry and resend are a separate job, usually placed beside the field.
- Do not lift the CSS variables into globals.css: the component has to stay a single file.

## 8. Verify
- it renders with no console errors;
- it looks like the preview on the VibeUI page you copied this from;
- if it does not, you changed something listed in section 5 — put it back.

For developers

npx shadcn@latest add https://vibeui.ru/r/otp-001.json
https://vibeui.ru/r/otp-001.json

Компонент самодостаточен: один файл, без зависимостей, собственная палитра в локальных переменных --vibeui-otp-001-*. Клиентский: "use client". Клетки — массив ref, ввод цифры переводит фокус вперёд, Backspace на пустой клетке — назад, стрелки ходят по клеткам. Вставка перехватывается на любой клетке и раскладывается целиком. У первой клетки autocomplete="one-time-code".

Component source

The same file your agent installs. Here in case you would rather copy it by hand.

"use client"

import { useId, useRef, useState } from "react"
import type {
  ClipboardEvent,
  ComponentPropsWithoutRef,
  CSSProperties,
  KeyboardEvent,
} from "react"

export type Otp001Props = Omit<
  ComponentPropsWithoutRef<"div">,
  "children" | "onChange"
> & {
  label?: string
  length?: number
  hint?: string
  onChange?: (code: string) => void
  accent?: string
}

// Идея компонента: код из СМС по одной цифре в клетке. Главное здесь —
// вставка целиком: код почти всегда копируют, и поле обязано разложить его по
// клеткам само. Backspace на пустой клетке уводит назад, иначе стереть
// набранное невозможно.
const STYLES = `
:where([data-vibeui-block="otp-001"]){
--vibeui-otp-001-bg:oklch(1 0 0);
--vibeui-otp-001-fg:oklch(0.22 0.014 265);
--vibeui-otp-001-muted:oklch(0.56 0.014 265);
--vibeui-otp-001-border:oklch(0.88 0.008 265);
--vibeui-otp-001-field:oklch(0.985 0.002 265);
--vibeui-otp-001-accent:oklch(0.55 0.17 265);
--vibeui-otp-001-font:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
}
[data-vibeui-block="otp-001"]{
display:flex;flex-direction:column;gap:0.5rem;
width:100%;max-width:20rem;box-sizing:border-box;padding:0.875rem;
background:var(--vibeui-otp-001-bg);
border:1px solid var(--vibeui-otp-001-border);border-radius:0.875rem;
font-family:var(--vibeui-otp-001-font);color:var(--vibeui-otp-001-fg);
}
[data-vibeui-block="otp-001"] [data-part="label"]{font-size:0.8125rem;font-weight:600}
[data-vibeui-block="otp-001"] [data-part="row"]{display:flex;gap:0.375rem}
[data-vibeui-block="otp-001"] input{
flex:1;min-width:0;height:3rem;padding:0;box-sizing:border-box;
border:1.5px solid var(--vibeui-otp-001-border);border-radius:0.625rem;
background:var(--vibeui-otp-001-field);color:inherit;
font:inherit;font-size:1.125rem;font-weight:650;text-align:center;
font-variant-numeric:tabular-nums;
}
[data-vibeui-block="otp-001"] input:focus-visible{
outline:2px solid var(--vibeui-otp-001-accent);outline-offset:1px;border-color:transparent;
}
[data-vibeui-block="otp-001"] input:not(:placeholder-shown){border-color:var(--vibeui-otp-001-accent)}
[data-vibeui-block="otp-001"] [data-part="hint"]{font-size:0.75rem;line-height:1.4;color:var(--vibeui-otp-001-muted)}
@media (prefers-reduced-motion:reduce){[data-vibeui-block="otp-001"] *{animation:none!important;transition:none!important}}
`

/**
 * Код из СМС по клеткам: вставка целиком раскладывается сама.
 * Один файл, ноль зависимостей, собственная палитра.
 */
export function Otp001({
  label = "Код из СМС",
  length = 6,
  hint = "Отправили на +7 999 000-00-00. Код придёт в течение минуты",
  onChange,
  accent,
  className,
  style,
  ...props
}: Otp001Props) {
  const id = useId()
  const size = Math.max(4, Math.min(8, length))
  const [code, setCode] = useState<string[]>(Array(size).fill(""))
  const boxes = useRef<(HTMLInputElement | null)[]>([])

  const palette = {
    ...(accent ? { "--vibeui-otp-001-accent": accent } : null),
    ...style,
  } as CSSProperties

  const push = (next: string[]) => {
    setCode(next)
    onChange?.(next.join(""))
  }

  const type = (index: number, value: string) => {
    const digit = value.replace(/\D/g, "").slice(-1)
    const next = [...code]
    next[index] = digit
    push(next)
    if (digit && index < size - 1) boxes.current[index + 1]?.focus()
  }

  // Вставка целиком: код копируют из сообщения, и раскладывать его руками
  // по клеткам никто не станет.
  const paste = (event: ClipboardEvent<HTMLInputElement>) => {
    const digits = event.clipboardData.getData("text").replace(/\D/g, "")
    if (!digits) return
    event.preventDefault()
    const next = Array.from({ length: size }, (_, index) => digits[index] ?? "")
    push(next)
    boxes.current[Math.min(digits.length, size - 1)]?.focus()
  }

  const onKeyDown = (event: KeyboardEvent<HTMLInputElement>, index: number) => {
    if (event.key === "Backspace" && !code[index] && index > 0) {
      event.preventDefault()
      boxes.current[index - 1]?.focus()
      const next = [...code]
      next[index - 1] = ""
      push(next)
    } else if (event.key === "ArrowLeft" && index > 0) {
      boxes.current[index - 1]?.focus()
    } else if (event.key === "ArrowRight" && index < size - 1) {
      boxes.current[index + 1]?.focus()
    }
  }

  return (
    <>
      <style href="vibeui-otp-001" precedence="medium">
        {STYLES}
      </style>
      <div
        {...props}
        data-vibeui-block="otp-001"
        className={className}
        style={palette}
        role="group"
        aria-labelledby={`${id}-label`}
      >
        <span data-part="label" id={`${id}-label`}>
          {label}
        </span>
        <div data-part="row">
          {code.map((digit, index) => (
            <input
              key={index}
              ref={(node) => {
                boxes.current[index] = node
              }}
              type="text"
              inputMode="numeric"
              autoComplete={index === 0 ? "one-time-code" : "off"}
              maxLength={1}
              placeholder=" "
              value={digit}
              aria-label={`Цифра ${index + 1} из ${size}`}
              onChange={(event) => type(index, event.target.value)}
              onPaste={paste}
              onKeyDown={(event) => onKeyDown(event, index)}
            />
          ))}
        </div>
        {hint ? <span data-part="hint">{hint}</span> : null}
      </div>
    </>
  )
}