Skip to content
MIZU.

OneTimeCode

A one-time code, one digit to a cell, that fills itself from the message it came in.

View .md

Live — this one is real

This demo accepts 424242.

Usage

tsx
"use client";

import { useState } from "react";
import { OneTimeCode } from "mizu-ui";

export function Example() {
  const [code, setCode] = useState("");
  const wrong = code.length === 6 && code !== "424242";
  return (
    <>
      <OneTimeCode
        label="Sign-in code"
        hint="This demo accepts 424242."
        value={code}
        onValueChange={setCode}
        error={
          wrong
            ? "That code doesn’t match. Check the email and try again."
            : undefined
        }
      />
      {code === "424242" && (
        <p role="status" className="mizu-field-hint">
          Signed in.
        </p>
      )}
    </>
  );
}

It starts with "use client" because it keeps state or passes functions to components, so it pastes into a Next.js page as it is. Other React setups ignore the directive.

Source & setup

Add this component to your project with the shadcn CLI. The command copies its source into your configured UI directory, with base.css, Mizu’s tokens, and the stylesheets that hold its rules. Each rule lives in one stylesheet, so components you add together never repeat CSS. In a project without a components.json, run npx shadcn@latest init 0xuser64bit/mizu/preset first: plain init installs shadcn’s theme into your global CSS. Then load the fonts.

sh
npx shadcn@latest add 0xuser64bit/mizu/one-time-code

Every component imports from its own path, its name in kebab case. To paste the example, replace its mizu-ui import with:

tsx
import { OneTimeCode } from "@/components/ui/mizu/one-time-code";

For npm imports, import mizu-ui/styles.css once. Fonts are optional via mizu-ui/fonts.css. Override --mizu-* tokens or use className for local styling. Uses native React and CSS; install the package peers for root imports. A Client Component: its source starts with “use client”.

Inspect forms/OneTimeCode.tsx

Source is included in the npm package under src/forms/OneTimeCode.tsx.

Open full source ↗

Props

PropTypeDefaultDescription
label / hint / errorstring—Visible label, help text and validation feedback, linked to the input.
value / defaultValue / onValueChangestring / string / (code: string) => void—Controlled or initial code. Anything that isn't a digit is dropped, so pasted codes with spaces or dashes arrive clean.
onComplete(code: string) => void—Called whenever the code reaches its full length, typed, pasted or filled in by the platform.
lengthnumber6How many digits, from 1 to 12.
name / required / disabled / autoFocusstring / boolean—Native form name, validation and states. A partial code fails native validation.

Accessibility

One real text input with autocomplete=one-time-code and a numeric keyboard, so SMS and email autofill, paste and assistive technology work as they do anywhere. The cells are drawn for sight only; the input is labelled, described and reads as the whole code.

Motion

The active cell takes the accent; the caret blinks there unless motion is reduced.

Works alongside