Boxes for a verification code that take a pasted or autofilled code whole, with Backspace across boxes.
Check your email
We sent a 6-digit code to [email protected]. It expires in 10 minutes.
Verification code
1 of 62 of 63 of 64 of 65 of 66 of 6
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Redacted, Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { OneTimeCode } from "./ui/uicontext/components/OneTimeCode.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ card: { display: "grid", gap: space.xl, inlineSize: "min(100%, 24rem)", padding: space.xxl, borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, }, title: { margin: 0, fontSize: font.lg, fontWeight: 600, letterSpacing: "-0.01em", color: color.fg }, text: { margin: 0, fontSize: font.sm, color: color.muted, overflowWrap: "anywhere" }, done: { margin: 0, fontSize: font.sm, color: color.success }, resend: { justifySelf: "start" },})const Message = UiSchema.TaggedEnum({ UpdatedCode: { code: Schema.Redacted(Schema.String) }, EnteredCode: { code: Schema.Redacted(Schema.String) }, SucceededCheckCode: {}, FailedCheckCode: { reason: Schema.String }, ClickedResend: {},})// stands in for the server: 424242 is the code it sentconst CheckCode = Command.make("Example.CheckCode", { args: { code: Schema.Redacted(Schema.String) } }, ({ code }) => Effect.sleep("600 millis").pipe( Effect.as(Redacted.value(code) === "424242" ? Message.SucceededCheckCode() : Message.FailedCheckCode({ reason: "That code isn't right. Check the latest email, or send a new one." })), ))export const VerifyEmail = Html.component({ name: "VerifyEmail", model: Schema.Struct({ code: Schema.Redacted(Schema.String), state: Schema.Literals(["typing", "checking", "verified"]), error: Schema.NullOr(Schema.String), }), message: Message,}).make({ init: () => ({ code: Redacted.make(""), state: "typing", error: null }), update: { // typing again clears the last error UpdatedCode: ($, { code }) => $.evolve({ code, error: null }), EnteredCode: ($, { code }) => $.evolve({ state: "checking" }).run(CheckCode.make({ code })), SucceededCheckCode: ($) => $.evolve({ state: "verified" }), // a wrong code clears the boxes so the next one goes straight in FailedCheckCode: ($, { reason }) => $.evolve({ state: "typing", error: reason, code: Redacted.make("") }), ClickedResend: ($) => $.evolve({ code: Redacted.make(""), error: null, state: "typing" }), }, view: (h, { code, state, error }) => h.div.attrs(sx(styles.card)).children( h.h2.attrs(sx(styles.title)).children("Check your email"), h.p.attrs(sx(styles.text)).children("We sent a 6-digit code to [email protected]. It expires in 10 minutes."), OneTimeCode(h, { label: "Verification code", hideLabel: true, value: code, readOnly: state !== "typing", ...(error === null ? {} : { error }), ...(state === "checking" ? { hint: "Checking…" } : {}), onChange: (next) => Message.UpdatedCode({ code: next }), onComplete: (next) => Message.EnteredCode({ code: next }), }), state === "verified" ? h.p.attrs({ role: "status", ...sx(styles.done) }).children("Email verified.") : Button(h, { emphasis: "ghost", size: "sm", style: styles.resend }).on("click", Message.ClickedResend()).children("Send a new code"), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/one-time-codeUsage
import { OneTimeCode } from "./ui/uicontext/components/OneTimeCode.ts"const Message = UiSchema.TaggedEnum({ UpdatedCode: { code: Schema.Redacted(Schema.String) }, EnteredCode: { code: Schema.Redacted(Schema.String) },})export const CodeField = (h: Html.H<typeof Message.Type>, code: Redacted.Redacted<string>) => OneTimeCode(h, { label: "Verification code", value: code, onChange: (next) => Message.UpdatedCode({ code: next }), onComplete: (next) => Message.EnteredCode({ code: next }), })Examples
Letters, masked and disabled
Backup code
1 of 82 of 83 of 84 of 85 of 86 of 87 of 88 of 8
One of the ten codes you saved when you turned on two-step sign-in.
PIN
1 of 42 of 43 of 44 of 4
Code
1 of 62 of 63 of 64 of 65 of 66 of 6
Send a new code to try again.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Redacted, Schema } from "effect"import { OneTimeCode } from "./ui/uicontext/components/OneTimeCode.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ stack: { display: "grid", gap: 28 } })const Message = UiSchema.TaggedEnum({ UpdatedCode: { field: Schema.String, code: Schema.Redacted(Schema.String) } })// a backup code has letters, a sign-in PIN is four masked digits, and a disabled field waits for a resendexport const CodeKinds = Html.component({ name: "CodeKinds", model: Schema.Struct({ backup: Schema.Redacted(Schema.String), pin: Schema.Redacted(Schema.String) }), message: Message,}).make({ init: () => ({ backup: Redacted.make("K7Q"), pin: Redacted.make("") }), update: { // backup codes are case-blind, so they're stored in capitals UpdatedCode: ($, { field, code }) => field === "backup" ? $.evolve({ backup: Redacted.make(Redacted.value(code).toUpperCase()) }) : $.evolve({ pin: code }), }, view: (h, { backup, pin }) => h.div.attrs(sx(styles.stack)).children( OneTimeCode(h, { key: "backup", label: "Backup code", value: backup, length: 8, kind: "alphanumeric", hint: "One of the ten codes you saved when you turned on two-step sign-in.", onChange: (code) => Message.UpdatedCode({ field: "backup", code }), }), OneTimeCode(h, { key: "pin", label: "PIN", value: pin, length: 4, mask: true, groupSize: 0, onChange: (code) => Message.UpdatedCode({ field: "pin", code }) }), OneTimeCode(h, { key: "waiting", label: "Code", value: Redacted.make(""), disabled: true, hint: "Send a new code to try again.", onChange: (code) => Message.UpdatedCode({ field: "none", code }) }), ),})API reference
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the field among others in the same view; needed for more than one. |
labelrequired | string | — | What the code is: "Verification code". It names the boxes, "Verification code 2 of 6". |
hideLabel | boolean | — | Hide the label but keep it as the boxes' name, when a heading above says what to type. |
valuerequired | Redacted.Redacted<string> | — | The code so far. The caller keeps it and stores what onChange asks for. |
length | number | 6 | How many characters; 6 by default. |
kind | "numeric" | "alphanumeric" | — | Digits only (numeric, the default, with the number keypad on a phone) or letters and digits. |
groupSize | number | — | A gap after every this many boxes, as "123–456"; half the length by default, 0 for none. |
mask | boolean | — | Show dots instead of the characters, for a code read aloud in a shared space. |
hint | string | — | A line under the boxes: "We sent it to [email protected]". |
error | string | — | What's wrong: "That code has expired". Marks every box invalid and is announced. |
disabled | boolean | — | — |
readOnly | boolean | — | Showing a code that's being checked: the boxes can't change. |
name | string | — | The form field the whole code submits under. |
onChangerequired | (value: Redacted.Redacted<string>) => Message | — | — |
onComplete | (value: Redacted.Redacted<string>) => Message | — | Every box is filled: check the code here. |
style | Style | — | StyleX styles applied after the field's own. |