Phone Input

A design-system-native international phone input that pairs a compact country calling-code picker with a formatted tel field. It formats as the user types, infers the country from the number, and reports E.164 plus validity metadata through onValueChange.

blockcountriesphoneformsregistry-item.json
Phone Input
LIVE · running in a sandboxed iframe
Installed with plain shadcn add · no workarounds · theme neutral (none shipped)
See how it was built

Installation

pnpm dlx shadcn@latest add @flagcn/phone-input

Requires @flagcn in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import * as React from "react"import { PhoneInput } from "@/components/flags/phone-input"export function PhoneField() {  const [phone, setPhone] = React.useState("")  return <PhoneInput value={phone} onValueChange={(value) => setPhone(value)} aria-label="Phone number" />}
  • Collecting international phone numbers in sign-up, checkout, or contact forms where the user may enter numbers from any country.
  • Needing the value stored as E.164 (for example +14155550123) while showing a human-friendly national or international format.
  • Requiring validity feedback (possible and valid flags) without writing your own libphonenumber logic.
  • Building a form that should match the existing shadcn Input styling and sizes inside a design system.

Examples

Controlled with E.164 output

Stores the E.164 value (or the formatted string when it cannot be parsed) in React state. Because the value is controlled, the country is inferred from the number itself.

controlled-with-e.164-output.tsx
import * as React from "react"import { PhoneInput } from "@/components/flags/phone-input"export function ContactPhone() {  const [phone, setPhone] = React.useState("")  return (    <PhoneInput      value={phone}      onValueChange={(value) => setPhone(value)}      aria-label="Phone number"    />  )}

Uncontrolled with default country and validity check

Starts with Japan selected and reads the meta object to show a validation message once the user leaves the field.

uncontrolled-with-default-country-and-validity-check.tsx
import * as React from "react"import { PhoneInput, type PhoneValueMeta } from "@/components/flags/phone-input"export function JapanPhone() {  const [valid, setValid] = React.useState(true)  return (    <div className="grid gap-2">      <PhoneInput        defaultCountry="jp"        displayFormat="international"        aria-label="Phone number"        aria-invalid={!valid}        onValueChange={(_value, meta: PhoneValueMeta) => setValid(meta.valid || meta.nationalNumber === undefined)}      />      {!valid && <p className="text-destructive text-sm">Enter a valid phone number.</p>}    </div>  )}

Size variants

Demonstrates the sm, default, and lg sizes side by side, matching other form controls in a compact toolbar.

size-variants.tsx
import { PhoneInput } from "@/components/flags/phone-input"export function PhoneSizes() {  return (    <div className="flex flex-col gap-3">      <PhoneInput size="sm" aria-label="Phone (small)" />      <PhoneInput size="default" aria-label="Phone (default)" />      <PhoneInput size="lg" aria-label="Phone (large)" />    </div>  )}

API reference

PropTypeDefaultDescription
valuestring—Controlled display value. When provided, the component does not keep its own value state and the country is inferred from it when possible.
defaultValuestring""Initial value for uncontrolled usage. Its country is inferred when parseable.
countryPhoneCountryCode—Controlled country code (lowercase ISO 3166-1 alpha-2, such as "us" or "jp"). Takes precedence over inference and internal state.
defaultCountryPhoneCountryCode"us"Initial country for uncontrolled usage when the default value does not imply one.
onCountryChange(country: PhoneCountryCode) => void—Called when the country changes via the picker or when typing a number infers a different country.
onValueChange(value: string, meta: PhoneValueMeta) => void—Called on every change. value is the E.164 string when parseable, otherwise the formatted display string. meta contains country, formatted, e164, nationalNumber, countryCallingCode, international, possible, and valid.
countriesreadonly PhoneCountryCode[]phoneCountryCodesRestricts the countries shown in the picker.
countryPickerLabelstring"Country calling code"Accessible label applied to the country picker trigger.
displayFormat"national" | "international""national"Format used when rewriting the displayed number after a country change or a value update.
size"sm" | "default" | "lg""default"Controls height, padding, and text size of the picker and input. Sets data-size on the wrapper.
inputClassNamestring—Additional classes applied to the inner input element.
classNamestring—Additional classes applied to the outer wrapper div, which carries the border and focus ring.
disabledboolean—Disables both the country picker and the input; the wrapper gets reduced opacity.
readOnlyboolean—Makes the input read-only and disables the country picker.
aria-labelstring—Accessible name forwarded to the input.
aria-invalidboolean—Marks the input invalid and switches the wrapper to destructive styling.
aria-describedbystring—Forwarded to the input to link helper or error text.
aria-errormessagestring—Forwarded to the input to link an error message element.

Accessibility

  • The input uses type="tel", inputMode="tel", and autoComplete="tel" so mobile keyboards and browser autofill behave correctly.
  • Always provide an accessible name: pass aria-label or associate a visible label with the input via htmlFor and id, since the input itself is not labeled by default.
  • The country picker trigger is labeled by countryPickerLabel (default "Country calling code"); override it to match your locale or context.
  • Set aria-invalid={true} together with aria-describedby pointing at the error message so screen readers announce the problem; the wrapper's destructive styling is driven by aria-invalid on the input.
  • readOnly disables the country picker so keyboard users cannot change a fixed country, and disabled removes both controls from the tab order.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Phone Input" component (flagcn/phone-input) from its shadcn registry.1. Install it with: npx shadcn@latest add @flagcn/phone-input2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: value, defaultValue, country, defaultCountry, onCountryChange, onValueChange, countries, countryPickerLabel, displayFormat, size, inputClassName, className, disabled, readOnly, aria-label, aria-invalid, aria-describedby, aria-errormessage.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport * as React from "react"import { PhoneInput } from "@/components/flags/phone-input"export function PhoneField() {  const [phone, setPhone] = React.useState("")  return <PhoneInput value={phone} onValueChange={(value) => setPhone(value)} aria-label="Phone number" />}```

Files & dependencies

  • src/components/flags/phone-input.tsx→ @components/flags/phone-input.tsx
dependencieslibphonenumber-js
registryDependenciesinput@flagcn/country-picker

Looks similar, elsewhere