Xzero UI
Pro library

Phone input

A phone field with a country picker, formatting as you type, and E.164 output.

$ pnpm dlx shadcn@4.23.0 add @xzero-ui/phone-input
Live · keyboard ready

When to use

When not to use

Installation

Register both Xzero UI namespaces in your project's components.json:

{
  "registries": {
    "@xzero-ui": "https://mcp.xzero.co.uk/r/{name}.json",
    "@xzero-ui-pro": "https://mcp.xzero.co.uk/r/pro/{name}.json"
  }
}
npx shadcn@4.23.0 add @xzero-ui/phone-input

Install xzero-foundation once, and import @/components/xzero/foundation.css in your app root. No Xzero token is used by this snapshot. For sample portraits, photos, fonts, or logos, download https://mcp.xzero.co.uk/install-assets.mjs and run node xzero-install-assets.mjs <receiving-project>.

Usage

import { useState } from "react";
import { PhoneInput } from "@/components/xzero/phone-input/phone-input";

export function ContactPhone() {
  const [phone, setPhone] = useState("");
  const [valid, setValid] = useState(false);
  return (
    <PhoneInput
      label="Phone number"
      name="phone"
      value={phone}
      onValueChange={(value, details) => { setPhone(value); setValid(details.valid); }}
      defaultCountry="GB"
      preferredCountries={["GB", "IE", "US"]}
      description={valid ? undefined : "We only text about your order"}
    />
  );
}

API reference

PhoneInput

A phone number field with a searchable country picker that grows out of the flag button, formatting as you type and returning E.164.

PropTypeDefaultDescription
label (required)string–Field label.
hideLabelbooleanfalseKeeps the label for screen readers only.
valuestring–Controlled number in E.164, such as "+14155550132". An empty string clears the field.
defaultValuestring–Starting number when uncontrolled.
onValueChange(value: string, details: PhoneInputDetails) => void–Fires on every edit with the E.164 number (empty without digits) and { country, formatted, status, valid }.
countrystring–Controlled ISO code of the selected country.
defaultCountrystring"US"ISO code used until someone picks a country or enters an international number.
onCountryChange(iso: string) => void–Called when the country changes, including from a pasted number.
countriesstring[]–Limit the picker to these ISO codes.
preferredCountriesstring[]["US", "CA", "GB"]Pinned at the top of the picker under Suggested.
descriptionstring–Hint under the field.
errorstring–Replaces the built-in validation message.
validatebooleantrueShow a message after blur when the number has the wrong length.
disabledbooleanfalseDisables the number and the picker.
requiredboolean–Marks the number input required.
namestring–Adds a hidden input carrying the E.164 value for native form submission.
idstring–Id of the number input.
classNamestring–Class on the root.
onBlur(event: FocusEvent<HTMLInputElement>) => void–Called when the number input loses focus.
refRef<HTMLInputElement>–Forwarded to the number input.

parsePhoneNumber / formatPhoneNumber / formatNational

Helpers: parse "+44 (0)7911…" or "0044…" into { country, national }, format E.164 for display, or format national digits for a country.

PropTypeDefaultDescription
input (required)string–parsePhoneNumber(input, pool?) returns { country, national } or null.
e164 (required)string–formatPhoneNumber(e164) returns "+44 7400 123456", or the input when it can not be read.
entry, digits (required)PhoneCountry, string–formatNational(entry, digits) formats national digits, keeping a typed trunk prefix.

PHONE_COUNTRIES / flagOf

The built-in table of 49 countries with calling codes, patterns, trunk prefixes, and examples, and a helper that turns an ISO code into a flag emoji.

No props.

Keyboard interactions

KeysAction
+ (in the number)Opens the picker with a calling code search.
ArrowDown / ArrowUp (on the country button)Opens the picker.
Any letter (on the country button)Opens the picker searching for that letter.
ArrowDown / ArrowUp / PageDown / PageUpMoves through countries while searching.
Home / EndFirst or last country when the search is empty.
EnterPicks the highlighted country and returns to the number.
EscapeCloses the picker and returns to the country button.
Backspace / DeleteOver a separator, deletes the digit beside it.

Accessibility

Motion

Responsive behavior

Performance

Notes for AI

Also in special inputs

Guidance for AI tools

Phone input: A phone field with a country picker, formatting as you type, and E.164 output. Follow the declared prop types and do not invent props. Keep keyboard access, reduced motion support, and both light and dark themes intact when adapting it.

Full library index: https://ui.xzero.co.uk/llms.txt

Browse all components