Phone input
A phone number field with a searchable country picker, flags, grouped digits, paste detection and E.164 output, in joined, split and chip layouts, or without a dropdown.
Try pasting +254 712 345 678: the country follows.
E.164 appears here
Use PhoneInput wherever you ask for a phone number, especially for people in more than one country. It keeps the number as digits, shows it grouped, and reports it in E.164 form (+255712345678), which is what SMS and payment services expect. The country defaults to Tanzania (+255).
Presentation only
This component never stores, logs or sends the number anywhere. It reports the value through onValueChange; your own server decides what to do with it. It checks length only (8 to 15 digits), not whether a number exists. For real validation use a library such as libphonenumber on your server.
Installation
pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/phone-input.json
This also installs Input, Input group and Picker, which it is built on.
Usage
import { Field } from "@/components/ui/field";
import { PhoneInput } from "@/components/ui/phone-input";
<Field label="Phone number">
{(control) => (
<PhoneInput
{...control}
name="phone"
defaultCountry="TZ"
onValueChange={(e164, { valid }) => save(e164, valid)}
/>
)}
</Field>name adds a hidden field holding the E.164 number, so a plain HTML form submits the clean value rather than the grouped text on screen.
What it does for you
- Country picker. A searchable list: type a name, an ISO code or a dial code ("254" finds Kenya). Common countries come first.
- Paste-friendly. Pasting
+254 712 345 678or00254 712 345 678switches the country to Kenya and fills the number. Pasting a local number just fills it. - Grouped digits.
712345678shows as712 345 678. The caret stays after the digit you were typing. The grouping is plain threes for readability, not a national format. - One leading zero is dropped from the E.164 value (
0712…becomes+255712…), because in many countries it is only used inside the country. The screen still shows what was typed. Turn it off withstripLeadingZero={false}for countries where the zero stays. - Never too long. Input stops at 15 digits in total, the E.164 maximum.
Layouts
Three ways to arrange the field with a country dropdown. All work with every look below.
joined(default): the country button and the number share one control, with a divider.split: two separate fields side by side, when you want the country to stand on its own.chip: a saffron chip with just the dial code sits inside the field, the quietest of the three.
Without a dropdown
Most phone fields should offer the country dropdown, because people type numbers from different countries. When yours doesn't need it, there are two alternatives:
fixedshows the country's flag and code as plain text, set bydefaultCountry. It is not a control and is not in the tab order. Use it when everyone is in one country but the code should still be visible.plainis only the number field. Use it when the country is obvious from the page. The E.164 value still starts with thedefaultCountrycode.
<PhoneInput layout="fixed" defaultCountry="KE" />
<PhoneInput layout="plain" />In both, pasting a full international number is read only if it is for the same country; another country's number is left as pasted, since the country cannot change.
Looks and sizes
It takes the same variant (default, soft, underline, glass) and size as Input. showName adds the country's name beside the code.
Flags
Each country shows its flag, in the button and in the list. The flags are emoji: plain text made from two letters, so there are no images to ship, no requests to make and no artwork to license. They are drawn by the device's own emoji font.
Windows
Windows does not draw flag emoji. There the two letters of the country code show instead (for example "TZ"), which still tells people which country it is. If you need the same flags everywhere, supply images with renderFlag.
Prefer a neutral look? flags="code" shows the two-letter code in a small tile on every system.
To use your own flag artwork (images you have the right to use), draw it yourself:
<PhoneInput renderFlag={(iso) => <img src={`/flags/${iso}.svg`} alt="" width={20} height={14} />} />Your own countries
The built-in list is a starting point, not every country. Pass countries to add, remove or translate:
<PhoneInput
countries={[
{ iso: "TZ", name: "Tanzania", dial: "255" },
{ iso: "KE", name: "Kenya", dial: "254" },
]}
preferred={["TZ"]}
/>Where a dial code is shared (the US and Canada both use +1), list each country separately; pasting keeps the country you already have if it matches.
Props
PhoneInput extends native <input> props (except value, onChange, size and type) and puts them on the number field, including ref and id. className goes on the outer control.
| Prop | Type | Default | Description |
|---|---|---|---|
| value / defaultValue | string | – | The number in E.164 form, or an empty string. Controlled or uncontrolled. |
| onValueChange | (e164: string, details: { country: string; national: string; valid: boolean }) => void | – | Called on every change. `valid` is a length check only. |
| defaultCountry | string | "TZ" | ISO code shown first. |
| countries | Country[] | – | The list to offer: { iso, name, dial }. Defaults to a built-in list. |
| preferred | string[] | ["TZ","KE","UG","RW"] | ISO codes listed first, under Common. |
| layout | "joined" | "split" | "chip" | "fixed" | "plain" | "joined" | joined, split and chip have the country dropdown. fixed shows the code as plain text; plain is only the number. |
| variant | "default" | "soft" | "underline" | "glass" | "default" | The surface, as on Input. |
| size | "sm" | "md" | "lg" | "md" | The height, as on Input. |
| showName | boolean | false | Also show the country's name on the button. |
| flags | "emoji" | "code" | "emoji" | Mark each country with its flag emoji, or with its two-letter code in a tile. |
| renderFlag | (iso: string) => ReactNode | – | Draw your own mark for a country, such as a flag image. Overrides `flags`. |
| stripLeadingZero | boolean | true | Drop one leading 0 from the E.164 value. |
| halo | boolean | true | The soft grey glow around the focused field. Set false to keep only the border change. |
| invalid | boolean | – | Sets aria-invalid on the number and a danger border. Field sets this for you. |
| name | string | – | Adds a hidden field with the E.164 number for form submission. |
| countryLabel / searchLabel / emptyText | string | – | Words for the country button, its search box and the empty list. Translate them here. |
Accessibility
- The number is a native
<input type="tel">withinputmode="tel"andautocomplete="tel-national", so phones show the right keypad and browsers can fill it. Give it a label (see Field). - The country button is a labelled combobox that says what it shows, for example "Country: TZ +255, Tanzania", so the visible text is part of its name. The list is a listbox with a search box; arrow keys, Enter and Escape work as in Picker.
- The flag, the code tile and any image you supply are decorative and hidden from assistive technology; the country's name is in the option text. The button's name always contains the text written on it, such as "+255".
- The focus ring appears on the country button for keyboard users. The number field shows a single darker border and a soft halo when focused, around the whole control in joined and chip layouts.
- An invalid field keeps its red border while focused, and should also show an error message in text (use Field).
- Targets are at least 24 pixels; the chip is 28 pixels high, and the default country button is 40.
- The button and list use the
--duration-*tokens through Popover, which go to nearly zero under reduced motion.
Tokens used
The same as Input and Picker, plus --accent and --accent-foreground for the chip.
Known limitations
- Length check only. It does not know each country's number plan. Validate on your server.
- Generic grouping. Digits are grouped in threes, not in each country's own pattern.
- Flag emoji depend on the device. Windows shows letters instead of flags (see above).
- A starting list, not all countries. About 60 are built in; add yours with
countries. - Leading zero rule is global.
stripLeadingZeroapplies to every country in the list. - Not for extensions, or for numbers that are not international (short codes).
- It is a client component.
Source
Three files are copied: the component below, plus phone-number.ts (digit helpers) and countries.ts (the list). The component is shown here.
"use client";import { type ChangeEvent, type ClipboardEvent, type ComponentProps, type ReactNode, useEffect, useLayoutEffect, useMemo, useRef, useState,} from "react";import { Input, type InputProps } from "@/components/ui/input";import { InputGroup } from "@/components/ui/input-group";import { Picker, type PickerOption } from "@/components/ui/picker";import { cn } from "@/lib/utils";import { COUNTRIES, type Country } from "./countries";import { digitsOf, findByDial, flagEmoji, groupDigits, isPlausible, MAX_DIGITS, toE164,} from "./phone-number";export interface PhoneDetails { /** ISO code of the chosen country. */ country: string; /** The digits typed after the country code, exactly as typed (a leading 0 is kept). */ national: string; /** Whether the number has a plausible length. A length check only. */ valid: boolean;}export interface PhoneInputProps extends Omit< ComponentProps<"input">, "value" | "defaultValue" | "onChange" | "size" | "type" | "children" > { /** Controlled value in E.164 form, such as "+255712345678", or "" when empty. */ value?: string; /** Initial value when uncontrolled, in E.164 form. */ defaultValue?: string; /** Called with the E.164 number ("" while empty) and details about it. */ onValueChange?: (e164: string, details: PhoneDetails) => void; /** ISO code of the country shown first. Defaults to "TZ". */ defaultCountry?: string; /** The countries to offer. Defaults to a built-in list. */ countries?: readonly Country[]; /** ISO codes listed first under "Common". Defaults to TZ, KE, UG and RW. */ preferred?: readonly string[]; /** * "joined": the country button and the number share one control. "split": two separate fields. * "chip": a saffron chip with the dial code sits inside the field. These three have a country * dropdown. "fixed" has none: the country is `defaultCountry` and its code shows as plain text. * "plain" is only the number field, for when the country is obvious from the page. */ layout?: "joined" | "split" | "chip" | "fixed" | "plain"; variant?: InputProps["variant"]; size?: InputProps["size"]; /** Also show the country's name next to the code on the button. */ showName?: boolean; /** * What marks each country. "emoji" (the default) draws the flag as text, with no images or * requests; "code" shows the two-letter code in a small tile instead. */ flags?: "emoji" | "code"; /** Draws your own mark for an ISO code, such as a flag image. Overrides `flags`. */ renderFlag?: (iso: string) => ReactNode; /** Whether a leading 0 is dropped from the E.164 output. Defaults to true. */ stripLeadingZero?: boolean; invalid?: boolean; /** The soft grey glow around the focused field. Defaults to true; false keeps just the border. */ halo?: boolean; /** Submitted with a form: a hidden field holds the E.164 number under this name. */ name?: string; /** Names the country button. Defaults to "Country". */ countryLabel?: string; /** Names the country search box. Defaults to "Search countries". */ searchLabel?: string; emptyText?: string; /** Classes for the outer control. */ className?: string;}const chevron = ( <svg aria-hidden="true" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="size-3.5 shrink-0 opacity-70" > <path d="M4 6l4 4 4-4" /> </svg>);const triggerHeight = { sm: "min-h-8", md: "min-h-10", lg: "min-h-12" } as const;const DEFAULT_PREFERRED = ["TZ", "KE", "UG", "RW"] as const;/** * A phone number field with a country picker. It keeps the number as digits, shows it grouped, and * reports it in E.164 form. Pasting a full international number (+255 712 345 678) switches the * country for you. * * It checks length only: for real validation of a number plan, use a library such as * libphonenumber on your server. It never stores or sends the number anywhere itself. */export function PhoneInput({ value, defaultValue, onValueChange, defaultCountry = "TZ", countries = COUNTRIES, preferred = DEFAULT_PREFERRED, layout = "joined", variant, size: sizeProp, showName = false, flags = "emoji", renderFlag, stripLeadingZero = true, invalid, halo, name, countryLabel = "Country", searchLabel = "Search countries", emptyText = "No country found", className, disabled, placeholder, ref, onPaste, ...props}: PhoneInputProps) { const size = sizeProp ?? "md"; const byIso = useMemo(() => new Map(countries.map((c) => [c.iso, c])), [countries]); const fallback = byIso.get(defaultCountry) ?? countries[0]; if (!fallback) throw new Error("PhoneInput needs at least one country."); const start = useMemo(() => { const found = defaultValue ? findByDial(digitsOf(defaultValue), countries, defaultCountry) : null; return { iso: found?.country.iso ?? fallback.iso, national: found?.national ?? "" }; }, [defaultValue, countries, defaultCountry, fallback.iso]); const [iso, setIso] = useState(start.iso); const [national, setNational] = useState(start.national); const country = byIso.get(iso) ?? fallback; const pickerEnabled = layout !== "fixed" && layout !== "plain"; const inputRef = useRef<HTMLInputElement | null>(null); const lastEmitted = useRef(toE164(country, national, stripLeadingZero)); const caret = useRef<number | null>(null); // A controlled value that did not come from this field's own last change replaces what is shown. // The check keeps a typed leading 0 on screen even though the E.164 echo drops it. useEffect(() => { if (value === undefined || value === lastEmitted.current) return; const found = value ? findByDial(digitsOf(value), countries, iso) : null; lastEmitted.current = value; if (found) { setIso(found.country.iso); setNational(found.national); } else if (!value) setNational(""); }, [value, countries, iso]); // After the digits are regrouped, put the caret back after the same digit it was after. useLayoutEffect(() => { const el = inputRef.current; if (caret.current === null || !el || document.activeElement !== el) return; const wanted = caret.current; caret.current = null; let seen = 0; let at = 0; while (at < el.value.length && seen < wanted) { if (/\d/.test(el.value[at] ?? "")) seen++; at++; } el.setSelectionRange(at, at); }); const maxNational = (c: Country) => MAX_DIGITS - c.dial.length + (stripLeadingZero ? 1 : 0); function commit(next: Country, digits: string) { const clipped = digits.slice(0, maxNational(next)); setIso(next.iso); setNational(clipped); const e164 = toE164(next, clipped, stripLeadingZero); lastEmitted.current = e164; onValueChange?.(e164, { country: next.iso, national: clipped, valid: isPlausible(e164) }); } function onChange(event: ChangeEvent<HTMLInputElement>) { const el = event.target; caret.current = digitsOf(el.value.slice(0, el.selectionStart ?? el.value.length)).length; commit(country, digitsOf(el.value)); } function handlePaste(event: ClipboardEvent<HTMLInputElement>) { onPaste?.(event); if (event.defaultPrevented) return; const text = event.clipboardData.getData("text").trim(); if (!/^(\+|00)/.test(text)) return; // "+255 712…" and "00255 712…" both mean the same thing: the digits after the prefix. const all = digitsOf(text); const found = findByDial(text.startsWith("00") ? all.slice(2) : all, countries, iso); if (!found) return; // Without a dropdown the country cannot change, so only a number for this country is read. if (!pickerEnabled && found.country.dial !== country.dial) return; event.preventDefault(); commit(found.country, found.national); } const e164 = toE164(country, national, stripLeadingZero); const options: PickerOption[] = useMemo(() => { const pinned = preferred.filter((p, i) => byIso.has(p) && preferred.indexOf(p) === i); const make = (c: Country, group: string): PickerOption => ({ value: c.iso, label: c.name, group, keywords: [c.iso, c.dial, `+${c.dial}`], }); return [ ...pinned.map((p) => make(byIso.get(p) as Country, "Common")), // Each country appears once, so an option's value is also its unique id. ...countries.filter((c) => !pinned.includes(c.iso)).map((c) => make(c, "All countries")), ]; }, [countries, preferred, byIso]); const mark = (c: Country) => renderFlag ? ( <span aria-hidden="true" className="inline-flex shrink-0 items-center"> {renderFlag(c.iso)} </span> ) : flags === "emoji" ? ( <span aria-hidden="true" className="inline-flex w-5 shrink-0 items-center justify-center text-base leading-none [font-family:'Apple_Color_Emoji','Segoe_UI_Emoji','Noto_Color_Emoji',sans-serif]" > {flagEmoji(c.iso)} </span> ) : ( <span aria-hidden="true" className="inline-flex h-5 min-w-7 shrink-0 items-center justify-center rounded-sm border border-border bg-muted px-1 text-[0.65rem] font-semibold leading-none text-foreground" > {c.iso} </span> ); const chip = layout === "chip"; // What is written on the button, so the spoken name always contains it. Flags are decorative. const visibleText = chip || flags === "emoji" || renderFlag ? `+${country.dial}` : `${country.iso} +${country.dial}`; const picker = ( <Picker aria-label={`${countryLabel}: ${visibleText}, ${country.name}`} options={options} value={iso} onValueChange={(next) => { const c = byIso.get(next); if (c) commit(c, national); }} searchable searchLabel={searchLabel} emptyText={emptyText} panelLabel={countryLabel} disabled={disabled} icon={chevron} panelClassName="w-72" className={cn( layout === "split" ? cn("w-auto min-w-[9.5rem]", triggerHeight[size]) : // joined: a segment flush with the field's edge. It runs the full height, has a clean // divider on its far side, and tints softly on hover. The group clips it to the rounded // corners, so the focus outline is drawn inside it. "h-auto min-h-0 w-auto gap-1.5 self-stretch rounded-none border-0 border-e border-border bg-transparent ps-3 pe-2.5 text-sm text-foreground hover:bg-foreground/5 aria-expanded:bg-foreground/5 focus-visible:outline-offset-[-2px]", chip && "rounded-full bg-accent px-2.5 py-0.5 text-xs font-medium text-accent-foreground hover:bg-accent hover:opacity-90", )} renderValue={(o) => { const c = byIso.get(o.value) as Country; return ( <span className="inline-flex items-center gap-1.5"> {chip ? null : mark(c)} <span className="tabular-nums">+{c.dial}</span> {showName && !chip ? <span className="hidden truncate sm:inline">{c.name}</span> : null} </span> ); }} renderOption={(o) => { const c = byIso.get(o.value) as Country; return ( <span className="flex min-w-0 flex-1 items-center gap-2.5"> {mark(c)} <span className="truncate">{c.name}</span> <span className="ms-auto ps-2 tabular-nums text-muted-foreground">+{c.dial}</span> </span> ); }} /> ); const numberProps = { ref: (node: HTMLInputElement | null) => { inputRef.current = node; if (typeof ref === "function") ref(node); else if (ref) ref.current = node; }, type: "tel", inputMode: "tel" as const, autoComplete: "tel-national", value: groupDigits(national), onChange, onPaste: handlePaste, disabled, placeholder, "aria-invalid": invalid || props["aria-invalid"] || undefined, ...props, }; // Pasting and typing both end here, so the form gets one clean value. const hidden = name ? <input type="hidden" name={name} value={e164} /> : null; if (layout === "plain") { return ( <> <Input {...numberProps} variant={variant} size={size} halo={halo} className={className} /> {hidden} </> ); } if (layout === "split") { return ( <div className={cn("flex w-full items-start gap-2", className)}> {picker} <Input {...numberProps} variant={variant} size={size} halo={halo} className="flex-1" /> {hidden} </div> ); } return ( <InputGroup variant={variant} size={size} halo={halo} // The segment sits flush against the left edge, so the group gives up its own padding there. className={cn(!chip && "ps-0 overflow-hidden", className)} start={ layout === "fixed" ? ( // No dropdown: the code is plain text, so it is not a control and not in the tab order. <span className="inline-flex items-center gap-1.5 self-stretch border-e border-border ps-3 pe-2.5 text-sm text-foreground"> {mark(country)} <span className="tabular-nums">+{country.dial}</span> </span> ) : ( picker ) } > <input {...numberProps} /> {hidden} </InputGroup> );}