# Phone Input

> A phone number with its country's flag and calling code. Type it as written at home or with its code; it's handed over as +447700900123.

Page: https://uicontext.com/docs/components/phone-input

Group: Primitives.

## When to use

A phone number with its country's flag and calling code. Type it as written at home or with its code; it's handed over as +447700900123. Use [Input](https://uicontext.com/docs/components/input.md) instead when the number is free text you won't call or text, so no country or format matters.

## Usage

```ts
import { FormField } from "./ui/uicontext/components/FormControls.ts"
import { PhoneInput } from "./ui/uicontext/components/PhoneInput.ts"

export const Phone = <M>(h: Html.H<M>, onChange: (value: string, valid: boolean) => M) =>
	FormField(h, { id: "phone", label: "Phone" }, (field) =>
		PhoneInput(h, { props: { field, country: "US" }, onEmit: { RequestedChange: ({ value, valid }) => onChange(value, valid) } }))
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A mobile number for sign-in

Try 07700 900123, or paste +1 415 555 0142: the code picks the country.

This example also uses Button. Install it too:

```sh
npx shadcn@latest add checkfu/uicontext/button
```

```ts title="src/demo.ts" source="www/src/examples/phone-input/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Button } from "./ui/uicontext/components/Button.ts"
import { FormField } from "./ui/uicontext/components/FormControls.ts"
import { PhoneInput } from "./ui/uicontext/components/PhoneInput.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	form: { display: "grid", gap: space.lg, inlineSize: "min(100%, 24rem)" },
	note: { margin: 0, fontSize: font.sm, color: color.muted, overflowWrap: "anywhere" },
	value: { fontFamily: font.mono, color: color.fg },
})

const Message = UiSchema.TaggedEnum({
	ChangedPhone: { value: Schema.String, valid: Schema.Boolean },
	Submitted: {},
})

// two-step sign-in asks for a mobile number: type it as written at home, or paste it with its code
export const MobileNumber = Html.component({
	name: "MobileNumber",
	model: Schema.Struct({ value: Schema.String, valid: Schema.Boolean, tried: Schema.Boolean, sent: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ value: "", valid: false, tried: false, sent: false }),
	update: {
		ChangedPhone: ($, { value, valid }) => $.evolve({ value, valid, sent: false }),
		Submitted: ($) => $.evolve({ tried: true, sent: $.model.valid }),
	},
	view: (h, model) => {
		const error = model.tried && !model.valid ? (model.value === "" ? "Enter a mobile number." : "That number is too short or too long for its country.") : undefined
		return h.form.attrs({ novalidate: "", ...sx(styles.form) }).on("submit", Message.Submitted()).children(
			FormField(h, { id: "mobile", label: "Mobile number", description: "We'll text a code to it when you sign in.", required: true, ...(error === undefined ? {} : { error }) }, (field) =>
				PhoneInput(h, {
					key: "mobile",
					props: { field, country: "GB" },
					onEmit: { RequestedChange: ({ value, valid }) => Message.ChangedPhone({ value, valid }) },
				})),
			Button(h, { tone: "primary", type: "submit" }).children("Send code"),
			h.p.attrs({ role: "status", ...sx(styles.note) }).children(
				model.sent ? "Code sent to " : "Saved as ",
				h.span.attrs(sx(styles.value)).children(model.value === "" ? "—" : model.value),
			),
		)
	},
})
```

### A few countries, a saved number

`countries` sets which countries are offered and in what order. `locale` sets the language of their names.

```ts title="src/countries.ts" source="www/src/examples/phone-input/countries.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { FormField } from "./ui/uicontext/components/FormControls.ts"
import { PhoneInput } from "./ui/uicontext/components/PhoneInput.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({ grid: { display: "grid", gap: space.lg, inlineSize: "min(100%, 24rem)" } })

// a shop that ships to a few countries offers only those, and fills in a saved number
export const ShippingContact = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.grid)).children(
		FormField(h, { id: "ship-phone", label: "Phone for the courier", description: "Only for delivery questions." }, (field) =>
			PhoneInput(h, { key: "ship", props: { field, value: "+4915123456789", countries: ["DE", "AT", "CH", "NL", "FR"] } })),
		FormField(h, { id: "ship-phone-fr", label: "Phone, in French", optional: true }, (field) =>
			PhoneInput(h, { key: "ship-fr", props: { field, country: "FR", locale: "fr", countries: ["FR", "BE", "CH", "LB"] } })),
		FormField(h, { id: "ship-phone-off", label: "Office line" }, (field) =>
			PhoneInput(h, { key: "office", props: { field, value: "+12125550199", disabled: true } })),
	)
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `field` | `FieldAttrs` | — | The attributes a Form Field hands its control: id, description and error wiring. |
| `label` | `string` | — | The field's name when there's no Form Field: "Mobile number". |
| `value` | `string` | — | The number to start with, as "+447700900123". |
| `country` | `string` | `"US"` | The country picked when the value doesn't say; "US" by default. |
| `countries` | `string[]` | `phoneCountries` | The countries offered, by code, in order; all of `phoneCountries` by default. |
| `locale` | `string` | — | The language country names are written in; the browser's by default. |
| `disabled` | `boolean` | — | — |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `RequestedChange` | `{ value: string; country: string; valid: boolean }` | The number changed: as "+447700900123" ("" when empty), its country, and whether it has the right digits. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/phone-input
```

With npm: `npx shadcn@latest add checkfu/uicontext/phone-input`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/phone-input`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/phone-input`.

It writes these files:

- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/PhoneInput.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/PhoneInput.ts](https://github.com/checkfu/uicontext/blob/main/src/PhoneInput.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
