Phone input
Country picker and a number that formats.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add phone-inputOr install the package
Don’t want the file? Import from
asta-ui/phone-inputand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { PhoneInput } from "asta-ui/phone-input";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useId, useLayoutEffect, useRef, useState, type ChangeEvent, type KeyboardEvent } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); const KEYFRAMES = `@keyframes asta-phone-in { from { opacity: 0; transform: translateY(-4px) scale(.98) } } @keyframes asta-phone-ok { from { opacity: 0; transform: scale(.4) } } @media (prefers-reduced-motion: reduce) { .asta-phone-a { animation: none !important } }`; export interface PhoneCountry { /** ISO 3166 code, e.g. "ID". */ code: string; name: string; /** Calling code without the plus, e.g. "62". */ dial: string; /** National format, one # per digit, e.g. "###-####-####". */ mask: string; } /** A starter list. Pass your own `countries` for more. */ export const PHONE_COUNTRIES: PhoneCountry[] = [ { code: "US", name: "United States", dial: "1", mask: "(###) ###-####" }, { code: "ID", name: "Indonesia", dial: "62", mask: "###-####-####" }, { code: "GB", name: "United Kingdom", dial: "44", mask: "#### ######" }, { code: "DE", name: "Germany", dial: "49", mask: "### ########" }, { code: "FR", name: "France", dial: "33", mask: "# ## ## ## ##" }, { code: "NL", name: "Netherlands", dial: "31", mask: "# ########" }, { code: "SG", name: "Singapore", dial: "65", mask: "#### ####" }, { code: "MY", name: "Malaysia", dial: "60", mask: "##-### ####" }, { code: "JP", name: "Japan", dial: "81", mask: "##-####-####" }, { code: "AU", name: "Australia", dial: "61", mask: "### ### ###" }, { code: "IN", name: "India", dial: "91", mask: "#####-#####" }, { code: "BR", name: "Brazil", dial: "55", mask: "(##) #####-####" }, { code: "CA", name: "Canada", dial: "1", mask: "(###) ###-####" }, { code: "AE", name: "United Arab Emirates", dial: "971", mask: "## ### ####" }, ]; const slots = (mask: string) => mask.split("").filter((c) => c === "#").length; /** Fits digits into a mask. Stops at the last digit, so a half-typed number has no trailing separators. */ export function formatPhone(digits: string, mask: string) { let out = ""; let i = 0; for (const c of mask) { if (i >= digits.length) break; if (c === "#") out += digits[i++]; else out += c; } return out + digits.slice(i); } export interface PhoneValue { /** E.164, e.g. "+6281234567890". Empty when there are no digits. */ e164: string; country: PhoneCountry; /** Every slot of the mask is filled. */ valid: boolean; } export interface PhoneInputProps { countries?: PhoneCountry[]; /** ISO code of the starting country. */ defaultCountry?: string; /** Starting national digits. */ defaultValue?: string; onChange?: (value: PhoneValue) => void; label?: string; hint?: string; /** An error from outside, e.g. on submit. It replaces the hint. Without it, an unfinished number shows an error on blur. */ error?: string; name?: string; disabled?: boolean; className?: string; } /** * A phone number field with a country picker. The number formats itself as you type and the caret stays put. * Paste a full number with its +code and the country switches to match. A check pops in once the number is * complete. The picker filters as you type and works with the arrow keys. */ export function PhoneInput({ countries = PHONE_COUNTRIES, defaultCountry = "US", defaultValue = "", onChange, label = "Phone number", hint, error, name = "phone", disabled, className, }: PhoneInputProps) { const id = useId(); const [country, setCountry] = useState(() => countries.find((c) => c.code === defaultCountry) ?? countries[0]); const [digits, setDigits] = useState(() => defaultValue.replace(/\D/g, "").slice(0, slots(country.mask))); const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const [hi, setHi] = useState(0); // Set on blur when the number is unfinished, cleared as soon as it is complete (or empty) again. const [short, setShort] = useState(false); const input = useRef<HTMLInputElement>(null); const search = useRef<HTMLInputElement>(null); const wrap = useRef<HTMLDivElement>(null); const caret = useRef<number | null>(null); const listId = `${id}-list`; const max = slots(country.mask); const valid = digits.length === max; const shown = formatPhone(digits, country.mask); const q = query.trim().toLowerCase().replace(/^\+/, ""); const results = q ? countries.filter((c) => c.name.toLowerCase().includes(q) || c.code.toLowerCase() === q || c.dial.startsWith(q)) : countries; const emit = (d: string, c: PhoneCountry) => { if (short && (!d || d.length === slots(c.mask))) setShort(false); onChange?.({ e164: d ? `+${c.dial}${d}` : "", country: c, valid: d.length === slots(c.mask) }); }; const message = error || (short ? `Enter all ${max} digits, like ${country.mask.replace(/#/g, "0")}.` : ""); const describedBy = message ? `${id}-err` : hint ? `${id}-hint` : undefined; // Put the caret back after the same number of digits it followed before formatting. useLayoutEffect(() => { const el = input.current; if (!el || caret.current === null) return; let seen = 0; let pos = 0; while (pos < shown.length && seen < caret.current) { if (/\d/.test(shown[pos])) seen++; pos++; } el.setSelectionRange(pos, pos); caret.current = null; }, [shown]); useEffect(() => { if (!open) return; search.current?.focus(); const down = (e: PointerEvent) => { if (!wrap.current?.contains(e.target as Node)) setOpen(false); }; document.addEventListener("pointerdown", down); return () => document.removeEventListener("pointerdown", down); }, [open]); const onInput = (e: ChangeEvent<HTMLInputElement>) => { const raw = e.target.value; // A pasted international number: pick the country from the longest matching calling code. if (raw.trim().startsWith("+")) { const all = raw.replace(/\D/g, ""); const match = [...countries].sort((a, b) => b.dial.length - a.dial.length).find((c) => all.startsWith(c.dial)); if (match) { const d = all.slice(match.dial.length).slice(0, slots(match.mask)); setCountry(match); setDigits(d); caret.current = d.length; emit(d, match); return; } } const before = raw.slice(0, e.target.selectionStart ?? raw.length).replace(/\D/g, "").length; const d = raw.replace(/\D/g, "").slice(0, max); caret.current = Math.min(before, d.length); setDigits(d); emit(d, country); }; const pick = (c: PhoneCountry) => { const d = digits.slice(0, slots(c.mask)); setCountry(c); setDigits(d); setOpen(false); setQuery(""); emit(d, c); input.current?.focus(); }; const onListKey = (e: KeyboardEvent) => { if (e.key === "ArrowDown" || e.key === "ArrowUp") { e.preventDefault(); const n = results.length; if (n) setHi((h) => (h + (e.key === "ArrowDown" ? 1 : -1) + n) % n); } else if (e.key === "Enter") { e.preventDefault(); if (results[hi]) pick(results[hi]); } else if (e.key === "Escape") { e.preventDefault(); setOpen(false); input.current?.focus(); } }; // Keep the highlighted country in view. useEffect(() => { if (open) document.getElementById(`${id}-opt-${hi}`)?.scrollIntoView({ block: "nearest" }); }, [hi, open, id]); return ( <div ref={wrap} className={cx("relative flex w-full flex-col gap-1.5", className)}> <style href="asta-phone-input" precedence="asta">{KEYFRAMES}</style> <label htmlFor={`${id}-num`} className="text-[13px] font-medium text-ink"> {label} </label> <div className="relative"> <div className={cx( "flex h-10 items-center rounded-lg bg-paper shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_14%,transparent)] transition-shadow duration-200", message && "!shadow-[inset_0_0_0_1.5px_var(--color-ink)]", "focus-within:shadow-[inset_0_0_0_1px_var(--color-brand),0_0_0_3px_color-mix(in_srgb,var(--color-brand)_18%,transparent)]", disabled && "pointer-events-none opacity-50", )} > <button type="button" aria-haspopup="listbox" aria-expanded={open} aria-label={`Country: ${country.name} +${country.dial}`} onClick={() => { setHi(Math.max(0, countries.indexOf(country))); setOpen((o) => !o); }} className="flex h-full shrink-0 items-center gap-1.5 rounded-l-lg pl-3 pr-2 text-[13.5px] text-ink outline-none transition-colors hover:bg-paper-2 focus-visible:bg-paper-2" > <span className="rounded-[4px] bg-paper-2 px-1 font-mono text-[10.5px] font-medium leading-[18px] text-ink-2 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_8%,transparent)]">{country.code}</span> <span className="tabular-nums text-ink-2">+{country.dial}</span> <svg viewBox="0 0 24 24" className={cx("h-3 w-3 text-mute transition-transform duration-200", open && "rotate-180")} fill="none" stroke="currentColor" strokeWidth="2.7" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="m6 9.5 6 6 6-6" /> </svg> </button> <span aria-hidden className="h-5 w-px bg-[color-mix(in_srgb,var(--color-ink)_10%,transparent)]" /> <input ref={input} id={`${id}-num`} type="tel" inputMode="tel" autoComplete="tel-national" name={name} value={shown} onChange={onInput} onBlur={() => setShort(digits.length > 0 && !valid)} disabled={disabled} placeholder={country.mask.replace(/#/g, "0")} aria-invalid={!!message || undefined} aria-describedby={describedBy} className="h-full min-w-0 flex-1 bg-transparent px-3 text-[16px] sm:text-[14px] tabular-nums text-ink outline-none placeholder:text-mute" /> <span className="flex w-9 shrink-0 justify-center text-brand"> {valid && ( <svg viewBox="0 0 24 24" className="asta-phone-a h-4 w-4 animate-[asta-phone-ok_.35s_cubic-bezier(.16,1,.3,1)_both]" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" role="img" aria-label="Complete number"> <path d="m5 12.5 4.5 4.5L19 7.5" /> </svg> )} </span> </div> {open && ( <div className="asta-phone-a absolute left-0 top-full z-50 mt-1.5 flex w-full max-w-[320px] origin-top-left animate-[asta-phone-in_.18s_cubic-bezier(.16,1,.3,1)_both] flex-col rounded-[11px] bg-paper p-1 shadow-[0_0_0_1px_color-mix(in_srgb,var(--color-ink)_9%,transparent),0_14px_34px_-12px_rgb(0_0_0/0.28)]"> <input ref={search} type="search" autoComplete="off" spellCheck={false} value={query} onChange={(e) => { setQuery(e.target.value); setHi(0); }} onKeyDown={onListKey} role="combobox" aria-expanded="true" aria-controls={listId} aria-activedescendant={results[hi] ? `${id}-opt-${hi}` : undefined} aria-label="Search countries" placeholder="Search country or code…" className="mb-1 h-9 w-full rounded-[7px] bg-paper-2 px-2.5 text-[16px] sm:text-[13px] text-ink outline-none placeholder:text-mute [&::-webkit-search-cancel-button]:hidden" /> <div id={listId} role="listbox" aria-label="Countries" className="max-h-[220px] overflow-y-auto overscroll-contain"> {results.length === 0 && <p className="px-2.5 py-3 text-[12.5px] text-mute">No country matches “{query}”</p>} {results.map((c, i) => ( <div key={c.code} id={`${id}-opt-${i}`} role="option" aria-selected={c.code === country.code} onPointerMove={() => hi !== i && setHi(i)} onClick={() => pick(c)} className={cx("flex h-8 cursor-default items-center gap-2.5 rounded-[7px] px-2 text-[13px]", hi === i ? "bg-paper-2 text-ink" : "text-ink-2")} > <span className="w-6 font-mono text-[10.5px] text-mute">{c.code}</span> <span className="flex-1 truncate">{c.name}</span> <span className="tabular-nums text-mute">+{c.dial}</span> {c.code === country.code && ( <svg viewBox="0 0 24 24" className="h-3.5 w-3.5 text-brand" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="m5 12.5 4.5 4.5L19 7.5" /> </svg> )} </div> ))} </div> </div> )} </div> {message ? ( <p id={`${id}-err`} role="alert" className="flex items-start gap-1.5 text-[12px] text-ink-2"> <svg viewBox="0 0 24 24" className="mt-px h-3.5 w-3.5 shrink-0" fill="none" stroke="currentColor" strokeWidth="2.25" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <circle cx="12" cy="12" r="8.5" /> <path d="M12 7.5v5" /> <path d="M12 16h.01" /> </svg> {message} </p> ) : ( hint && ( <p id={`${id}-hint`} className="text-[12px] text-mute"> {hint} </p> ) )} {name && <input type="hidden" name={`${name}_e164`} value={digits ? `+${country.dial}${digits}` : ""} />} </div> ); } export default PhoneInput;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Phone input component to my project. Country picker and a number that formats. It includes: country picker, search, mask, paste detect, E.164, valid check, keys. Install it: ```sh pnpm dlx asta-ui@latest add phone-input --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/phone-input.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { Button } from "@/components/astabyte/button"; import { PhoneInput, type PhoneValue } from "@/components/astabyte/phone-input"; ``` Usage example and props: https://ui.astabyte.com/phone-input Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<!-- asta.js formats the number with the selected country's data-mask, fills phone_e164 and sets data-valid when it is complete. --> <form style="display:grid;gap:12px;max-width:340px" onsubmit="return false"> <div class="asta-phone" data-asta-phone> <label class="asta-label" for="ex-phone">Phone number</label> <div class="asta-phone-control"><select class="asta-phone-country" name="country" aria-label="Country" autocomplete="tel-country-code"><option value="+1" data-mask="(###) ###-####">US +1</option><option value="+44" data-mask="#### ######">GB +44</option><option value="+62" data-mask="###-####-####" selected>ID +62</option><option value="+65" data-mask="#### ####">SG +65</option><option value="+49" data-mask="#### #######">DE +49</option><option value="+81" data-mask="##-####-####">JP +81</option></select><input id="ex-phone" class="asta-phone-input" type="tel" inputmode="tel" autocomplete="tel-national" name="phone" placeholder="812-3456-7890"><span class="asta-phone-check" aria-hidden="true"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m4 8.5 2.5 2.5L12 5.5"/></svg></span></div> <input type="hidden" name="phone_e164"> </div> <button type="submit" class="asta-btn asta-btn-accent" style="width:100%"><span class="asta-btn-label">Send code</span></button> </form>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { Button } from "@/components/astabyte/button";
import { PhoneInput, type PhoneValue } from "@/components/astabyte/phone-input";
export default function PhoneInputDemo() {
const [value, setValue] = useState<PhoneValue | null>(null);
const [sent, setSent] = useState(false);
const [error, setError] = useState("");
return (
<div className="flex min-h-[460px] w-full items-start justify-center p-6 pt-12 sm:p-10 sm:pt-14">
<form
className="flex w-full max-w-[380px] flex-col gap-5"
noValidate
onSubmit={(e) => {
e.preventDefault();
if (value?.valid) return setSent(true);
// The button stays enabled: an unfinished number gets a message and focus instead.
setError(value?.e164 ? "Finish the number, then send the code." : "Enter your phone number first.");
(e.currentTarget.elements.namedItem("phone") as HTMLInputElement | null)?.focus();
}}
>
<div>
<p className="text-[18px] font-medium tracking-[-0.02em] text-ink">Turn on two-step sign-in</p>
<p className="mt-1 text-[13px] leading-snug text-mute">We text a six-digit code to this number each time you sign in to Astabyte.</p>
</div>
<PhoneInput
defaultCountry="ID"
hint="Try pasting +44 7700 900123"
name="phone"
error={error}
onChange={(v) => {
setValue(v);
setSent(false);
setError("");
}}
/>
<div className="flex items-center justify-between gap-3">
<p className="min-w-0 truncate text-[12px] tabular-nums text-mute" aria-live="polite">
{sent ? `Code sent to ${value?.e164}` : value?.e164 ? `${value.e164}${value.valid ? "" : " · keep typing"}` : "No number yet"}
</p>
<Button type="submit" size="sm" variant="accent">
Send code
</Button>
</div>
</form>
</div>
);
}